More of a math problem

Reply

Join Date: Feb 2009
Posts: 36
Reputation: smoore is an unknown quantity at this point 
Solved Threads: 1
smoore's Avatar
smoore smoore is offline Offline
Light Poster

More of a math problem

 
0
  #1
May 3rd, 2009
Okay so I have an ellipse and I could like to have a "racecar" (actually just a 10x10 rectangle) follow the ellipse around the edge. The point that is always touching the ellipse is the upper left hand corner of the "racecar." I'm not even sure where to begin writing this code. I want to make it move a random distance along the edge of the ellipse. so i guess the code could look something like this:
  1. while(!raceOver)
  2. {
  3. car.moveRandomDistance();
  4. repaint();
  5. waitSomeTime(500);
  6. }
This is a thread so this would be inside the run() method and also inside try{}catch{}

The only advice I got was to use angles and to update the angle each loop but not sure about that.

Any help would be awesome. Thanks.

Some more info:

- the waitSomeTime() is simply made into a method in order to synchronize the methods and ensure one finishes before moving on

- car is the variable that holds a racecar object

- The car.move() method takes two ints, the x coordinate and y coordinate of its next location. They make up the point of the upper left hand corner.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: More of a math problem

 
1
  #2
May 4th, 2009
OK, so the basic idea is that you can use sine and cosine to convert from an angle to a position on the screen. If r is the radius, then the (x) and (y) coordinates are r * Math.sin(angle) and r * Math.cos(angle), where angle is measured in radians (so goes from 0 to 2 * PI to complete the circle/ellipse), and where you may need to swap round sin/cos depending on whether you start at 12 o'clock or 3 o'clock. And with an ellipse, your radius actually changes as you go round...

So the idea is that each time the car moves, you nudge the angle on by some random amount and then re-calculate the coordinates.

There's actually a problem with this approach: because the radius of the ellipse varies as you go round, the amount of "real distance" covered by a particular change in angle also varies as you go round. So your cars will appear to move faster at the points where the radius is larger.

A couple of ways round this are to (a) either make the maximum "nudge" vary depending on the radius, or (b) each time calculate a DIRECTION based on a fixed increment to the angle, then calculate a random movement in this direction.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC