# The Game Loop: What Happens Inside One Frame and Why Movement Needs Delta Time

How the update-then-draw cycle works, and why multiplying speed by delta time keeps a game the same speed on every machine.

> delta time · frame rate · game physics · About 7 min · Nov 2

## Key points

1. A game is a repeating loop: read input, update state, draw, repeat — each pass is one frame, and 60 FPS means sixty passes per second.
2. The world lives as state (numbers such as position, velocity, timers); update changes that state, draw only paints it, and separating them makes the simulation independent of how fast drawing happens.
3. Moving a fixed amount per frame makes speed depend on hardware: `x += 1` covers 30 px/s at 30 FPS and 144 px/s at 144 FPS.
4. Multiplying rates by delta time (the seconds the last frame took) replaces pixels-per-frame with pixels-per-second, so 300 px/s covers 300 px in one second at any frame rate.
5. Only per-second rates get multiplied by delta; one-off events must not be, and countdowns should subtract delta from a value in seconds rather than count frames.
6. Coyote time written as 0.1 seconds of grace behaves identically at 30 and 144 FPS; written as a fixed number of frames, its real duration shrinks on faster hardware.
7. Raw variable delta fails for fast objects (tunneling through thin walls), unstable simulations, and reproducibility, which is why physics usually runs on a fixed timestep that can execute zero, one or several times per drawn frame.
8. Fixed timestep and fixed frame rate are different things: normally the simulation steps at a constant rate while the display draws at a variable one.

---

Every game you have played is the same function running over and over. It reads your input, changes the state of the world, paints the result, and immediately does it again — thousands of times until you quit. That repeating function is the **game loop**, and almost every rule about movement, timing and physics comes out of it.

This article builds three pieces and then connects them: the loop itself (update then draw), the **state** it carries from one pass to the next, and **delta time** — the one number that ties the loop to real seconds so a game does not run at different speeds on different machines.

## A game does not run once; it runs forever

An ordinary program starts, does its work, and exits. A game starts and then waits in a loop until you close it. Each pass through the loop produces one **frame** — one still picture, one tick of the world. "60 FPS" means this loop completed 60 times in the last second.

Inside a single pass, three things happen in order:

1. **Read input.** Which keys are held, where the mouse is, what arrived over the network.
2. **Update.** Turn that input into changes: move the player, run gravity, tick timers, move enemies, resolve collisions, check win conditions.
3. **Draw.** Take the current state of the world and paint it.

```mermaid
flowchart LR
  A["Read input"] --> B["Update: change state"]
  B --> C["Draw: paint state"]
  C --> A
  S["State: positions, velocities, timers"] --> B
  B --> S
  S --> C
```

## Update and draw are two different jobs

The thing being looped over is **state**: plain numbers sitting in memory. A player's state might be an `x` and `y`, a velocity, a health value, whether it is on the ground, and which animation frame it is on. No pixels involved — just numbers.

**Update reads state, changes state.** It never touches the screen.

**Draw reads state, paints it.** It rarely changes anything.

Keeping the two apart is what lets the same simulation work at 30 FPS on a laptop and 144 FPS on a desktop, and it is why a slow frame of drawing does not corrupt the simulation: the update still advances correctly on the next pass. Nearly every engine is built around this split — Godot spells it out as "each frame, you need to update the state of your game world before drawing it on screen" [3].

## The naive loop, and the bug everyone hits

Written plainly, moving a player looks like this:

```
while game is running:
    read_input()
    update()      # player.x += 1  ... move one pixel right
    draw()
```

Why is this wrong? Because `+1` happens *once per frame*, and the number of frames per second is not fixed.

| Display / frame rate | Frames per second | Distance after 1 second |
|---|---|---|
| Slow laptop | 30 | 30 px |
| Typical monitor | 60 | 60 px |
| Gaming monitor | 144 | 144 px |

