943,770 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 410
  • Java RSS
May 3rd, 2009
0

More of a math problem

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 39
Solved Threads: 1
Light Poster
smoore is offline Offline
36 posts
since Feb 2009
May 4th, 2009
1

Re: More of a math problem

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.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JTextField in JComponent
Next Thread in Java Forum Timeline: hi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC