Article
+ UpdatedInterpolation and Easing: Lerp, Slerp, Easing Functions, and Smooth Damping
Making values “flow” instead of “jump” is a major source of good game feel. This chapter starts with lerp, moves on to easing functions, then to a widely circulated technique that depends on frame rate, and finally to its correct version.
The Problem: Why Does Everything Look So “Stiff”?
Health drops from 100 to 60 and the health bar shrinks instantly; UI panels appear out of nowhere; the camera is nailed to the character and shakes with every movement. None of this is a bug, but it all looks cheap.
Good feel usually comes from one principle: don’t let values jump to their target instantly — let them “flow” there over a period of time. The tool for this is interpolation.
Linear Interpolation: Where It All Starts
// Linear interpolation: t = 0 gives a, t = 1 gives b
constexpr float lerp(float a, float b, float t) { return a + (b - a) * t; }
constexpr Vec2 lerp(Vec2 a, Vec2 b, float t) { return a + (b - a) * t; }
// Inverse lerp: what fraction of [a, b] does v sit at?
constexpr float inverseLerp(float a, float b, float v) { return (v - a) / (b - a); }
// Map v from [inMin, inMax] to [outMin, outMax], e.g. health -> health bar width
constexpr float remap(float v, float inMin, float inMax, float outMin, float outMax) {
return lerp(outMin, outMax, inverseLerp(inMin, inMax, v));
}
constexpr float clamp01(float t) { return t < 0 ? 0 : (t > 1 ? 1 : t); }These three functions work as a set:
lerp(a, b, t): given a fraction, find the value.t = 0.25is one quarter of the way from a to binverseLerp(a, b, v): given a value, find the fraction. For example, how much of max health the current health isremap: a combination of the first two, mapping one range onto another. Health → bar width and distance → volume are both remaps
Easing Functions: Controlling “How to Get There”
lerp moves at constant speed. Real-world motion rarely does: objects accelerate from rest, decelerate to a stop, and sometimes overshoot and bounce back. An easing function remaps t through a curve before handing it to lerp:
// Easing function: takes t ∈ [0,1] and returns the "progress". All satisfy f(0) = 0 and f(1) = 1
namespace ease {
inline float linear(float t) { return t; }
inline float inQuad(float t) { return t * t; }
inline float outQuad(float t) { return 1 - (1 - t) * (1 - t); }
inline float inOutQuad(float t) { return t < 0.5f ? 2 * t * t : 1 - std::pow(-2 * t + 2, 2.0f) / 2; }
inline float inCubic(float t) { return t * t * t; }
inline float outCubic(float t) { return 1 - std::pow(1 - t, 3.0f); }
inline float inOutCubic(float t) { return t < 0.5f ? 4 * t * t * t : 1 - std::pow(-2 * t + 2, 3.0f) / 2; }
inline float inOutSine(float t) { return -(std::cos(kPi * t) - 1) / 2; }
inline float smoothstep(float t) { return t * t * (3 - 2 * t); }
// Overshoots the end slightly, then comes back. Good for pop-up UI
inline float outBack(float t) {
const float c1 = 1.70158f, c3 = c1 + 1;
return 1 + c3 * std::pow(t - 1, 3.0f) + c1 * std::pow(t - 1, 2.0f);
}
// Oscillates back and forth like a spring before settling
inline float outElastic(float t) {
if (t <= 0) return 0;
if (t >= 1) return 1;
const float c4 = 2 * kPi / 3;
return std::pow(2.0f, -10 * t) * std::sin((t * 10 - 0.75f) * c4) + 1;
}
// Bouncing on landing
inline float outBounce(float t) {
const float n1 = 7.5625f, d1 = 2.75f;
if (t < 1 / d1) return n1 * t * t;
if (t < 2 / d1) { t -= 1.5f / d1; return n1 * t * t + 0.75f; }
if (t < 2.5f / d1) { t -= 2.25f / d1; return n1 * t * t + 0.9375f; }
t -= 2.625f / d1;
return n1 * t * t + 0.984375f;
}
} // namespace ease
using EaseFn = float (*)(float);
// Drive a tween from a to b over duration, shaped by an easing function
inline float tween(float a, float b, float elapsed, float duration, EaseFn ease) {
return lerp(a, b, ease(clamp01(elapsed / duration)));
}Every easing function satisfies f(0) = 0 and f(1) = 1, and they differ only in the shape in between. The naming is simple: in means slow at the start (accelerating), out means slow at the end (decelerating), inOut is slow at both ends.
Switch easing functions on the left. The curve is in the middle (the dashed line is the linear reference), and on the right are three common uses: translation, scale pop-up, and a progress bar. outBack and outElastic overshoot the end point.
Rules of thumb for choosing an easing function:
Scenario | Recommended | Why |
|---|---|---|
UI element entering the screen |
| Appears quickly and settles softly; |
UI element leaving the screen |
| Starts slowly and leaves with acceleration, without stealing attention |
Looping back-and-forth animation |
| Smooth at both ends, with no abrupt turning points |
Object landing |
| Simulates bouncing, but only suits stylized visuals |
Value changes (coins, score) |
| Fast then slow, so the readout settles at the end |
Interpolating Angles and Directions
Lerping angles directly has a trap: going from 170° to -170°, lerp takes the long way around through 340° instead of the short 20°. The right approach is to wrap the difference into (-π, π] first:
// Angle interpolation: wrap the difference into (-π, π] so we always take the "short side"
inline float lerpAngle(float a, float b, float t) {
float delta = std::remainder(b - a, 2.0f * kPi);
return a + delta * t;
}
// 2D spherical interpolation (slerp): rotate at constant speed between two unit vectors
inline Vec2 slerp(Vec2 from, Vec2 to, float t) { return rotate(from, signedAngle(from, to) * t); }slerp (spherical linear interpolation) rotates at constant speed between two unit vectors, rather than moving along the straight chord. In 3D, rotations are represented by quaternions, and the counterpart is quaternion slerp — the foundation of skeletal animation blending.
Following: A Widely Circulated Technique
For camera follow, health-bar catch-up, and mouse trails, the most common snippet online is this one line:
// Common but flawed: move 10% closer to the target every frame. The result is tied to the frame rate!
inline Vec2 naiveFollow(Vec2 current, Vec2 target, float factorPerFrame) {
return lerp(current, target, factorPerFrame);
}Moving 10% closer to the target each frame looks smooth. But it is tied to the frame rate: at 60 fps it closes in 60 times per second, at 144 fps 144 times, so the follow speed is more than twice as fast.
A little derivation fixes it. Each frame leaves 90% of the distance, so after n frames 0.9ⁿ remains. Measured in time rather than frames, after t seconds at 60 fps 0.9^(60t) remains, which can be written as e^(-k·t), where k = -60·ln(0.9) ≈ 6.32. So:
// Frame-rate-independent smooth follow: the remaining distance decays exponentially; the larger decay is, the tighter the follow
inline float expDecay(float current, float target, float decay, float dt) {
return target + (current - target) * std::exp(-decay * dt);
}
inline Vec2 expDecay(Vec2 current, Vec2 target, float decay, float dt) {
return target + (current - target) * std::exp(-decay * dt);
}This is exponential decay: the remaining distance shrinks by a fixed ratio per second, independent of frame rate. The larger decay is, the tighter the follow.
Red: per-frame lerp 0.1; black: exponential decay; yellow: smoothDamp. At 60 fps red and black coincide exactly. Drag the fps slider and watch how the red dot’s lag changes; hold the mouse button to drag the target.
A few small experiments worth trying:
At 60 fps the red and black dots coincide and the
lagvalues match. That’s becausedecaywas derived to be equivalent at 60 fps.Raise fps to 144 and the red dot follows noticeably tighter; drop it to 15 and the red dot falls far behind. The black dot is unchanged at any frame rate.
Hold the mouse button to drag the target, then stop suddenly. Compare how the black and yellow dots start and stop: the black dot shoots off at full speed immediately, while the yellow dot accelerates gradually.
Smooth Damping: Following with Velocity
Exponential decay has a quirk: when the target suddenly moves, the follower gains a large velocity instantly. For a camera this causes a jerky feel. A better model is a critically damped spring: the follower has its own velocity and is pulled toward the target by a spring, with just enough damping to avoid overshoot.
// Critically damped spring (Game Programming Gems 4, 1.10): carries velocity, starts and stops gently, never overshoots
// smoothTime is roughly the time needed to catch up with the target; the caller stores velocity and passes it in every frame
inline float smoothDamp(float current, float target, float& velocity, float smoothTime, float dt) {
smoothTime = std::max(0.0001f, smoothTime);
float omega = 2.0f / smoothTime;
float x = omega * dt;
float decay = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x); // ≈ e^-x
float change = current - target;
float temp = (velocity + omega * change) * dt;
velocity = (velocity - omega * temp) * decay;
return target + (change + temp) * decay;
}
inline Vec2 smoothDamp(Vec2 current, Vec2 target, Vec2& velocity, float smoothTime, float dt) {
return {smoothDamp(current.x, target.x, velocity.x, smoothTime, dt),
smoothDamp(current.y, target.y, velocity.y, smoothTime, dt)};
}velocity must be stored by the caller on the follower. smoothTime is roughly the time needed to catch up with the target. Unity’s Mathf.SmoothDamp uses exactly this formula.
Choosing Among the Three Follow Methods
For values (health bars, score), exponential decay is enough; for things that need a sense of “weight”, like cameras and crosshairs, use smoothDamp; for fixed-duration animations that need precise control of the curve, use an easing function plus a tween.
Common Pitfalls
Problem | Why | Fix |
|---|---|---|
Follow speed differs across frame rates |
| Switch to |
Never reaches the target | Exponential decay only approaches it asymptotically | Snap to the target once the distance falls below a threshold |
Character turns the long way around | Lerping angles directly | Use |
| Accumulated time exceeds the duration |
|
outBack briefly pushes values out of range | Overshoot is by design | Values with hard limits, such as health or opacity, need to be clamped again |
Applications in Games
UI motion: nearly every panel appearing or disappearing and every button press feedback is a tween plus an easing function.
Camera: use smoothDamp for following, and an
inOutCubictween for camera cuts.“Damage health bar”: the red bar drops immediately while a white bar behind it catches up slowly via exponential decay, so players see at a glance how much health was just lost.
Animation blending: when switching between two animations, use
smoothstepto control the weights and avoid a pose pop.
Further Reading
easings.net: a quick visual reference of common easing curves
Robert Penner, Programming Macromedia Flash MX, Chapter 7: the origin of today’s easing libraries
Thomas Lowe, Critically Damped Ease-In/Ease-Out Smoothing, in Game Programming Gems 4: the source of the smoothDamp formula
Newsletter
Subscribe
Get an email when a new article is published. Unsubscribe anytime.
Copyright
Copyright of this article belongs to its author or rights holder. When reposting or quoting, please follow the applicable license terms and keep the link to this article.
XuYi, “Interpolation and Easing: Lerp, Slerp, Easing Functions, and Smooth Damping”, https://xuyi.dev/en/2026-11-07-0h7yta
Comments
Comments
Loading comments…