Fixed Timestep and Interpolation
The integrators lesson ended with a rule: keep dt fixed. Display frames don't cooperate. A 144 Hz monitor delivers 6.9 ms frames, a laptop on battery delivers 33 ms frames, and every machine has hitches. This lesson shows how engines decouple the two clocks, why physics belongs in the fixed step, and how rendering hides the seams with interpolation.
Note: The Fixed-Step Loop in the canvas course and Fixed Timestep in the fundamentals course introduce the accumulator. Here we look at it from the physics side.
Variable dt makes physics depend on the machine
Run the same two seconds of a spring-and-gravity simulation on a "fast PC" (steady 144 Hz), a "slow PC" (30 Hz with a hitch) and with a fixed 60 Hz step fed by each of those frame sequences:
#include <iostream>
#include <iomanip>
#include <vector>
struct State { double y = 1.0, v = 0.0; };
void step(State& s, double dt) { // a damped spring hanging under gravity
double a = -40 * (s.y - 0.5) - 1.5 * s.v - 9.81;
s.v += a * dt;
s.y += s.v * dt;
}
std::vector<double> frames(double frameDt, double total, int hitchAt) {
std::vector<double> f;
double sum = 0;
for (int i = 0; sum < total - 1e-9; i++) {
double d = (i == hitchAt) ? 0.25 : frameDt;
if (sum + d > total) d = total - sum;
f.push_back(d);
sum += d;
}
return f;
}
int main() {
std::cout << std::fixed << std::setprecision(4);
const double STEP = 1.0 / 60.0;
struct Machine { const char* name; std::vector<double> f; };
std::vector<Machine> machines = {
{"fast PC (144 Hz)", frames(1.0 / 144, 2.0, -1)},
{"slow PC (30 Hz + hitch)", frames(1.0 / 30, 2.0, 20)},
};
for (auto& m : machines) {
State variable, fixed;
double acc = 0;
int updates = 0;
for (double d : m.f) {
step(variable, d); // physics with the frame's dt
acc += d;
while (acc >= STEP - 1e-12) { step(fixed, STEP); acc -= STEP; updates++; }
}
std::cout << m.name << "\n variable dt: y = " << variable.y
<< "\n fixed step: y = " << fixed.y << " after " << updates << " updates\n";
}
}fast PC (144 Hz)
variable dt: y = 0.4201
fixed step: y = 0.4183 after 120 updates
slow PC (30 Hz + hitch)
variable dt: y = 0.6543
fixed step: y = 0.4183 after 120 updates
With the frame's dt the two machines disagree, and the hitch (one 250 ms frame) throws the stiff spring far off course. With a fixed step, both machines run exactly the same 120 updates and land on the same number to every printed digit. The accumulator absorbs the uneven frames; the physics never sees them.
The loop engines use
const double STEP = 1.0 / 60.0;
double acc = 0;
while (running) {
double frame = clock.secondsSinceLastFrame();
acc += std::min(frame, 0.25); // clamp: survive breakpoints and long hitches
int steps = 0;
while (acc >= STEP && steps < MAX_STEPS) {
savePreviousState(); // for interpolation
physicsStep(STEP);
acc -= STEP;
steps++;
}
if (steps == MAX_STEPS) acc = 0; // drop time instead of spiralling
render(acc / STEP); // alpha in [0, 1)
}Each engine wraps this loop for you:
| Engine | Fixed callback | Default rate | Where to change it |
|---|---|---|---|
| Unity | FixedUpdate() |
50 Hz (Time.fixedDeltaTime = 0.02) |
Project Settings > Time |
| Godot | _physics_process(delta) |
60 Hz | Project Settings > Physics > Common > Physics Ticks per Second |
| Unreal | physics runs per frame, with optional substepping or async physics at a fixed rate | frame-dependent unless configured | Project Settings > Physics |
| Box2D / Rapier / Jolt (your own loop) | you call b2World_Step, world.step() or PhysicsSystem::Update |
whatever you pass | your code |
Everything that touches a body's velocity or forces belongs in the fixed callback. Reading input happens per frame (so presses aren't missed); store the intent and apply it in the next fixed step.
The spiral of death
If one physics step takes longer to compute than the time it simulates, every frame is slower than the last: more time accumulates, more steps run, the frame gets even longer. A cap on the steps per frame turns a freeze into slow motion:
#include <iostream>
#include <iomanip>
#include <algorithm>
int main() {
std::cout << std::fixed << std::setprecision(1);
const double STEP = 1000.0 / 60.0; // milliseconds
const double costPerStep = 30.0; // a heavy scene: each step costs 30 ms of CPU
for (int maxSteps : {1000, 3}) {
std::cout << (maxSteps == 1000 ? "no cap:\n" : "cap of 3 steps per frame:\n");
double acc = 0, frameMs = 16.7;
for (int frame = 1; frame <= 7; frame++) {
acc += std::min(frameMs, 1000.0);
int steps = 0;
while (acc >= STEP && steps < maxSteps) { acc -= STEP; steps++; }
if (steps == maxSteps) acc = 0;
frameMs = 2 + steps * costPerStep; // the next frame lasts as long as this one's work
std::cout << " frame " << frame << ": " << steps << " steps, next frame " << frameMs << " ms\n";
}
}
}no cap:
frame 1: 1 steps, next frame 32.0 ms
frame 2: 1 steps, next frame 32.0 ms
frame 3: 2 steps, next frame 62.0 ms
frame 4: 4 steps, next frame 122.0 ms
frame 5: 7 steps, next frame 212.0 ms
frame 6: 13 steps, next frame 392.0 ms
frame 7: 24 steps, next frame 722.0 ms
cap of 3 steps per frame:
frame 1: 1 steps, next frame 32.0 ms
frame 2: 1 steps, next frame 32.0 ms
frame 3: 2 steps, next frame 62.0 ms
frame 4: 3 steps, next frame 92.0 ms
frame 5: 3 steps, next frame 92.0 ms
frame 6: 3 steps, next frame 92.0 ms
frame 7: 3 steps, next frame 92.0 ms
Without a cap the step count keeps climbing; with a cap of three the game settles at a steady (slow) rate and recovers when the load drops. Unity's equivalent setting is Maximum Allowed Timestep (Time.maximumDeltaTime); Godot has Max Physics Steps per Frame.
Interpolation: smooth pictures from discrete steps
At 50 or 60 physics steps per second on a 144 Hz display, some frames show the same physics state twice and some skip ahead, so motion judders. The fix is to render each body between its previous and current physics state, using the leftover fraction alpha = acc / STEP:
#include <iostream>
#include <iomanip>
#include <algorithm>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double STEP = 1.0 / 20.0; // a coarse 20 Hz physics step, to make the effect visible
const double speed = 2.0; // m/s, constant
double prevX = 0, x = 0, acc = 0, clock = 0;
const double frames[] = {0.016, 0.017, 0.016, 0.017, 0.016, 0.017, 0.016, 0.017};
std::cout << "render time raw x interpolated x exact x\n";
for (double d : frames) {
clock += d;
acc += d;
while (acc >= STEP) { prevX = x; x += speed * STEP; acc -= STEP; }
double alpha = acc / STEP;
double shown = prevX + (x - prevX) * alpha;
std::cout << std::setw(11) << clock << std::setw(7) << x << std::setw(16) << shown
<< std::setw(9) << speed * std::max(0.0, clock - STEP) << "\n";
}
}render time raw x interpolated x exact x
0.016 0.000 0.000 0.000
0.033 0.000 0.000 0.000
0.049 0.000 0.000 0.000
0.066 0.100 0.032 0.032
0.082 0.100 0.064 0.064
0.099 0.100 0.098 0.098
0.115 0.200 0.130 0.130
0.132 0.200 0.164 0.164
The raw position stands still for two or three frames and then jumps 0.1 m. The interpolated one moves a little every frame and matches the exact motion one physics step in the past. That one-step delay is the price of interpolation; at 60 Hz it is 16.7 ms and nobody notices it. (The alternative, extrapolation, predicts forward instead and has no delay, but it overshoots whenever a body collides or changes direction.)
Engines do this for you if you ask:
- Unity: set
Rigidbody.interpolation = RigidbodyInterpolation.Interpolateon bodies the camera follows. - Godot: enable Physics Interpolation (Project Settings > Physics > Common) and call
reset_physics_interpolation()after teleporting a node, so it doesn't glide from its old spot. - Your own engine: keep
previousandcurrenttransforms per body; renderlerpof the positions andslerpof the rotations (see Slerp).
Tip: Move the camera in the same clock as its target. A camera updated per frame following a body that moves only in physics steps is the most common source of "my character stutters".
Test yourself
Two machines with different frame rates run the same fixed-step simulation for 2 seconds. What do they compute?
- Slightly different results
- Exactly the same states, because they run the same number of identical steps
- Results that differ only after a hitch
- Nothing; fixed steps need equal frame rates
What does alpha = acc / STEP represent when rendering?
- The frame rate
- How far the render moment is between the previous and the current physics state
- The physics error
- The number of steps left
Why does capping the number of physics steps per frame prevent the spiral of death?
- It makes each step cheaper
- It drops simulated time when the CPU can't keep up, so the game slows down instead of freezing
- It changes the time step
- It disables collision detection