Frame-Rate-Independent Damping
Here is a line found in countless games, tutorials and game-jam projects:
position = lerp(position, target, 0.1) // every frameIt moves 10% of the remaining distance each frame, so the object glides toward the target and slows down as it arrives. It feels great on the developer's machine. On a machine running at twice the frame rate it moves twice as fast, and on a slow machine it crawls. This lesson shows why, and the one-line fix.
The problem
After n frames, the remaining distance is (1 - 0.1)ⁿ of the original. In one second, that is 0.9^30 at 30 FPS but 0.9^144 at 144 FPS: completely different results.
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(4);
for (int fps : {30, 60, 144}) {
double x = 0, target = 1;
for (int frame = 0; frame < fps; frame++) // simulate one second
x = x + (target - x) * 0.1; // lerp with a constant factor
std::cout << fps << " FPS: after 1 s, x = " << x << "\n";
}
}30 FPS: after 1 s, x = 0.9576
60 FPS: after 1 s, x = 0.9982
144 FPS: after 1 s, x = 1.0000
The fix: exponential decay with dt
What we actually want is "the remaining distance shrinks by the same fraction per second", whatever the frame rate. That is exponential decay, and its per-frame factor depends on dt:
t = 1 - exp(-speed * dt)
position = lerp(position, target, t)Two frames of dt/2 now give exactly the same result as one frame of dt, because exp(-a) * exp(-b) = exp(-(a + b)).
#include <iostream>
#include <iomanip>
#include <cmath>
double damp(double current, double target, double speed, double dt) {
return current + (target - current) * (1 - std::exp(-speed * dt));
}
int main() {
std::cout << std::fixed << std::setprecision(4);
const double speed = 5; // larger = snappier
for (int fps : {30, 60, 144}) {
double x = 0, dt = 1.0 / fps;
for (int frame = 0; frame < fps; frame++) x = damp(x, 1, speed, dt);
std::cout << fps << " FPS: after 1 s, x = " << x << "\n";
}
std::cout << "exact: 1 - exp(-5) = " << 1 - std::exp(-5.0) << "\n";
}30 FPS: after 1 s, x = 0.9933
60 FPS: after 1 s, x = 0.9933
144 FPS: after 1 s, x = 0.9933
exact: 1 - exp(-5) = 0.9933
All three frame rates agree to four decimals (and the exact answer, 1 - e^-5, is what they all converge to).
Tuning with a half-life
speed is hard to reason about. A half-life is friendlier: "the camera closes half the remaining gap every 0.1 seconds".
t = 1 - 2^(-dt / halfLife) (equivalently speed = ln 2 / halfLife)#include <iostream>
#include <iomanip>
#include <cmath>
double dampHalfLife(double current, double target, double halfLife, double dt) {
return current + (target - current) * (1 - std::exp2(-dt / halfLife));
}
int main() {
std::cout << std::fixed << std::setprecision(4);
double x = 0;
const double dt = 1.0 / 60, halfLife = 0.1;
for (int frame = 1; frame <= 24; frame++) {
x = dampHalfLife(x, 10, halfLife, dt);
if (frame % 6 == 0) std::cout << "t=" << frame * dt << " s: x = " << x << " (gap " << 10 - x << ")\n";
}
}t=0.1000 s: x = 5.0000 (gap 5.0000)
t=0.2000 s: x = 7.5000 (gap 2.5000)
t=0.3000 s: x = 8.7500 (gap 1.2500)
t=0.4000 s: x = 9.3750 (gap 0.6250)
Every 0.1 s (6 frames at 60 FPS) the gap halves: 5, 2.5, 1.25, 0.625.
See the difference
The canvas simulates the same follow behavior at 20 FPS (top two dots) and 144 FPS (bottom two dots) side by side, while the target jumps back and forth. With the constant factor, the 144 FPS dot races ahead of the 20 FPS one. With exponential damping, both arrive together.
<canvas id="c" width="380" height="220" style="background:#14161c;display:block;max-width:100%"></canvas>
<script>
const ctx = document.getElementById('c').getContext('2d');
const rows = [
{ label: 'lerp 0.1/frame @20fps', fps: 20, exp: false, y: 40, color: '#ef5350' },
{ label: 'lerp 0.1/frame @144fps', fps: 144, exp: false, y: 80, color: '#ff9800' },
{ label: 'damp(speed 6) @20fps', fps: 20, exp: true, y: 140, color: '#42a5f5' },
{ label: 'damp(speed 6) @144fps', fps: 144, exp: true, y: 180, color: '#66bb6a' },
].map((r) => ({ ...r, x: 30, acc: 0 }));
let target = 350, timer = 0, last = null;
function step(now) {
const real = last === null ? 0 : Math.min((now - last) / 1000, 0.1);
last = now;
timer += real;
if (timer > 1.6) { timer = 0; target = target > 200 ? 30 : 350; }
for (const r of rows) {
const dt = 1 / r.fps;
r.acc += real;
while (r.acc >= dt) { // run each simulation at its own fixed rate
r.acc -= dt;
const t = r.exp ? 1 - Math.exp(-6 * dt) : 0.1;
r.x += (target - r.x) * t;
}
}
ctx.clearRect(0, 0, 380, 220);
ctx.strokeStyle = '#555'; ctx.beginPath(); ctx.moveTo(target, 10); ctx.lineTo(target, 210); ctx.stroke();
ctx.font = '11px system-ui';
for (const r of rows) {
ctx.fillStyle = '#999'; ctx.fillText(r.label, 8, r.y - 12);
ctx.fillStyle = r.color; ctx.beginPath(); ctx.arc(r.x, r.y, 7, 0, 7); ctx.fill();
}
requestAnimationFrame(step);
}
requestAnimationFrame(step);
</script>Springs: SmoothDamp
Exponential damping starts at full speed the instant the target moves. For cameras, that first jolt can feel harsh. A critically damped spring keeps a velocity, so it accelerates smoothly and still never overshoots. Unity's Mathf.SmoothDamp / Vector3.SmoothDamp implement exactly this (with a smoothTime parameter, roughly the time to reach the target); Godot and Unreal users usually write it themselves.
#include <iostream>
#include <iomanip>
#include <cmath>
// Critically damped spring, the approximation used by Game Programming Gems 4 and Unity's SmoothDamp.
double smoothDamp(double current, double target, double& velocity, double smoothTime, double dt) {
double omega = 2 / smoothTime;
double x = omega * dt;
double e = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x); // approximates exp(-x)
double change = current - target;
double temp = (velocity + omega * change) * dt;
velocity = (velocity - omega * temp) * e;
return target + (change + temp) * e;
}
int main() {
std::cout << std::fixed << std::setprecision(3);
double spring = 0, vel = 0, expo = 0;
const double dt = 1.0 / 30;
for (int frame = 1; frame <= 30; frame++) {
spring = smoothDamp(spring, 10, vel, 0.3, dt);
expo += (10 - expo) * (1 - std::exp(-2 / 0.3 * dt));
if (frame % 5 == 0 || frame == 1)
std::cout << "frame " << std::setw(2) << frame << " spring " << std::setw(6) << spring
<< " exponential " << std::setw(6) << expo << "\n";
}
}frame 1 spring 0.211 exponential 1.993
frame 5 spring 3.041 exponential 6.708
frame 10 spring 6.499 exponential 8.916
frame 15 spring 8.448 exponential 9.643
frame 20 spring 9.357 exponential 9.883
frame 25 spring 9.745 exponential 9.961
frame 30 spring 9.902 exponential 9.987
The spring starts much more gently (frame 1: 0.21 versus 1.99) and builds up speed instead of jumping: a softer start that players read as "weighty". It arrives a little later for the same time constant, so shorten smoothTime if it feels laggy.
In the engines
| Engine | Constant per-frame trap | Frame-rate independent |
|---|---|---|
| Unity | Vector3.Lerp(a, b, 0.1f) in Update |
Lerp(a, b, 1 - Mathf.Exp(-speed * Time.deltaTime)), Vector3.SmoothDamp |
| Unreal | FMath::Lerp(A, B, 0.1f) in Tick |
FMath::FInterpTo(Current, Target, DeltaTime, Speed) (first-order approximation; exact enough at normal frame rates) |
| Godot | position.lerp(target, 0.1) in _process |
position.lerp(target, 1.0 - exp(-speed * delta)) |
Tip: The same rule applies to slerp.
Quaternion.Slerp(current, target, 0.1f)every frame is frame-rate dependent; use1 - exp(-speed * dt)as thet.
Test yourself
Why is x = lerp(x, target, 0.1) per frame frame-rate dependent?
- lerp is not linear
- More frames per second apply the 10% step more often, so it converges faster
- It overshoots at low FPS
- It never converges
Which factor makes damping frame-rate independent?
- t = 0.1 * dt
- t = 1 - exp(-speed * dt)
- t = dt * dt
- t = speed / fps
With a half-life of 0.2 s, what fraction of the gap remains after 0.6 s?
- 1/2
- 1/4
- 1/8
- 0