Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Game Loop  ›  Capstone Project

The Fixed Timestep

Client Side Development 15 minAn accumulator collects real time each frame and physics runs in equal fixed steps drained from it, so the simulation behaves the same on every machine while rendering happens once per frame.

The Fixed Timestep

With delta time, the drone flies at the right speed on every machine. The physics still misbehaves, though: a crate dropped on a deck bounces a little higher on a slow phone than on a fast PC, and when a frame runs long, a fast-falling crate sometimes passes straight through a thin boat deck. Physics is sensitive to the size of its time steps. The fix is to stop feeding it whatever dt the frame happened to have.

The rule

Render every frame; simulate in equal steps from an accumulator.

Pick one fixed physics step — say 0.02 s, which is 50 steps a second. Each frame, pour the real time that has passed into an accumulator. While it holds at least one step's worth, run one physics step and take that step out. Whatever is left over waits for the next frame.

STEP = 0.02                        # 50 physics steps per second
accumulator = 0.0
while running:
    accumulator += frame_seconds() # real time since the last frame
    read_input()
    while accumulator >= STEP:
        physics_step(STEP)         # always exactly the same slice
        accumulator -= STEP
    render(alpha=accumulator / STEP)

Physics now always sees the same STEP, so the crate bounces identically on every machine, and replaying the same inputs gives the same result. Drawing still happens once per frame, as fast as the device allows.

The leftover isn't wasted. alpha = accumulator / STEP says how far real time has got between the last physics state and the next one, so the renderer can blend the two positions. Motion stays smooth even though frames and physics steps don't line up.

The shortcut

Work in milliseconds and it's just division with a remainder: steps this frame = how many whole steps fit in the accumulator; the remainder carries over.

Three frames with a 20 ms stepStep 1 of 4
  1. Frame A takes 16 ms

    Accumulator 0 + 16 = 16 ms. Less than 20, so no physics step runs. 16 ms waits.

The trap is the spiral of death. If computing one physics step takes longer than the game time it simulates, every frame falls further behind: the accumulator grows, more steps are needed, those take even longer, and the game grinds to a halt. Guard against it by capping the steps per frame (at most 5, say) or clamping the time added to the accumulator.

Drills

Answer fast — trust the rule.

quick drill

The step is 20 ms. The accumulator holds 7 ms and this frame took 48 ms. How many physics steps run?

Same frame: after those steps run, how many milliseconds stay in the accumulator?
7+48−2×20= ?7 + 48 - 2 \times 20 = \,?
quick drill

Why simulate physics in fixed steps instead of using each frame's dt?

quick drill

With 15 ms left over and a 20 ms step, what blend factor does the renderer use?

quick drill

Each physics step takes 30 ms of computer time to simulate 20 ms of game time. With no cap, what happens?

Remember

  • Render every frame; run physics in equal steps drained from an accumulator.
  • Steps = whole steps in the accumulator; the remainder carries over and sets the blend factor.
  • Cap the steps per frame to avoid the spiral of death.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne long frame, step by step

Harbour Hopper runs physics every 20 ms. The accumulator holds 5 ms at the start of this frame, and the frame took 55 ms because a boat model was loading. Explain step by step what the loop does, what gets rendered, and why the physics results match those on a faster machine.