Math for Programming / Part 1: Space and motion

Angles: turning a direction into numbers

Sine and cosine sound like advanced math, but in a program they do one friendly job: they turn an angle into an arrow, and atan2 turns it back.

Lesson 5 of 13About 12 minutesBuilds on: right-triangle trigonometry

Two ways to measure a turn

A full turn is 360 degrees. Programmers also use radians, which measure a turn by arc length: walk around a circle of radius 1, and the distance you travel is the angle in radians. A full turn is 2π ≈ 6.283 radians, a half turn is π ≈ 3.14159, and a quarter turn is π/2 ≈ 1.571. One radian is about 57.3°. Most programming languages, including GDScript, expect radians, so you will often convert:

radians = degrees × π ÷ 180    degrees = radians × 180 ÷ π

GDScript
print(deg_to_rad(90))   # 1.5707...
print(rad_to_deg(PI))   # 180
rotation = deg_to_rad(45)  # turn a node 45 degrees

Cosine and sine turn an angle into a direction

Picture a point on a circle of radius 1, centered at the origin. As the angle grows, the point travels around the circle. Its x-coordinate is cos θ and its y-coordinate is sin θ. That is all cosine and sine are in a game: the horizontal and vertical parts of a one-unit arrow pointing at angle θ.

Worked example

At 30°, cos 30° ≈ 0.866 and sin 30° = 0.5. A bullet fired at 30° with speed 400 px/s has velocity (400 × 0.866, 400 × 0.5) ≈ (346, 200). Check the length: √(346² + 200²) ≈ 400. The speed is preserved.

Figure

Move the angle and watch the point travel around the circle.

Going backward: which way should I face?

Often we know the arrow and want the angle: a turret at (0, 0) needs to aim at a target offset (3, 4). The tool for this is atan2(y, x), an arctangent that looks at the signs of both parts so it can tell all four quadrants apart. For (3, 4), atan2(4, 3) ≈ 0.927 radians ≈ 53.13°. On screen, where y points down, that is 53° clockwise from “right.”

GDScript
func _process(delta):
    var to_mouse := get_global_mouse_position() - global_position
    rotation = atan2(to_mouse.y, to_mouse.x)   # same as to_mouse.angle()
Common mistake

Plain arctangent of y ÷ x cannot tell (3, 4) from (−3, −4), because both give 4/3. The turret would point exactly backward half of the time. Use atan2, which keeps the signs.

Circles, orbits, and waves

If the angle increases steadily over time, (cos θ, sin θ) walks around the circle forever. Multiply by a radius and add a center, and you have an orbit: center + Vector2(cos(t), sin(t)) * radius. Use only the sine part, and you get a smooth up-and-down wave, perfect for a floating coin or a bobbing boat.

GDScript
var t := 0.0
func _process(delta):
    t += delta
    position.y = 300 + sin(t * 2.0) * 10   # bob 10 px up and down
Where this comes from in school math

Geometry introduces sine, cosine, and tangent as ratios of the sides of a right triangle: SOH-CAH-TOA. The unit circle from Algebra II or precalculus extends them to every angle, including angles past 90° and negative angles. A game uses the unit circle constantly, usually without naming it.

Where you will see it

  • Aiming: turrets, cameras, and characters that face the mouse.
  • Projectiles: turning an angle and a speed into a velocity.
  • Animation: orbits, pendulums, waves, and breathing effects.
  • Sound: a pure musical tone is a sine wave.

Exercises

Convert 270° to radians.

270 × π ÷ 180 = 3π/2 ≈ 4.712.

What direction vector points at 90° on a school graph? On a screen?

cos 90° = 0 and sin 90° = 1, so (0, 1). On a school graph that points up; on a screen it points down.

A coin bobs with y = 200 + 8·sin(t). What are its highest and lowest y-values?

sin ranges from −1 to 1, so y ranges from 192 to 208.