Fixed Timestep & the Accumulator
Delta time keeps movement speed right, but the result of a simulation still depends on how the time was sliced. Physics engines, replays, lockstep multiplayer and anything that must behave the same on every machine need the simulation to advance in equal steps. The standard technique is a fixed timestep with an accumulator, made famous by Glenn Fiedler's article "Fix Your Timestep!".
The problem: same input, different results
Simulate a jump with simple physics (velocity += gravity * dt; height += velocity * dt) on two machines. Both run for the same 0.6 seconds, but one slices the time into small frames and the other into large ones:
using System;
double JumpPeak(double dt, int frames)
{
double height = 0, velocity = 10, peak = 0; // jump at 10 units/s, gravity -30
for (int i = 0; i < frames; i++)
{
velocity += -30 * dt;
height += velocity * dt;
peak = Math.Max(peak, height);
}
return peak;
}
Console.WriteLine(quot;144 FPS: peak {JumpPeak(1.0 / 144, 86):F3}");
Console.WriteLine(quot; 60 FPS: peak {JumpPeak(1.0 / 60, 36):F3}");
Console.WriteLine(quot; 20 FPS: peak {JumpPeak(1.0 / 20, 12):F3}");144 FPS: peak 1.632
60 FPS: peak 1.583
20 FPS: peak 1.425
The exact answer is 10² / (2 · 30) ≈ 1.667. Low frame rates jump lower, so a gap that is easy at 144 FPS might be impossible at 20 FPS. In a multiplayer game, two clients would disagree about where everyone is.
The accumulator
Decouple the simulation from the frame rate:
- Choose a fixed step, for example 20 ms (50 updates per second; Unity's default
FixedUpdaterate). - Each frame, add the real elapsed time to an accumulator.
- While the accumulator holds at least one step, run one fixed update and subtract the step.
- Render. What's left in the accumulator (less than one step) tells you how far you are between two simulation states.
accumulator += frameTime
while accumulator >= step:
update(step)
accumulator -= step
alpha = accumulator / step // 0..1, used to interpolate rendering
render(alpha)Here is a trace with uneven frame times. Times are integers in milliseconds, so the output is exact:
using System;
int[] frameTimesMs = { 16, 16, 17, 33, 8, 50, 16, 7 };
const int stepMs = 20;
int accumulator = 0, ticks = 0;
foreach (int frameMs in frameTimesMs)
{
accumulator += frameMs;
int stepsThisFrame = 0;
while (accumulator >= stepMs)
{
ticks++; // one fixed update: physics, gameplay rules
stepsThisFrame++;
accumulator -= stepMs;
}
double alpha = (double)accumulator / stepMs;
Console.WriteLine(quot;frame {frameMs,2} ms -> {stepsThisFrame} step(s), total ticks {ticks}, leftover {accumulator,2} ms, alpha {alpha:F2}");
}
Console.WriteLine(quot;simulated {ticks * stepMs} ms of {Sum(frameTimesMs)} ms");
static int Sum(int[] values) { int s = 0; foreach (int v in values) s += v; return s; }frame 16 ms -> 0 step(s), total ticks 0, leftover 16 ms, alpha 0.80
frame 16 ms -> 1 step(s), total ticks 1, leftover 12 ms, alpha 0.60
frame 17 ms -> 1 step(s), total ticks 2, leftover 9 ms, alpha 0.45
frame 33 ms -> 2 step(s), total ticks 4, leftover 2 ms, alpha 0.10
frame 8 ms -> 0 step(s), total ticks 4, leftover 10 ms, alpha 0.50
frame 50 ms -> 3 step(s), total ticks 7, leftover 0 ms, alpha 0.00
frame 16 ms -> 0 step(s), total ticks 7, leftover 16 ms, alpha 0.80
frame 7 ms -> 1 step(s), total ticks 8, leftover 3 ms, alpha 0.15
simulated 160 ms of 163 ms
Some frames run no simulation step, some run several. Over time the simulation keeps pace with real time, and every step is exactly 20 ms.
Same result on every machine
Now run the jump inside the accumulator loop. Two machines with very different frame times reach exactly the same state, because both execute the same sequence of 20 ms steps:
using System;
const double Step = 0.02;
string Simulate(int[] frameTimesMs)
{
double height = 0, velocity = 10, peak = 0;
int accumulatorMs = 0, ticks = 0;
foreach (int ms in frameTimesMs)
{
accumulatorMs += ms;
while (accumulatorMs >= 20)
{
velocity += -30 * Step;
height += velocity * Step;
peak = Math.Max(peak, height);
accumulatorMs -= 20;
ticks++;
}
}
return quot;{ticks} ticks, peak {peak:F4}, height {height:F4}";
}
var fast = new int[60]; Array.Fill(fast, 7); // ~144 FPS for 420 ms
var slow = new int[6]; Array.Fill(slow, 70); // ~14 FPS for 420 ms
Console.WriteLine(quot;fast PC: {Simulate(fast)}");
Console.WriteLine(quot;slow PC: {Simulate(slow)}");fast PC: 21 ticks, peak 1.5680, height 1.4280
slow PC: 21 ticks, peak 1.5680, height 1.4280
This is why Unity has FixedUpdate, Godot has _physics_process and Unreal has physics substepping: physics runs on a fixed clock, separate from rendering.
Interpolating the render
If the simulation runs at 50 Hz and the screen at 144 Hz, drawing the latest physics state shows visible stutter: some frames draw the same position twice. The fix is to keep the previous and current state and draw a blend using alpha:
using System;
double previousX = 0, currentX = 0;
const double speed = 100; // units per second
const double step = 0.02;
double accumulator = 0;
double[] frames = { 0.007, 0.007, 0.007, 0.007, 0.007, 0.007 };
foreach (double frameTime in frames)
{
accumulator += frameTime;
while (accumulator >= step)
{
previousX = currentX; // remember the last state
currentX += speed * step; // advance one fixed step
accumulator -= step;
}
double alpha = accumulator / step;
double drawnX = previousX + (currentX - previousX) * alpha;
Console.WriteLine(quot;physics x={currentX,4:F1} drawn x={drawnX,4:F2}");
}physics x= 0.0 drawn x=0.00
physics x= 0.0 drawn x=0.00
physics x= 2.0 drawn x=0.10
physics x= 2.0 drawn x=0.80
physics x= 2.0 drawn x=1.50
physics x= 4.0 drawn x=2.20
The physics position jumps in steps of 2, but the drawn position moves smoothly, 0.7 units per 7 ms frame. The cost is that rendering shows the world up to one step (20 ms) in the past. Unity's Rigidbody Interpolate setting does this for you.
Note: The first two frames draw 0.00 because there is no previous state to blend from yet; the simulation hasn't taken its first step.
The spiral of death
If one fixed update takes longer to compute than the step it simulates, each frame accumulates more time than it can process, the next frame is even slower, and the game freezes. Every robust loop caps the work:
using System;
const int stepMs = 20, maxStepsPerFrame = 5;
int accumulator = 0;
int[] frameTimes = { 16, 400, 16 }; // a 400 ms hitch (alt-tab, loading, a breakpoint)
foreach (int ms in frameTimes)
{
accumulator += ms;
int steps = 0;
while (accumulator >= stepMs && steps < maxStepsPerFrame)
{
accumulator -= stepMs;
steps++;
}
if (steps == maxStepsPerFrame && accumulator >= stepMs)
{
Console.WriteLine(quot; dropping {accumulator} ms of backlog");
accumulator %= stepMs; // give up on the lost time
}
Console.WriteLine(quot;frame {ms,3} ms: {steps} steps, leftover {accumulator} ms");
}frame 16 ms: 0 steps, leftover 16 ms
dropping 316 ms of backlog
frame 400 ms: 5 steps, leftover 16 ms
frame 16 ms: 1 steps, leftover 12 ms
The game slows down briefly instead of locking up. Unity's equivalent setting is Maximum Allowed Timestep; browsers simply stop calling requestAnimationFrame in hidden tabs, which produces exactly this kind of huge frame when the player comes back.
Which code goes where
Fixed step (FixedUpdate, _physics_process) |
Variable step (Update, _process, Tick) |
|---|---|
| physics forces and velocities | reading input |
| movement that collides | camera, UI, animation blending |
| deterministic game rules (lockstep, replays) | visual effects, interpolation |
Read input every frame (so presses aren't missed) and consume it in the next fixed step.
Test yourself
The fixed step is 20 ms and the accumulator holds 13 ms. A 50 ms frame arrives. How many fixed updates run?
- 2
- 3
- 4
- 1
What is alpha = accumulator / step used for?
- Deciding the physics step
- Interpolating between the previous and current state when rendering
- Measuring FPS
- Clamping deltaTime
What is the 'spiral of death'?
- A physics bug with spinning objects
- Simulation steps take longer than real time, so the backlog grows every frame
- A shader effect
- A memory leak in the renderer