skip to content

Why does lerp(value, target, 0.1) run every frame behave differently at 30 and 144 frames per second, and how do you make it frame-rate independent?

level: middleimportance: must knowfreq 60%

answer

  1. frames are not seconds
  2. a fixed fraction per call
  3. the gap shrinks geometrically
  4. exponent in elapsed time
  5. 1 - exp(-k * dt)

basics

~20 s

Each frame keeps 90% of the remaining gap, so after one second 0.9^30 (about 4%) remains at 30 fps but almost nothing at 144 fps. A blend factor of 1 - exp(-k * dt) makes the decay depend only on elapsed time.

solid answer

~50 s

The call closes a fixed fraction of the gap per call, so for a fixed target the gap after `n` frames is `0.9^n` of the start. Frames are not a unit of time: in one second the 30 fps player sees about 4.2% of the gap left and the 144 fps player about 0.00003%, and the 90% point arrives after 0.73 s versus 0.15 s. The fix makes the kept fraction a power of elapsed time: `value = lerp(value, target, 1 - exp(-k * dt))`. Because `exp(-k * dt1) * exp(-k * dt2) = exp(-k * (dt1 + dt2))`, any split of a second into frames, even an irregular one, leaves the same `exp(-k)` of the gap. To keep a feel tuned at 60 fps, solve `exp(-k / 60) = 0.9`, giving `k = -60 * ln 0.9`, about 6.32 per second.

code

pseudocode · 10 lines
pseudocode
// k: rate per second, dt: seconds since last frame
function smoothToward(current, target, k, dt):
    alpha = 1 - exp(-k * dt)
    return current + (target - current) * alpha

// tuned to match lerp(value, target, 0.1) at 60 fps
k = -60 * ln(0.9)          // about 6.32 per second
value = smoothToward(value, target, k, dt)
if abs(target - value) < snapThreshold:
    value = target

go deeper

for a junior

Know that blending by 0.1 per frame multiplies the remaining gap by 0.9 each frame, so more frames per second means faster convergence.

for a middle

Derive 1 - exp(-k * dt) from the product of per-frame kept fractions, compute k from a per-frame factor at a reference rate, and explain why scaling by dt is only approximate.

for a senior

Diagnose the bug from its symptom, cover hitches, snap thresholds and steady-state lag for moving targets, and know when a critically damped follow should replace it.

for a principal

Standardise one time-based smoothing helper with half-life tuning so every system behaves the same on every frame rate and platform.

