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.
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.
The step is 20 ms. The accumulator holds 7 ms and this frame took 48 ms. How many physics steps run?
Why simulate physics in fixed steps instead of using each frame's dt?
With 15 ms left over and a 20 ms step, what blend factor does the renderer use?
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.