Same code, same input — but the character is nearly five times faster on the gaming monitor. And frame rate is not even constant on one machine: a level load, a garbage-collection pause, or a browser throttling a background tab all stretch individual frames. A jump that clears a gap during a smooth stretch can fail in a busy moment, which feels like the game is being unfair rather than slow.

## Delta time: the length of the last frame, in seconds

The fix is to stop thinking "per frame" and start thinking "per second". To do that, the loop measures how much real time the last pass took:

$$\Delta t = t_{\text{now}} - t_{\text{previous}}$$

That value — written `dt`, `delta`, or `deltaTime` — is the frame duration in seconds. On a 60 FPS machine it is about $1/60 \approx 0.0167$ s; during a hiccup it might be $0.1$ s. Then movement becomes:

```
player.x += speed * dt      # speed is in pixels per second
```

Now watch what each machine actually does. Suppose `speed = 300`.

| Frame rate | $\Delta t$ | Movement this frame | Distance after 1 second |
|---|---|---|---|
| 30 | 0.0333 s | 10 px | 300 px |
| 60 | 0.0167 s | 5 px | 300 px |
| 144 | 0.00694 s | 2.08 px | 300 px |

The per-frame step differs; the speed in the world does not. That property has a name worth knowing — **frame-rate independence** — and it is why a well-written game feels the same on a cheap phone and a fast PC.

Where does `dt` come from? In a browser, `requestAnimationFrame` hands your callback a timestamp, and you subtract the previous one; MDN's walkthrough of the loop uses exactly that timestamp [2]. In an engine you usually do not measure anything: Unity exposes `Time.deltaTime`, and Godot passes `delta` into `_process(delta)`, documented as "the time elapsed in seconds since the previous call" [1][3].

## What must *not* be multiplied by delta time

Delta time is not a spell you sprinkle everywhere. The test is the unit of the number:

- **A rate** — pixels per second, degrees per second, gravity in units per second squared — must be multiplied by `dt`: `velocity.y += gravity * dt`.
- **A one-time event** — "set the jump velocity to −600 when the key is pressed" — must not be, or the jump gets weaker on slower frames.
- **A countdown in seconds** — a 3-second invulnerability, an attack cooldown, an animation timer — subtracts `dt` and is compared against zero, never against a frame count.

The most common bug in AI-written game code is a timer that counts frames ("wait 6 frames, then shoot") instead of seconds. It works on the machine where it was tested and quietly changes on every other machine.

## Coyote time: a rule that must be measured in seconds

**Coyote time** is the classic example, and it shows why delta time is a design decision, not just a physics detail. Players expect to still be able to jump for a moment after walking off a ledge — a slightly late input should still work. The usual implementation:

```
# on the ground:
coyote_timer = 0.1

# every frame:
coyote_timer -= dt

# on jump press:
if coyote_timer > 0 and not jumping:
    jump()
```

`0.1` seconds is a tenth of a second of grace, no matter the hardware: at 30 FPS that is 3 frames, at 144 FPS about 14. Written as "6 frames of grace" instead, the window is 0.1 s at 60 FPS but only 0.04 s at 144 — the mechanic is subtly broken on good monitors, which is the opposite of what players expect. Jump buffering (pressing jump slightly *before* landing) works the same way.

## Where plain delta time runs out

Using the measured frame time everywhere is called a **variable timestep**, and it has three known failure modes:

- **Tunneling.** A projectile at 1000 px/s with $\Delta t = 0.1$ s moves 100 px in a single step. If a 10 px wall sits in between, nothing ever checked the space it jumped over, so it passes straight through.
- **Instability.** Simulation math that reacts to motion (springy movement, stacks of physical objects) can behave very differently — or blow up — when one step is far larger than expected [4].
- **Non-reproducibility.** Two machines produce different sequences of `dt`, so identical inputs diverge. Replays, deterministic tests and multiplayer synchronisation all depend on identical step sizes.