## What the per-frame lerp really does The line `value = lerp(value, target, 0.1)` run once per frame moves the value 10% of the way to the target each time. For a fixed target the remaining gap is multiplied by 0.9 every frame, so after `n` frames it is `0.9^n` of the starting gap. That is a **geometric decay per frame**, and frames are not a unit of time. A player at 144 frames per second runs the line 4.8 times as often as one at 30, so the value covers any given share of the gap 4.8 times sooner in wall-clock time. A UI value glides on one machine and snaps on another; a follow behaviour tuned at 60 frames per second feels sluggish on hardware that only reaches 30. ## Putting numbers on it Frames needed to close 90% of the gap: solve `0.9^n = 0.1`, so `n = ln 0.1 / ln 0.9`, about 21.9 frames. | Frame rate | Gap left after 1 s | Time to close 90% | |---|---|---| | 30 fps | `0.9^30`, about 4.2% | about 0.73 s | | 60 fps | `0.9^60`, about 0.18% | about 0.36 s | | 144 fps | `0.9^144`, about 0.00003% | about 0.15 s | | exponential, `k` about 6.32 per second, any rate | `exp(-6.32)`, about 0.18% | about 0.36 s | The last row is the fix, tuned to reproduce the 60 fps feel at every frame rate. ## The exponential fix The goal is a gap that shrinks by a factor depending on **elapsed time only**. The exponential is the function with that property: 1. choose a rate `k` per second and require the gap after time `T` to be `exp(-k * T)` of the start; 2. a frame lasting `dt` must therefore keep `exp(-k * dt)` of the gap, which means blending by `alpha = 1 - exp(-k * dt)`; 3. over frames lasting `dt1, dt2, ...` the kept fractions multiply, `exp(-k * dt1) * exp(-k * dt2) = exp(-k * (dt1 + dt2))`, so every way of splitting the same time into frames leaves the same gap. In code: `value = lerp(value, target, 1 - exp(-k * dt))`. For a fixed target this is the exact solution of "approach the target at a rate proportional to the remaining gap", the equation `dx/dt = k * (target - x)`, written in closed form for each frame rather than stepped, so the frame length drops out. To keep a feel tuned at a reference rate, solve for `k`: keeping 90% per frame at 60 fps means `exp(-k / 60) = 0.9`, so `k = -60 * ln 0.9`, about 6.32 per second. ## Picking k: three ways to say the same thing Designers rarely think in rates per second, so expose whichever parameter reads best: - **rate `k`**: `alpha = 1 - exp(-k * dt)`; larger is snappier; - **half-life `h`**: the time to close half the gap, `alpha = 1 - 2^(-dt / h)`, with `k = ln 2 / h` (for `k` of 6.32, `h` is about 0.11 s); - **fraction left after one second `r`**: `alpha = 1 - r^dt`, with `k = -ln r`. All three give identical motion for matching values; half-life is usually the easiest to tune by eye. ## Approximations that look right and are not - **Scaling the factor by `dt`**, as in `lerp(value, target, 6 * dt)`, is the first-order approximation of `1 - exp(-6 * dt)`. It agrees for small `dt` but still drifts with frame rate (about 0.12% of the gap left after a second at 30 fps against 0.22% at 144 fps), and on a 0.25 s hitch it gives a factor of 1.5 that overshoots the target by half the gap. Clamping the factor to 1 hides the overshoot, not the drift. - **Capping the frame rate** makes the behaviour consistent only on machines that reach the cap. - **Running the smoothing at a fixed rate** removes the dependence but can make the value move in visible steps when that rate is below the display rate, unless the display is smoothed separately. ## Limits of the fix - The exponential never reaches the target exactly; snap to it once the gap falls below a threshold in the value's own units. - It is exact only while the target holds still. For a target moving at a steady speed `v`, the value trails by about `v / k`, and a small frame-rate dependence remains in that lag. - The value's velocity jumps whenever the target jumps. When velocity continuity matters, as for a follow camera, a critically damped follow that carries its own velocity is the usual next step. - This is smoothing toward a target. Interpolating rendered state between two fixed simulation steps, and integrating physical motion over time, are separate subjects with their own techniques.

  • Why does lerp(value, target, 6 * dt) still drift between frame rates and misbehave on a hitch?
    It is the first-order approximation of `1 - exp(-6 * dt)`. The gap left after a second becomes `(1 - 6 * dt)^(1 / dt)`, which depends on `dt`: about 0.12% at 30 fps against 0.22% at 144 fps, versus 0.25% for the exact form. On a 0.25 s hitch the factor is 1.5, so the value overshoots by half the gap; the exponential gives about 0.78 and never passes the target.
  • The smoothed value never quite equals the target; what do you do about it?
    Snap it once the remaining gap is below a threshold chosen in the value's own units, a fraction of a pixel or a millimetre, so the exponential tail does not keep the value dirty forever. If the motion must finish at a known time, a fixed-duration eased tween is the better tool, since exponential smoothing has no end time by design.
  • When is exponential smoothing the wrong tool for following a target?
    When the target moves steadily, the value lags by about `v / k` and never catches up; raising `k` shrinks the lag but makes reactions twitchier. When the target jumps, the value's velocity jumps too. A critically damped follow, which tracks a velocity as well as a position and reaches the target as fast as possible without overshooting, handles both better, at the cost of a little more state.

It is like interest paid per statement: a bank paying 10% per statement pays far more in a year if it sends 144 statements than if it sends 30. Continuous compounding, growth by exp(rate * time), gives the same yearly result however often you check the balance.

  • lerp(value, target, 0.1) reaches the target after ten frames.
  • Higher frame rates make this smoothing slower because each step is smaller.
  • Multiplying the 0.1 by dt makes it exactly frame-rate independent.
  • The exponential form only works with a fixed timestep.
  • Capping the frame rate fixes the smoothing difference everywhere.