Underneath every game is a loop that runs again and again, usually 60 times per second or more:
while game is running:
read input
update the world
draw the world
Engines like Godot, Unity and Unreal run this loop for you and call your code at the right moment (_process, Update, Tick). It's still worth understanding, because one detail trips up almost every beginner, and once you see it you'll spot it in other people's code everywhere.
The bug: speed depends on frame rate
// Moves 5 pixels every frame
player.x += 5;
At 60 frames per second the player moves 300 pixels per second. On a 144 Hz monitor they move 720 pixels per second, more than twice as fast. On a slow laptop that only manages 30 frames per second, they crawl at 150. The same code gives three different games.
The fix: delta time
Delta time (dt, or delta) is the number of seconds since the last frame, for example 0.0167 at 60 FPS. Multiply speeds by it, and movement becomes "per second" instead of "per frame":
const speed = 300; // pixels per second
player.x += speed * dt;
Now the player moves 300 pixels every second on every machine. At 60 FPS each frame moves 5 pixels; at 144 FPS each frame moves about 2.1 pixels, but there are more frames, so the total per second is the same.
Where to use delta time
- Movement and rotation:
position += velocity * dt - Timers:
cooldown -= dt, and fire when it reaches zero - Acceleration:
velocity += gravity * dt
Do not multiply things that happen once, such as "add 10 points when a coin is collected". Delta time is for things that happen continuously over time.
A browser game loop
let last = performance.now();
function frame(now) {
const dt = Math.min((now - last) / 1000, 0.1); // seconds, capped to survive tab switches
last = now;
update(dt);
draw();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
requestAnimationFrame calls frame once per screen refresh and passes in the current time in milliseconds. The cap matters: browsers pause hidden tabs, so if the player switches tabs for 5 seconds, an uncapped dt of 5 would teleport everything at once. Our HTML5 canvas guide uses this loop to build a complete game.
The same idea in Godot
Godot passes delta time into _process and _physics_process as delta. A cooldown timer for a weapon, using an input action called shoot that you add under Project Settings → Input Map:
extends Node2D
const FIRE_DELAY := 0.25 # seconds between shots
var cooldown := 0.0
func _process(delta: float) -> void:
cooldown = max(cooldown - delta, 0.0)
if Input.is_action_pressed("shoot") and cooldown == 0.0:
shoot()
cooldown = FIRE_DELAY
func shoot() -> void:
print("Bang")
Hold the button and it fires four times a second on any computer.
Worked example: a jump with gravity
Movement with gravity is where delta time matters most, because velocity changes every frame. Here's a complete Godot 4 player on a CharacterBody2D:
extends CharacterBody2D
const SPEED := 300.0 # pixels per second
const GRAVITY := 1200.0 # pixels per second, per second
const JUMP_VELOCITY := -500.0 # negative is up in Godot 2D
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity.y += GRAVITY * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
velocity.x = Input.get_axis("ui_left", "ui_right") * SPEED
move_and_slide()
Notice two things. Gravity is multiplied by delta, because it changes velocity a little every frame. The jump is not, because it happens once: you set the velocity to a value. And move_and_slide() already multiplies velocity by the frame time for you, which brings us to the most common mistake.
Common mistakes
- Multiplying by delta twice. In Godot,
move_and_slide()and Unity's physics velocities already handle time. Writingvelocity = direction * SPEED * deltabeforemove_and_slide()makes your character crawl, and the speed changes with frame rate again. - Using delta for one-off events. Jumps, points, damage from a single hit: set or add them once, without delta.
- Smoothing with a fixed fraction.
x = lerp(x, target, 0.1)every frame moves faster at high frame rates. A frame-rate-independent version isx = lerp(x, target, 1 - Math.exp(-10 * dt)), where 10 controls how quickly it catches up. - Not capping delta. A stall from loading a file or switching tabs produces one huge
dt, and objects pass straight through walls. - Physics in the wrong callback. In Godot, move physics bodies in
_physics_process, not_process, so collisions are checked at a steady rate.
Test it: change the frame rate on purpose
You can't see a frame-rate bug on your own machine, because your machine always runs at the same rate. So change the rate deliberately and check that nothing else changes:
- In Godot, add
Engine.max_fps = 20to any_ready()function (or set Application → Run → Max FPS in Project Settings) and play. Then try 144. A character that crosses the screen in two seconds should still take two seconds, just more or less smoothly. - In the browser, open your developer tools, turn on CPU throttling in the Performance panel, and play again. Jump heights and timer lengths should stay the same.
- Time something real. Use a stopwatch for a five-second cooldown or a full jump. Compare the result at 20 and 144 frames per second. If they differ by more than a few hundredths of a second, look for a speed that's not multiplied by delta, or one that's multiplied twice.
Remove the frame cap when you're done. This takes five minutes and catches the bug before a player with a fast monitor finds it for you.
Going further: fixed timesteps
Physics simulations behave best when they step by the same amount every time. The standard solution is to run physics on a fixed step (for example 1/60 s) and render as often as possible, using an accumulator:
const STEP = 1 / 60;
let accumulator = 0;
let last = performance.now();
function frame(now) {
accumulator += Math.min((now - last) / 1000, 0.25);
last = now;
while (accumulator >= STEP) {
update(STEP); // always the same step: stable and repeatable
accumulator -= STEP;
}
draw(accumulator / STEP); // 0..1: how far between two updates, for smooth interpolation
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
Glenn Fiedler's free article Fix Your Timestep! is the classic explanation, including the interpolation step. Most engines already do this for their physics (Godot's _physics_process, Unity's FixedUpdate), which is why physics code belongs there.
What we learned building Tank Arcade
Frame-rate independence paid off in a practical way when we made Tank Arcade.
The arcade needs to know what a believable score looks like, so it can hold back impossible ones for review. To find out, we wrote a bot that plays the game, and ran it headless with Godot's --fixed-fps 60 option. That flag tells the engine to pretend every frame took exactly one sixtieth of a second, however long it really took. With no window to draw, the game then runs many times faster than real time, and because every frame is the same length, a thirty-minute game gives the same kind of result whether it ran in thirty minutes or three.
It only works because the game's movement, timers and spawning all use delta properly. A game that moved things "5 pixels per frame" would behave completely differently at fixed and real frame rates, and the measurements would be useless. With delta time everywhere, the bot's runs mean something: its honest games scored 3 to 79 points per second, and those measurements became the arcade's limits.
So delta time isn't only about fairness between a 60 Hz laptop and a 144 Hz monitor. It's what lets you test your game at any speed you like.
Where this fits
If you're following Start making games today, for free, this is the fix for the "ball moves at different speeds on different computers" trap in the Pong checklist. Once movement is frame-rate independent, you can safely tune how it feels, which is what Game feel: make every action satisfying is about.
Read more
- Game Programming Patterns: Game Loop: the chapter that covers this in depth.
- MDN: Anatomy of a video game: the browser game loop, step by step.
- Godot: Idle and physics processing: when Godot calls each function.
Sources & credits
This guide is original writing by Santoshi Games. It draws on, and links to, the free resources below. All rights to them belong to their creators. Please support them.
- Fix Your Timestep!, Glenn Fiedler (Gaffer On Games)
- Game Programming Patterns: Game Loop, Robert Nystrom
- Anatomy of a video game, Mozilla (MDN)
- Idle and physics processing, Godot Engine documentation
- MonoBehaviour.FixedUpdate, Unity documentation