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.
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.
Two dots race across the screen at a chosen frame rate.
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.
var velocity := Vector2(60, 0) # pixels per second
func _process(delta):
position += velocity * delta # same speed at any frame rateChanging 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.
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.0 | 0 | 0 | 0 |
| 0.1 | 98 | 9.8 | 4.9 |
| 0.2 | 196 | 29.4 | 19.6 |
| 0.3 | 294 | 58.8 | 44.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.
var velocity := Vector2.ZERO
const GRAVITY := Vector2(0, 980)
func _physics_process(delta):
velocity += GRAVITY * delta # acceleration changes velocity
position += velocity * delta # velocity changes positionForgetting 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.
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.
