| | |
More of a math problem
![]() |
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:
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.
Java Syntax (Toggle Plain Text)
while(!raceOver) { car.moveRandomDistance(); repaint(); waitSomeTime(500); }
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.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
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.
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.
![]() |
Similar Threads
- Problem with do-while loop (C++)
- Math problem with Pygame (Python)
- Help with Simple Math Problem (Python)
- Help needed with Math problem (C++)
- PLz I need help!!!..C++ problem solving (C++)
- Math problem in C (C)
- A discrete math problem (Computer Science)
Other Threads in the Java Forum
- Previous Thread: JTextField in JComponent
- Next Thread: hi
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows





