How does an accumulator run a fixed simulation step under a variable frame rate, and why interpolate rendered state with the leftover fraction?
answer
- bank real time, spend fixed steps
- zero, one or several steps per frame
- keep previous and current state
- alpha = leftover / step
- render lags up to one step
basics
~20 sAn accumulator banks each frame's real elapsed time and runs whole fixed steps while it holds at least one. The leftover fraction, divided by the step, blends the previous and current simulation states so motion looks smooth at any display rate.
solid answer
~40 sEach frame the loop adds the measured, clamped frame time to an `accumulator`, then runs `while accumulator >= step: previous = current; current = simulate(current, step); accumulator -= step`. So a frame runs zero, one or several identical steps, and simulated time tracks real time. What remains is less than one step, and `alpha = accumulator / step` says how far real time has moved past the last completed step. Rendering `current` directly stutters, because at 144 frames per second with a 60 Hz step the world visibly moves on only 5 of every 12 frames. Rendering `lerp(previous, current, alpha)` gives evenly spaced positions instead, at the cost of drawing the world up to one step behind the newest simulated state. Teleports must reset `previous`, and rotations need a rotation-aware blend.
code
pseudocode · 19 linesSTEP = 1.0 / 60.0
MAX_FRAME = 0.25
accumulator = 0.0
lastTime = monotonicClockSeconds()
while running:
now = monotonicClockSeconds()
frameTime = min(now - lastTime, MAX_FRAME)
lastTime = now
accumulator = accumulator + frameTime
processInput()
while accumulator >= STEP:
previousState = currentState
currentState = simulate(currentState, STEP)
accumulator = accumulator - STEP
alpha = accumulator / STEP // 0 <= alpha < 1
render(blend(previousState, currentState, alpha))go deeper
Recall that an accumulator collects real time and runs whole fixed steps, and that the leftover fraction is used to blend two states for drawing.
Write the loop from memory, including saving the previous state before each step and computing alpha after the loop.
Explain the judder interpolation removes, its one-step latency cost, extrapolation as the alternative, and the teleport and rotation details that break naive versions.
Weigh the step rate against the interpolation latency and CPU cost, and decide which systems may bypass the fixed step to stay responsive.
## The problem the accumulator solves A **fixed timestep** advances the simulation by the same `step` every time, for example 1/60 s, so results do not depend on frame rate. But frames are not 1/60 s long: a display might refresh at 144 Hz, or a slow machine might take 40 ms per frame. The loop needs a way to run the right number of fixed steps so that **simulated time keeps pace with real time**, without ever taking a step of a different length. ## The accumulator pattern The **accumulator** is a running balance of real time that has passed but has not yet been simulated: 1. Measure the frame's elapsed time with a monotonic clock. 2. **Clamp** it to a maximum (a quarter of a second is a common illustrative choice) so one huge frame cannot demand hundreds of steps. 3. Add it to the accumulator. 4. While the accumulator holds at least one `step`, save the current state as `previous`, simulate one step, and subtract `step`. 5. Compute `alpha = accumulator / step`, which lies in the range 0 to just under 1. 6. Render a blend of `previous` and `current` weighted by `alpha`. The number of steps per frame follows from the rates: | Display rate | Step rate | Steps per frame | |---|---|---| | 144 Hz | 60 Hz | 0 or 1 (2.4 frames per step on average) | | 60 Hz | 60 Hz | usually 1, occasionally 0 or 2 from clock jitter | | 30 Hz | 60 Hz | usually 2 | No time is lost: the remainder carries into the next frame, so over many frames the step count matches elapsed time exactly. ## Why render without interpolation stutters If the renderer simply draws `current`, the world only moves on frames where a step ran. At 144 Hz with a 60 Hz step, 12 frames contain 5 steps, so positions update on 5 frames and stand still on 7, in an uneven pattern. An object moving at constant speed appears to **judder**: the image is not wrong, but the spacing between drawn positions is uneven. The same happens when a fixed 60 Hz step meets a 60 Hz display whose frame times wobble slightly, producing occasional double steps and empty frames. ## Render-state interpolation The fix keeps **two snapshots** of whatever the renderer needs, typically each object's transform: the state before the last step and the state after it. The renderer draws: ```pseudocode renderPosition = previous.position * (1 - alpha) + current.position * alpha ``` Because `alpha` advances in proportion to real time, drawn positions are evenly spaced on every frame. Key consequences: - **Latency** - the displayed world lies between the last two completed steps, behind the newest simulated state by up to one step, so interpolation adds up to one step of visual delay (up to 16.7 ms at a 60 Hz step). - **Teleports and spawns** must copy `current` into `previous`, or the object visibly slides from its old location. - **Rotations** need a rotation-aware blend rather than a component-wise one, a subject of rotation interpolation. - **Only render state** is interpolated; gameplay logic always reads the authoritative `current` state. ## Extrapolation as the alternative Instead of blending toward the past, a loop can **extrapolate**: draw `current.position + current.velocity * alpha * step`. That removes the step of latency but guesses wrong whenever something changes during the remainder, such as a collision or a sudden stop, causing visible pops and objects briefly drawn inside walls. Most games choose interpolation for local simulation; extrapolation appears where latency matters more than occasional error. ## Common mistakes - Subtracting the frame time rather than `step` inside the loop, which reintroduces a variable step. - Forgetting to save `previous` before each step, so interpolation blends the wrong pair when several steps run in one frame. - Interpolating with `alpha` computed before the step loop rather than after. - Omitting the clamp, which opens the door to the spiral of death after a hitch.
- How much input-to-display latency does render interpolation add, and how would you reduce it?Up to one simulation step, because the drawn state lies between the last two completed steps. At a 60 Hz step that is up to about 16.7 ms. Reducing it means a higher step rate, extrapolating instead for some objects, or simulating latency-critical things such as the local camera on the variable frame time while gameplay stays on the fixed step.
- Several steps run in one frame after a slow frame. Which two states should the renderer blend?The state before the last step and the state after it. Saving `previous` immediately before every step, not once per frame, guarantees that, so `alpha` blends across exactly one step. Saving it once per frame would blend across several steps with a weight meant for one, drawing objects in the wrong place.
- Why must a teleport or spawn update the previous state as well as the current one?Interpolation draws a blend between the two snapshots. If only `current` moves, the next frames draw the object partway between its old and new positions, so it visibly slides across the level for one step. Copying the new transform into both snapshots makes it appear in place.
- Subtracting the measured frame time inside the step loop instead of the fixed step
- Rendering the newest simulation state directly and calling the resulting judder a display problem
- Believing interpolation shows the future and therefore adds no latency
- Interpolating gameplay logic instead of only the state the renderer reads
- Discarding the leftover time each frame so simulated time slowly drifts from real time