# Using atan2 vs dot product to get an angle in 2D games

I have some confusion that I need to be cleared up about the atan2 function.

I am making a game in Godot where a 2D ship rotates to face some object in space by using atan2 and it is working good. Atan2 finds angle between that object and the ship, right?

If so, what is with dot product then, since dot product also finds angle? I have a feeling it's very simple but I'm confused about the difference between these functions.

atan2(v.y, v.x) gives you the bearing angle of a single vector. That is, the signed angle of rotation measured counter-clockwise from the positive x axis to that vector. Vectors with a negative y component will give you a negative (clockwise) angle. So this gives you an absolute reference coordinate system to rotate to exactly this direction.

The dot product of two vectors gives you the cosine of the angle between them, multiplied by the length of each vector. If we normalize the vectors first so they both have length 1, then this simplifies to just the cosine, and we can take the arc cosine of that value to get an angle:

angle = acos(dot(normalize(a), normalize(b)))

This tells you this magnitude of the angular gap between them, but unlike atan2 it does not tell you the direction of rotation - you will only ever get positive angles this way. In the diagram below, both acos(dot(a, b)) and acos(dot(c, b)) will give you the same angle:

So this is no longer an absolute angular coordinate but a relative measure. That means the dot product is good for measuring how parallel two vectors are to each other (1 = 0° = parallel, 0 = 90° = perpendicular, -1 = 180° = anti-parallel), but not good for determining which direction to rotate to get from one to the other.

To get that, you'd need to construct a perpendicular to one of the vectors (which in 2D is as easy as interchanging its x & y components and negating one of them), then take the dot product between the other vector and this perpendicular. Positive means rotation toward your chosen perpendicular, negative means rotation away.

• Thank you very much. Expertly explained!! Nov 18, 2022 at 17:16