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)
atan2(y, x) uses both signs to return the angle from +X, positive counterclockwise and negative clockwise.