Intuition
Lerp draws a straight line between A and B. t=0 gives A, t=1 gives B, t=0.5 gives halfway.
Variables
- Start value.
- End value.
- Interpolation factor, usually clamped to 0..1.
Explanation
Lerp is used for blending positions, colors, health bars, camera movement, and animation parameters. The key is choosing the right t. For frame-rate-independent smoothing, t should be derived from delta time.
Where it shows up in games
Camera follow, UI transitions, animation blending, damage flashes, gradual stat changes.
- For smooth camera follow, use exponential damping rather than a fixed t each frame.
- Clamp or validate t when it comes from untrusted input.
- Using a constant lerp factor per frame makes smoothing depend on frame rate.
- Be clear whether your lerp clamps t. Unclamped lerp (extrapolation) is sometimes useful but often a bug.
Implementation
JavaScript & C++function lerp(a, b, t) {
return a + (b - a) * t;
}
function lerpVec3(a, b, t) {
return {
x: lerp(a.x, b.x, t),
y: lerp(a.y, b.y, t),
z: lerp(a.z, b.z, t),
};
}
// Frame-rate-independent smoothing
function damp(current, target, smoothing, dt) {
// smoothing: higher = snappier
const t = 1 - Math.exp(-smoothing * dt);
return lerp(current, target, t);
}#include <cmath>
float lerp(float a, float b, float t) {
return a + (b - a) * t;
}
float damp(float current, float target, float smoothing, float dt) {
float t = 1.0f - std::exp(-smoothing * dt);
return lerp(current, target, t);
}