Skip to content

Find the angle to a target

  • The Craft

We know where the target is. Now work backwards from its coordinates to a direction, then test that direction with a shot.

Drag in the picture or use the labelled inputs and sliders. Grid squares span 25 world units. This is a mathematical model running in your browser.

Subtract before finding the angle

For a launcher at (sx, sy) and target at (tx, ty), calculate Δx = tx − sx and Δy = ty − sy. Our launcher is at (0, 0), so these differences equal the target coordinates.

Tangent relates the two components: tan(angle) = Δy / Δx. Its inverse, atan, can recover an angle from a ratio, but the ratio loses information. Targets at (100, 100) and (−100, −100) both give a ratio of 1, although they lie in opposite directions.

atan2(Δy, Δx) accepts the two components separately, retaining their signs. It also handles a vertical direction without dividing by Δx = 0. At the origin itself, no direction is defined. Our targets always have a non-zero distance.

For target (−100, 100), should the angle be 45° or 135°?

Show the explanation

135°. X is negative and Y is positive, so the direction points up and left. atan(100 / −100) gives −45°, which points down and right instead.

Predict, calculate, fire

Try a manual shot first. Reveal the calculation, use its angle and fire again. Next target visits other quadrants and a vertical target. A negative result from atan2 is a clockwise angle; adding 360° gives the equivalent anticlockwise angle.

The reference shot moves 3 units per tick with no gravity or drag. A hit means its path comes within 5 units of the target centre. Contact is checked along each movement segment, so a small target cannot be skipped between ticks. The trail continues beyond contact to show the direction.