Game dev
Saved & recent

atan2

Also called: two-argument arctangent

Related terms: trigonometry, aiming

Turns a 2D direction into an angle, accounting for which quadrant it points into. In many languages the call is atan2(y, x), with Y first, and the result is in radians. Check the API argument order.

To aim at a target, subtract the shooter position from the target position, then pass that direction to atan2. A zero-length direction has no meaningful angle; keep the previous facing instead.

angle = atan2(target.y - y, target.x - x)

Further reading
Python documentation: math.atan2(y, x)
X Y (3, 2)0.59 rad (-3, 2)2.55 rad (-3, -2)−2.55 rad (3, -2)−0.59 rad
All four arrows have the same absolute components. atan2(y, x) uses both signs to return the angle from +X, positive counterclockwise and negative clockwise.

View in the full Game dev collection