Games that care about physics solve this with a **fixed timestep**: the simulation always advances in equal chunks, say $1/60$ s, and the loop accumulates real time and consumes it in whole chunks — running zero, one or several update steps in a frame, then drawing once. Engines expose both kinds of processing: Godot's `_physics_process` runs at a fixed 60 times per second by default, "independent of your game's actual framerate", while `_process` runs once per drawn frame [3]; Unity's `FixedUpdate` uses `Time.fixedDeltaTime`, 0.02 s (50 times per second) by default [1].

Two things are easy to confuse here, so state them precisely: a **fixed timestep** means the simulation advances in equal steps; a **fixed frame rate** means the display refreshes at a constant rate. They are independent — the normal setup is a fixed-step simulation under a display-driven, variable frame rate.

## What to check in code an AI wrote for you

When a coding tool hands you a game, these checks catch most timing bugs:

- Which function actually runs every frame? If you cannot identify it, you cannot reason about anything else.
- Is every continuous motion multiplied by delta — position, rotation, gravity, animation playback?
- Are gameplay timers in seconds and decremented by delta, rather than counting frames?
- Is physics or collision handled in a fixed step (or a capped delta), rather than whatever `dt` happened to arrive?

## The picture in one sentence

A game is a loop that updates state and then draws it, tens to hundreds of times per second; delta time is how the loop converts "once more" into "how much time passed", which is what lets a speed written in per-second units stay the same speed on any machine — and what lets a tenth of a second of coyote time feel the same to every player.

## Mind map

```mermaid
flowchart LR
  L["Game loop: repeats until quit"] --> F["One frame"]
  F --> I["Read input"]
  F --> U["Update: changes state"]
  F --> D["Draw: paints state"]
  S["State: positions, velocities, timers"] --> U
  U --> S
  S --> D
  U --> T["Timing: multiply rates by delta time"]
  T --> N["Not for one-off events"]
  T --> C["Timers in seconds: coyote time"]
  T --> X["Limits: tunneling, instability, non-reproducibility"]
  X --> FX["Fixed timestep: equal steps, zero to many per frame"]
```

## Glossary

- Game loop：the never-ending function a game runs, repeating read input, update state, draw until the player quits.
- Frame：one complete pass of that loop, producing one picture; 60 FPS means 60 passes in the last second.
- State：the set of numbers describing the world in memory — positions, velocities, health, timers — and the only thing carried from one frame to the next.
- Update step：the half of the loop that changes state without touching pixels; movement, gravity, timers and collision checks happen here.
- Draw (render)：the half of the loop that reads state and paints it, normally without changing anything.
- Delta time (dt)：how many seconds the previous frame took, used to turn a per-second rate into "how far this frame".
- Frame-rate independence：the same input producing the same speed and feel at 30 FPS and 144 FPS, achieved by multiplying every rate by delta time.
- Fixed timestep：a simulation that always advances in equal time chunks, such as 1/60 s, running zero, one or several times per drawn frame as accumulated real time allows.
- Coyote time：a short grace period after leaving a ledge during which a jump still counts, expressed in seconds so it feels the same at any frame rate.

## Sources

1. [Unity Scripting API: Time.fixedDeltaTime — the interval at which physics and FixedUpdate run (0.02 s by default)](https://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html)
2. [MDN: Anatomy of a video game — input, update, render, and using the requestAnimationFrame timestamp](https://developer.mozilla.org/en-US/docs/Games/Anatomy)
3. [Godot docs: Idle and Physics Processing — _process(delta) vs _physics_process at a fixed 60 Hz, and why speed is multiplied by delta](https://docs.godotengine.org/en/stable/tutorials/scripting/idle_and_physics_processing.html)
4. [Glenn Fiedler, "Fix Your Timestep!" — variable vs fixed vs semi-fixed timesteps and the accumulator loop](https://gafferongames.com/post/fix_your_timestep/)

---

Original article: https://eulore.ai/articles/game-loop-57422ff7

> **Eulore** · Start small. Go deep.
>
> Eulore is an AI learning tool built around connected articles, each a 5–10 minute read. Ask questions as you go and help shape what comes next. That’s how this article was created.
>
> Start your own series → https://eulore.ai
