Determining angle between current and last position; 2D

Hi,

I have a player on a JPanel from a bird's eye view. I have to determine the angle (0 - 360) of the player's move:

0 or 360

|

|

|

|

270--Player--90

|

|

|

|

180

The top of the JPanel corresponds to 0 or 360 degree, the right side to 90 degree etc.

I have to determine the angle between the current and the last (x, y) coordinates of the player. The player is being moved by mouse clicks.

I don't know where to start to implement this... and how I can calculate the angle between these 2 positions (which I store in an Ellipse2D ArrayList).

Thanks for your help!

[678 byte] By [SFLa] at [2007-10-3 8:24:18]
# 1
protected double calcAngle(Point p1,Point p2) {return Math.atan2(p2.y-p1.y,p2.x-p1.x)*180.0/Math.PI;}
Woogleya at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 2
Hmmm... is it possible to modify the calculation so that I get 0 degree when I move up, 90 degree when I move right etc?Now I get ~ -178 by moving right, ~90 by moving up,...Thanks!
SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 3
Subtract 90 degrees from the result, then you'd get about 90 for right and about 0 for up.
patrickmallettea at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 4

OK, I now changed it to

return (Math.atan2(lp.getY() - p.getY(), lp.getX() - p.getX()) * 180.0 / Math.PI - 90);

When I keep on clicking down (moving down) I get about -180. When I then click right I get about -260. When I move left I get about -90 :(

Should I correct the result by changing the -90 in the formula or is it then completely wrong? :)

SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 5
it's because of [url= http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html]operator precedence[/url].this should work:return (Math.atan2(lp.getY() - p.getY(), lp.getX() - p.getX()) * 180.0 / Math.PI) - 90;
Woogleya at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 6
Thanks, however, it's still mixed up :)up: 0 (OK)down: -180right: -270left: -90Down is correct except the negative value. Left and right are interchanged and negative as well.
SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 7
Multiply them by -1. Do you understand how degrees work? Degrees go counterclockwise. So 0 degrees should be on the right, and 90 degrees should be straight up.
CaptainMorgan08a at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 8
lp.getY() - p.getY()If you multiply this part by -1, it almost works.
CaptainMorgan08a at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 9

> Multiply them by -1. Do you understand how degrees

> work? Degrees go counterclockwise. So 0 degrees

> should be on the right, and 90 degrees should be

> straight up.

Oh... I didn't know that degrees in Java go counterclockwise and start on the right. Sorry. Is there a way to tell Java to start "at the top" and go clockwise? Or is the solution to change the formula? Thanks! :)

SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 10

> Oh... I didn't know that degrees in Java go

> counterclockwise and start on the right. Sorry. Is

> there a way to tell Java to start "at the top" and go

> clockwise? Or is the solution to change the formula?

> Thanks! :)

Degrees in general work like that, not just in Java. Read reply #8.

CaptainMorgan08a at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 11

> Degrees in general work like that, not just in Java.

> Read reply #8.

Hmmm... if I consult my books I can read that 0 are at the top and then it goes clockwise. Maybe there are different ways for this... :)

Is there no way to get the degrees like in my first post? I changed the formula and tried to get the results in the way I need them but I wasn't successful :(

SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 12
Really no solution in Java 5? :(
SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 13
There is. Learn how degrees/radians work, then you should be able to come up with a solution. I think I gave you a way that worked earlier, too.
CaptainMorgan08a at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 14

Unfortunately your solution doesn't calculateted the degrees clockwise, starting at 0 degree at the top. I tried to change the formula so that it works the way I need it. I have my old school books here but we never learned it in-depth. I am not a mathematician so that I can change the formula and being certain that it's being done in a good way.

SFLa at 2007-7-15 3:30:26 > top of Java-index,Other Topics,Java Game Development...
# 15

> Unfortunately your solution doesn't calculateted the

> degrees clockwise, starting at 0 degree at the top. I

> tried to change the formula so that it works the way

> I need it. I have my old school books here but we

> never learned it in-depth. I am not a mathematician

> so that I can change the formula and being certain

> that it's being done in a good way.

Try this uncommented and untested (!) method:

public double angle(Point p1, Point p2) {

double radians = Math.atan2(p2.y-p1.y, p2.x-p1.x);

double degrees = radians*180.0/Math.PI;

if(degrees < 0.0) {

return -degrees+90;

} else if(degrees > 90) {

return 360-(degrees%90);

} else {

return 90-degrees;

}

}

Good luck.

prometheuzza at 2007-7-21 12:26:07 > top of Java-index,Other Topics,Java Game Development...