Smoothing That Doesn't Depend on Frame Rate
Closing a fixed fraction of the gap every frame makes the motion depend on how many frames get drawn. Close it the way a continuous decay would instead, multiplying what's left by each frame, and every frame rate traces the same curve. A half-life , with , is the friendly way to set the speed.
What the symbols mean
- Smoothed value
The thing that chases: a camera's position, a bar's displayed width.
- Target
Where x is headed: the player the camera follows, the health the bar should show.
- Per-frame fraction
The share of the remaining gap closed in one frame.
What the symbols mean
- Rate
How fast the gap shrinks, per second. Bigger is snappier.
- Starting value
Where x is at time 0.
What the symbols mean
- Frame time
Seconds since the last frame: 1/30, 1/144, or whatever it really was.
What the symbols mean
- Half-life
The time for the remaining gap to halve.
Explanation
Smoothing, in game code, simply means letting a value chase a target instead of jumping to it: a camera trailing a player, a health bar's "damage" trail catching up to the real health, a turret easing onto its aim. The classic one-liner is x += (target - x) * k every frame, which closes a fraction of whatever gap is left. Frame-rate independent means the result after, say, half a second is the same whether the game drew frames in that time or . The one-liner fails that test, and the fix is a single exponential. The exact step looks heavier than it is: is the gap still left, is the fraction of that gap that survives this frame, and adding back turns the gap into a position again. Bear with the exponential; it's the only new idea here.
Why per-frame smoothing depends on frame rate. Each frame keeps of the gap, so after frames the gap is multiplied by . In one second at frames per second, that's . With :
- at fps, : of the gap is left after a second;
- at fps, : it arrived long ago.
Same code, same , and the player with the fast monitor gets a far snappier camera. Worse, when the frame rate stutters, the motion stutters with it. There's an animation further down with the two frame rates side by side.
The continuous picture. What we really want is a rule about time, not frames: "the gap shrinks at a rate proportional to its size." That's the differential equation
Call the gap . With held still, , which separates into (it's also the simplest linear first-order equation). So : the gap decays exponentially, multiplied by every second however the second is chopped up.
The exact step. Over one frame lasting seconds, then, the gap gets multiplied by :
Think about why this can't depend on the frame rate. Two frames of and multiply the gap by , exactly what one long frame covering the same time would do, because exponents add. counts frames; counts seconds. Written as a lerp, the step is , so the old one-liner had the right shape all along. The only fix is that must be , computed from this frame's .
// Moves x toward target; lambda is per second, dt is this frame's length in seconds.
function damp(x: number, target: number, lambda: number, dt: number): number {
return lerp(x, target, 1 - Math.exp(-lambda * dt));
}
Matching a game tuned with . If a game was tuned with some at fps, match its feel by asking for the same keep-factor per sixtieth of a second: , so . For , per second.
Half-life: a number a designer can feel. " per second" is hard to picture. The half-life is the time for the gap to halve: , so . A camera with s closes half the gap in s, three quarters in s and seven eighths in s, and in general the gap left after time is of the original, the same arithmetic as radioactive decay.
A common mistake. Multiplying by the frame time: x += (target - x) * lambda * dt. It's close for small steps, since (the first terms of the Taylor series). But it's really Euler's method applied to , and it fails the same way Euler's method does on big steps. Each frame multiplies the gap by : once that's negative and the value overshoots the target, and once the overshoots grow and it blows up. A single slow frame, say a ms hitch while something loads, is enough. The exponential can't do that: is always between and , so the exact step never overshoots for any .
Another one. Converting a fps to fps by doubling it. One fps frame should keep the gap twice over, , so the right per-frame fraction is : for that's , not . Close, but for bigger doubling is badly wrong ( doubled is , an overshoot every frame).
Moving targets. The exact step assumes holds still during the frame. When the target keeps moving, the follower lags behind it; at a steady speed , it settles a distance behind (the last practice problem works that out). A shorter half-life follows more tightly. In Pointing at Things the same exponential smooths rotations, and SmoothDamp replaces it with a spring when you also want the speed to change smoothly.
See it
Worked examples
A camera uses every frame, starting m from its target. How much of the gap is left after one second at fps, and at fps? Then find the rate that reproduces the fps behavior, and the per-frame fraction to use at fps.
- 1
Each frame closes of the gap, so it keeps : the gap is multiplied by per frame.
A health bar's damage trail chases the real health with a half-life of s. It's px behind. How far behind is it s later, and what lerp fraction does one frame at fps use?
- 1
Rate from the half-life: per second.
Practice
Type your answer and check it. Solve them all to complete the lesson.
Per-frame smoothing runs at fps toward a fixed target. What fraction of the starting gap remains after s?
A game was tuned with per frame at fps. What rate , per second, gives the same smoothing with the exact step ?
A follow camera should close half of its remaining gap every s. What rate does that take, per second?
A damage trail on a health bar catches up with a half-life of s. It starts px behind the real health, which then stays put. How many px behind is it after s?
Solve with .
A programmer smooths with x += (G − x)·λ·Δt, where per second and is the frame time. Below what frame rate, in fps, does a single frame carry past the target?
A camera was tuned with per frame at fps. To get exactly the same motion at fps, what per-frame fraction should it use?
The hardest one here. Get it, and you've got this lesson.
A camera follows a player using the exact smoothing step with a half-life of s, so between frames it obeys . The player runs at a steady m/s, so . Once things settle, how far behind the player does the camera ride, in meters?
Where it's used
Exponential smoothing with a half-life keeps follow cameras, health-bar damage trails, cursor-following tooltips and turret aim steady on everything from a Hz phone to a Hz monitor. Audio engines ramp volume and filter settings toward new values the same way so changes don't click, and online games use it to blend away the small position corrections that arrive from the server. It's the same formula engineers know as a first-order low-pass filter: a capacitor charging through a resistor follows , with .