How does atan2() works?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iuripugliero

Does the atan2() function uses the global X axis? If so, how can I use the local object X axis in order to properly steer it?

:bust_in_silhouette: Reply From: skysphr

The function simply returns the arc tangent of the values you give it. If you are trying to calculate the bearing to a vector relative to a different vector, you can either use Vector.angle_to_point() or subtract the vectors and calculate the atan2 between the result’s coordinates.

I did the second approach, and since it returns the angle between the passed vector (in this case, “target minus position”) and the X axis, I expected it to return in local coordinates. Thing is, it just dont change after rotating the object in place (since the position vector hasn’t changed at all and the angle is between this vector and the global X).
I thought about sum or subtract this angle on the object rotation, so it would “apply” to his own directions, however, i think it require some math coding. Anything I may be missing?

iuripugliero | 2021-10-26 02:44

You mean you want the angle between one vector and another? Vector2.angle_to() might come in handy.

skysphr | 2021-10-26 10:33