Getting the angle of an object relative to another

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.


boolean direction = 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.


boolean xx = input.x - x;
boolean yy = 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 :slight_smile:

Math.toDegrees(Math.atan2(yy, xx));

Thanks, solved half the problem. Got me to 0 to 180 and -180 to 0 all the way around.
Just had to normalize the negativity and subtract from 360 and all works fine now :stuck_out_tongue:

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 :P,

h3ckboy

What you said is basically what I’ve managed to do now :stuck_out_tongue:
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:


			//Get the input x/y and make it relative to the objects position and centre
			xx = inX - x - 32;
			yy = (inY - y - 32)*-1;
			
			//Calculate the direction and convert to degrees
			direction = Math.atan2(yy, xx)/Math.PI*180;
			
			//If the direction is negative, then normalise it
			if (direction < 0){
				direction = direction*-1;
				direction = 360 - direction;
			}

Works fine all around, 0 to 360 degrees.

And I really should be revising maths too but I’m tired. Ironic that my way of having a break involves more maths. (Curse you sine and cosine rules)

Thanks though :smiley:

Why negate then subtract from 360? Just add 360 to it and that’s all :stuck_out_tongue:

Also why don’t you want negative degrees? The work fine everywhere AFAIK :wink:

Hey, it works. That’s all I need xD Now I’m curious… Let’s try changing it! (Darn you!)

Where are you using that angle?