Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (755) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 math question  (Read 1581 times) 0 Members and 1 Guest are viewing this topic.
javatypo

Junior Devvie

 « Posted 2005-03-19 01:37:31 »

ok ill first draw a diagram..

[code]
0 degrees
|
|
|
|                      (x2,y2)
|  (x,y)
|_______________ 90 degrees

so up there i have two known points, and i want to find the angle at which (x,y) must be rotated to be facing (x2,y2). this is for my car racing game, im trying to line up (just the human) car at to the finish line. what im doing is finding the mid point of the finish/starting line, then i give it an offset (so thats is somewhere before the starting line) then i calculate the angle to the point on the starting line where the car will be perdendicular to.

i was using something like the rule cos(theta) = y/x, but for some reason that doesnt really work to great. it works if i calculate the angle the car must travel to hit the midpoint, but if i try and move that point, then for some reason it doesnt give me the expected results.

thanks for any help

Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #1 - Posted 2005-03-20 01:23:23 »

 1 `Math.atan2(y2 - y, x2 - x);`

How's that?

That's the angle of the first guy to the second guy.

GameLizard.com
Play Rimscape!    |    Play Conquer!
javatypo

Junior Devvie

 « Reply #2 - Posted 2005-03-21 20:09:33 »

nice okay i got that working, but now i have another problem..

im using the same method of lining up cars to lines in my game for the admin cars, so what i did was precalculate an array of angles so that as the ai car moved along and hit certain points it would take up a new angle. the track is "circle" which is made up of lines, and is drawn as a polygon, so if u can imagine that.. lol. the problem here is that if a line has an opposite slope to another line, then the angle will be the same.

so to draw
 1  2  3  4  5 `||               (x2,y2)|  | (x1,y1)|_________`

the angle here is say 30 (degrees) and the car should in theory be facing at (x2,y2) and accelerate that way when it accelerates

 1  2  3  4 `|             (x1,y1)||(x2,y2)|__________`

here the angle works out (if x1 and y1 become x2 and y2 respectively) to the same as the above example, so im wondering how to get what that angle would be if u took into consideration that the begining angle is zero.

or maybe if i just added that angle value to what ever is was before it hit that line?

Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #3 - Posted 2005-03-23 00:00:48 »

if the two points are the same, then why do you need to know the angle between them?  That's a silly question that you should never even ask.  If you've reached your destination, you need a new destination.