# How to use atan2 to get the correct angle? (inside or outside angle) [duplicate]

I would like to rotate an object (white rectangle) towards another object (black rectangle). EDIT: the white rectangle (at the top) has a camera. I finally rotated the camera by 180 around the "y" axis, because it was looking to the right of the image. Now, the goal is to rotate the camera around the "x" axis, by the green angle.

I found some images that explains atan2 :

What I try to do is :

Edit : the green angle seems wrong, here is a new one :

If I refer to the trigonometry circle, I should have a value around -120 :

Would someone know what is happening? Here is the code I am using :

    //boxNode is the black rectangle, boxYellow is the white rectangle at the top
//163 degrees
let opposite = boxNode.position.z - boxYellow.position.z
let adjacent = boxNode.position.y - boxYellow.position.y

//-73 degrees
//let opposite = boxNode.position.y - boxYellow.position.y
//let adjacent = boxNode.position.z - boxYellow.position.z



Thanks

## marked as duplicate by congusbongus, msell, bummzack, MichaelHouse♦Mar 25 '15 at 21:43

• are you trying to rotate an object or move it ? what do you mean by "rotating the white rectangle towards the black rectangle" ? you want the white rectangle to be touching the rectangle's largest angle ? – dimitris93 Mar 24 '15 at 0:04
• @Shiro I am trying to rotate the white rectangle so that it looks in the direction of the black rectangle. Actually, I am trying to get the rest of the angle now, to try, the angle inside the triangle, not the red circle I have drawn, but the rest of it, until we reach 2 pi. Would you know how to do this? – Paul Mar 24 '15 at 0:07
• you need to describe better what you are trying to say, because "black rectangle" doesn't define any direction. in fact their sides are parallel. i will soon post an answer, let me know if it answers your question – dimitris93 Mar 24 '15 at 0:09
• – dimitris93 Mar 24 '15 at 0:33
• @Shiro OK I had to inverse the subtraction of "position.z" because -Z is on the right, and on a graph, the "positive" axis should be on the right. Then, subtracting 180 to the red angle (given by atan2), it works fine. Thank you very much for the help! – Paul Mar 24 '15 at 2:19

Your drawings seem inconclusive with respect to axis names and signs.

Just going by the first illustration, you could say approximately:

_playerSpeedY = 2
_playerSpeedX = -1   // going to the left, negative!


I get radians = 2.0344439357957027 and degrees = 116.56505117080718

(I used python and just typed:

bash: py
>>> import math
>>> math.atan2(2,-1)
2.0344439357957027
>>> math.atan2(2,-1) * 57.29577951
116.56505117080718
>>>


)

So... do it like that! :-)

• Thanks, you are right, I placed my white rectangle differently and the result looks good... in the logs. Inside the white rectangle, I added a camera. I think I need to rotate the camera first by 180degrees, which I did. But I now need only the rest of the angle, that goes until 2 pi. In my drawing, the angle inside the triangle, the rest of the red circle in order to complete 2 pi. Would you know how to do this? – Paul Mar 24 '15 at 0:10
• In Picture #2, there are two graphs shown. The axes are not clear to me, so I'm going to talk about them as if they are perfectly normal X and Y. X is positive to the right, Y is positive up. The top one has a red angle. Call it redAngle = atan2(-1,-1). The top one has a second green angle, which we could think of as greenAngle = atan2(0,-1) - redAngle. The bottom one has a green angle, which we could describe as bottomGreenAngle = atan2(-1,-1) - atan2(-1,0) = atan2(-1,-1) - pi. You can substitute in your Z, or negative-Z, or whatever instead... – david van brink Mar 24 '15 at 1:13
• Thanks david for the help, I made it work like that : I had to inverse the subtraction of "position.z" because -Z is on the right, and on a graph, the positive should be on the right. Then, subtracting 180 to the red angle (given by atan2), to get the green angle from the second image :) Thanks! – Paul Mar 24 '15 at 2:21