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 :

enter image description here

What I try to do is :

enter image description here

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

enter image description here

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

enter image description here

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

    camRadians = atan2(opposite, adjacent)
    println("deg: \(convertRad2Deg(camRadians))")


  • \$\begingroup\$ 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 ? \$\endgroup\$ – dimitris93 Mar 24 '15 at 0:04
  • \$\begingroup\$ @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? \$\endgroup\$ – Paul Mar 24 '15 at 0:07
  • \$\begingroup\$ 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 \$\endgroup\$ – dimitris93 Mar 24 '15 at 0:09
  • 1
    \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ – dimitris93 Mar 24 '15 at 0:33
  • 1
    \$\begingroup\$ @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! \$\endgroup\$ – 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!
radians = atan2(_playerSpeedY, _playerSpeedX)
degrees = radians * 57.29577951

I get radians = 2.0344439357957027 and degrees = 116.56505117080718

(I used python and just typed:

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


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

| improve this answer | |
  • \$\begingroup\$ 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? \$\endgroup\$ – Paul Mar 24 '15 at 0:10
  • \$\begingroup\$ 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... \$\endgroup\$ – david van brink Mar 24 '15 at 1:13
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Paul Mar 24 '15 at 2:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.