Math for Programming / Part 1: Space and motion

Rates: making movement depend on time

A rate answers “how much per unit of time?” That familiar idea is the bridge from a position on a screen to an animation.

Lesson 4 of 13About 12 minutesBuilds on: slope and rates

Start with speed

If a cyclist travels 12 kilometers in one hour at a steady pace, the speed is 12 kilometers per hour. A game uses the same arithmetic with screen units: a character moving at 80 pixels per second should cover 80 pixels in one second. The computer does not jump straight to the final position. It redraws the picture many times per second, and each redraw is called a frame. The time between frames, often called delta, can vary.

Worked example

At 80 pixels per second, a 0.25-second interval calls for a move of 80 × 0.25 = 20 pixels. A 0.5-second interval calls for 40 pixels. The rule is simply distance = rate × time.

The frame-rate trap

Suppose a beginner writes “move 2 pixels every frame.” On a computer drawing 60 frames per second, that is 120 pixels per second. On one drawing 30 frames per second, it is 60 pixels per second. The game literally runs at half speed on the slower machine. Multiplying by delta fixes it: the fast computer takes many small steps, the slow one takes fewer large steps, and both cover the same distance per second.

Figure

Two dots race across the screen at a chosen frame rate.

2 px per frame120 px per second × delta At 60 fps the two dots match. Try a lower frame rate.

Direction joins the calculation

Speed tells us how much; a vector tells us which way. Together they form a velocity. If a character’s velocity is (60, 0) pixels per second, then after 0.5 seconds its displacement is (30, 0). In two dimensions, multiply each part of the velocity by elapsed time and add the result to the position.

GDScript
var velocity := Vector2(60, 0)   # pixels per second
func _process(delta):
    position += velocity * delta  # same speed at any frame rate

Changing speed: acceleration and gravity

Velocity is the rate of change of position. Acceleration is the rate of change of velocity. Gravity is a constant acceleration: every second, a falling object gets faster by the same amount. A program can model that with two lines, run every frame: first update velocity with acceleration, then update position with velocity. This step-by-step method is called Euler integration.

A falling ball, one tenth of a second at a time

Gravity is 980 pixels per second², the ball starts at rest, and each step is 0.1 seconds.

Time (s)Velocity (px/s)Distance fallen (px)Exact ½·g·t²
0.0000
0.1989.84.9
0.219629.419.6
0.329458.844.1

Each row adds 98 to the velocity, then adds velocity × 0.1 to the distance. The stepped answer runs a little ahead of the exact formula, and smaller steps shrink the gap. Real game frames are about 0.017 seconds long, so the error is much smaller than in this table.

GDScript
var velocity := Vector2.ZERO
const GRAVITY := Vector2(0, 980)
func _physics_process(delta):
    velocity += GRAVITY * delta   # acceleration changes velocity
    position += velocity * delta  # velocity changes position
Common mistake

Forgetting delta in one of the two lines. If gravity is added without delta, objects fall faster on faster computers. Every “per second” quantity needs to be multiplied by elapsed time.

Where this comes from in school math

In Algebra I, slope tells us how much one quantity changes when another changes. On a distance-versus-time graph, the slope of a straight line is speed. Under constant acceleration, the distance graph becomes a parabola, the same y = ax² shape from the quadratics unit. Calculus later gives exact names to these rates (derivatives), but a program can already simulate them by adding small changes, one step at a time.

Where you will see it

  • Platformers: jumping, falling, and landing.
  • Animation: anything that should look the same speed on every device.
  • Science simulations: planets, springs, and populations change in small time steps.

Exercises

A car moves at 150 px/s. The last frame took 0.02 s. How far should it move this frame?

150 × 0.02 = 3 pixels.

A dot moves 3 px per frame, without delta. How fast is it at 60 fps? At 144 fps?

180 px/s at 60 fps and 432 px/s at 144 fps.

With gravity 980 px/s² and 0.1 s steps, what is the velocity after 0.5 s, starting from rest?

98 per step × 5 steps = 490 px/s.