Wasn't sure which board to post this in but nevertheless. If it needs moving, move it.

I'm working on a game and I need help with some math.What I'm trying to do is get the angle (direction) of the mouse cursor relative to the object checking the angle.

I've applied what I have learned from class but I'm not sure if I'm doing it quite right here.

1

booleandirection = Math.atan(yy/xx)/Math.PI*180;

This works mostly as I get an angle returned, and in degrees not radians.Only the angle passes from 0 to 90 and 0 to -90. Repeating itself at certain angles that would otherwise be something like 200 degrees.The other problem is xx and yy can be negative, because the values are relative to the object on the screen.

1 2

booleanxx = input.x - x;booleanyy = input.y - y;

I tried by forcing the values to always be positive but the angles were still messed up.

But don't get me wrong, this all works from 0 to 90 degrees flawlessly. Although if I go to around where 360-270 degrees should be, it shows 0 to -90 degrees instead.It's like a sine wave all the way around: 0 to 90, 90 to 0, 0 to -90, -90 to 0.

I'd just rather this go from 0-360 all the way around. If anyone can help I'll be most thankful

I'm sorry if I am not answering your question correctly, but from what I understand you are having domain/range restriction issues (I cannot remember which is the correct one)?

in order to determine if the restriction is obscuring the correct answer, check the x value, if the x value is negative then you have to add or subtract 180 to get what would have been the correct angle.

e.g, if you have a x change of -sqrt(2)/2 (I'm keeping it on the unit circle for simplicity), and a y value of sqrt(2)/2 then you have atan ((sqrt(2)/2)/(-sqrt(2)/2))

this will give you -pi/4 (or -45 degrees) because of the domain restrictions, but you know that it is wrong because the x value is negative, so you simply add/subtract 180 degrees (or pi whichever) to get 3pi/4 or -5pi/4 ( 135 or -225 degrees)

I apologize if the math makes no sense or the notation is ugly (typing math really sucks), but the main gist is this:

if you have a negative x change value (so the bottom part of your tan value) then you must add or subtract(completely preference which you choose) pi/2 (180 degrees) from your final angle to compensate for the restriction.

hope this helps, I apologize if I am completely off-base I am a little fried from studying ,

I'm sorry if I am not answering your question correctly, but from what I understand you are having domain/range restriction issues (I cannot remember which is the correct one)?

in order to determine if the restriction is obscuring the correct answer, check the x value, if the x value is negative then you have to add or subtract 180 to get what would have been the correct angle.

e.g, if you have a x change of -sqrt(2)/2 (I'm keeping it on the unit circle for simplicity), and a y value of sqrt(2)/2 then you have atan ((sqrt(2)/2)/(-sqrt(2)/2))

this will give you -pi/4 (or -45 degrees) because of the domain restrictions, but you know that it is wrong because the x value is negative, so you simply add/subtract 180 degrees (or pi whichever) to get 3pi/4 or -5pi/4 ( 135 or -225 degrees)

I apologize if the math makes no sense or the notation is ugly (typing math really sucks), but the main gist is this:

if you have a negative x change value (so the bottom part of your tan value) then you must add or subtract(completely preference which you choose) pi/2 (180 degrees) from your final angle to compensate for the restriction.

hope this helps, I apologize if I am completely off-base I am a little fried from studying ,

h3ckboy

What you said is basically what I've managed to do now I knew the negativity would be a problem but it had to be there for the object to take the direction relative from its position.The only other thing to do was reverse the Y as computers have it mirrored from what we learn in class. Nothing tricky though.

The finished result:

1 2 3 4 5 6 7 8 9 10 11 12

//Get the input x/y and make it relative to the objects position and centrexx = inX - x - 32;yy = (inY - y - 32)*-1;

//Calculate the direction and convert to degreesdirection = Math.atan2(yy, xx)/Math.PI*180;

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org