flint

Manual 02 · time

The Fixed Timestep

Why the world advances in whole ticks, how the accumulator drains real time, and how rendering interpolates the remainder.

A physics step is only reproducible if it always sees the same dt. flint fixes the timestep at world construction — the default is 1/60 s — and never scales it by the frame's wall-clock duration. Variable steps make integration and contact resolution frame-rate dependent, and frame-rate dependence is the enemy of determinism.

The accumulator

Your render loop runs at whatever rate the display allows. To reconcile a wobbly render clock with a fixed simulation clock, drain elapsed real time in fixed slices:

const DT: f32 = 1.0 / 60.0;
let mut accumulator = 0.0;

fn frame(world: &mut World, real_elapsed: f32, accumulator: &mut f32) {
    *accumulator += real_elapsed.min(0.25); // clamp to escape the spiral of death
    while *accumulator >= DT {
        world.step();
        *accumulator -= DT;
    }
    let alpha = *accumulator / DT; // 0..1, how far into the next tick we are
    render(world, alpha);
}
A render frame consuming three fixed ticks, leaving a partial remainder in the accumulator tick tick tick remainder α
One render frame consumed three whole ticks; the leftover remainder becomes the interpolation factor α for the next draw.

Interpolation, not extrapolation

The alpha remainder is used only for rendering. Interpolate each body's drawn transform between its previous and current simulated states:

let draw_pos = prev.position.lerp(curr.position, alpha);

This smooths motion on high-refresh displays without ever letting the renderer influence the simulation. The simulation state stays discrete and exact; only the pixels are continuous.

Clamp accumulated time before the loop. If a frame stalls — a GC pause, a breakpoint — an unclamped accumulator asks flint to run hundreds of catch-up steps, each of which makes the next frame later. That feedback loop is the "spiral of death," and a single .min(0.25) prevents it.