SmoothDamp and Second-Order Follow
SmoothDamp is the exact critically damped spring, evaluated one frame at a time, so it gives the same motion at any frame rate. Add one more term that looks at how fast the target is moving and the same equation can anticipate, overshoot or keep up with no lag at all, depending on a single knob .
What the symbols mean
- Smooth time
The tuning knob: roughly how long the follower takes to close most of the gap.
- Gap
Follower position minus target position.
- Velocity
The follower's velocity, kept from one frame to the next.
- Frame time
How much time this frame covers, in seconds.
What the symbols mean
- Follower
The thing that moves: a head turning, a hand reaching, a UI element tracking the cursor.
- Target
Where the follower is trying to be.
- Frequency
How quickly it responds, in cycles per second.
- Damping ratio
How bouncy, exactly as for any spring.
- Response
How it starts: r < 0 winds up the wrong way first, r = 0 is a plain spring, r > 1 overshoots, r = 2 keeps up with a steadily moving target with no lag.
What the symbols mean
- Target speed
The target's constant velocity.
Explanation
SmoothDamp is simply a critically damped spring packaged as a one-line function: every frame you hand it where the follower is, how fast it's moving and where the target is, and it hands back the new position and velocity. The name says what it does: it smoothly damps out the gap to the target. The update in the box looks like a lot of letters, but it's the critical spring solution from Springs: Bouncy, Critical and Sluggish, , evaluated one frame ahead. The pieces are (how far the follower is from the target), (its velocity), (the coefficient of in that solution), (how much the gap shrinks in one frame) and , which turns a "smooth time" , easier to tune by feel, into a frequency. There's nothing new here, only bookkeeping; bear with it.
Where the update comes from. Take the critical solution and its derivative from the last lesson, starting from the current gap and velocity :
(Differentiate the first with the product rule: , and .) Set and you have the box. Here it is as code; game engines ship essentially this function:
function smoothDamp(x: number, v: number, target: number, smoothTime: number, dt: number) {
const w = 2 / smoothTime;
const u = x - target;
const c = v + w * u;
const e = Math.exp(-w * dt); // engines often use 1 / (1 + k + 0.48k² + 0.235k³) with k = w·dt instead
return { x: target + (u + c * dt) * e, v: (v - w * c * dt) * e };
}
Why the frame rate can't change it. Compare , which moves a fixed fraction per frame, so at 144 fps it's more than twice as fast as at 60. SmoothDamp moves along the exact solution, and following an exact solution for s twice lands exactly where following it for s once does. The first worked example checks this with numbers. The cheap fraction that engines use in place of agrees with it to about for , so the frame-rate independence survives.
What smooth time means. Released from rest, the gap left after time is of where it started. At we have , and : the follower has closed of the gap. Half the gap is gone a little sooner, at , about . So smooth time is "roughly how long it takes", not the time to arrive (a critically damped spring never quite arrives). Daniel Holden's "Spring-It-On" parameterizes springs by a half-life for exactly this reason.
Following a moving target. Now let the target move at a steady speed , a player running in a straight line. Once things settle, the follower moves at speed too, so and . Put that into the spring, :
The camera trails the player by a fixed distance, speed times smooth time. That's the price of smoothness: a smoother camera (bigger ) lags farther behind. The drag term is to blame. It pushes against the follower's own velocity, even when that velocity is exactly right.
Second-order follow: one equation, three personalities. So let the drag push against the difference between the follower's velocity and the target's: replace with . That's the second formula in the box. It has three knobs that mean something to an animator: (how fast, in cycles per second, with ), (how bouncy) and (how it starts). You'll also see it written as with , and . Multiply that form by and you get exactly the boxed one, since and .
Redo the moving-target calculation with the new term and the steady lag becomes . At it's the plain spring's lag; at it's zero.
What does when the target jumps. When the target snaps from to , is a huge, brief spike whose total (its integral) is the size of the jump. Integrate the equation across that instant: and don't have time to change, so the only terms that add up to anything are and the spike. The velocity jumps by on the spot. After that it's an ordinary spring heading for . With , starting from gap and velocity , the critical formula gives
- : the plain critically damped spring. Smooth, no overshoot.
- : the starting velocity points away from the target. The follower winds up the wrong way first, then goes. That's anticipation, one of the classic principles of animation (a character crouches before it jumps), coming out of one equation.
- : the starting kick is bigger than a spring would give, and the follower overshoots even though it's critically damped. At it overshoots by .
Here's one frame of it in code, stepped the way the next lesson recommends (velocity first, then position):
// y, yv: follower and its velocity. x, xv: target and its velocity (xv = (x - xPrev) / dt).
const w = 2 * Math.PI * f;
yv += dt * (w * w * (x - y) + 2 * z * w * ((r / 2) * xv - yv));
y += dt * yv;
A common mistake. SmoothDamp's velocity has to survive from one frame to the next. It's tempting to keep only the position and pass every frame, since the function returns a position anyway. Then every frame restarts the spring from rest, and a spring starting from rest barely moves in one short frame: each frame keeps of the gap. With at 60 fps that's a frame, so after a whole second of the gap is still there instead of , and it gets slower at higher frame rates. Store the velocity it hands back.
A common mistake. With the follower reacts to the target's velocity. If the target teleports (a respawn, a camera cut), the estimate is enormous for one frame and the follower gets a huge kick. Reset the follower, or skip the velocity term, on a teleport.
The circle chase further down shows SmoothDamp's lag in action; the pulse after it shows the three personalities side by side. If you've read Chaining and Interrupting Motion, notice that a spring retargets for free: it carries its velocity, so a new target mid-flight never causes a jolt. And the "slow in" of Timing: Why Motion Needs Easing is built into the critical spring's start from rest.
See it
Worked examples
A camera at m follows a target fixed at using SmoothDamp with smooth time s, starting at rest. Find its position and velocity after one frame of s. Then check that two frames of s land in the same place.
- 1
Turn the smooth time into a frequency: per second.
A character's head tracks a target with second-order dynamics, Hz, and . The target jumps from to . How far does the head swing the wrong way first, and when?
- 1
Turn the frequency into : rad/s.
Practice
Type your answer and check it. Solve them all to complete the lesson.
An object follows a fixed target with SmoothDamp, smooth time s. It starts at rest m from the target. How far from the target is it s later?
A chase camera uses SmoothDamp with smooth time s to follow a car driving in a straight line at a steady m/s. Once things settle, how far behind the car is the camera (in meters)?
A follower uses one SmoothDamp step with per second and s. Its target is at ; it's at moving at (away from the target). Where is it after the step?
A drone camera follows its target with second-order dynamics , using Hz, and . The target flies in a straight line at a steady m/s. Once things settle, how far behind is the camera (in meters)?
A UI element tracks a cursor with second-order dynamics . For which response value does it keep up with a cursor moving at constant velocity with no lag at all?
A follower with and response chases a target that jumps from to . Its position afterwards is . By what percent of the jump does it overshoot?
A follower with and response chases a target that jumps from to . Its position afterwards is . How far below its starting point does it dip, as a fraction of the jump?
A SmoothDamp follower with starts at rest at and chases a target fixed at , so with and . Solve for .
The hardest one here. Get it, and you've got this lesson.
A chase camera uses second-order follow with Hz and . It must trail a car moving at a steady m/s by exactly m. Choose to make that happen, then find by what percent of the jump this camera overshoots when its target instead jumps by a fixed distance and stops. (With , the response to a jump is .)
Where it's used
Most game engines ship a SmoothDamp-style function, and it's the default way to make a camera, a health bar, a minimap marker or a UI counter chase a value without jitter at any frame rate. Second-order follow is a favorite for procedural animation: a head tracking a target, a hand reaching, a mech's legs placing their feet, a UI element that anticipates a swipe, all tuned with , and instead of hand-made keyframes. Robotics uses the same idea as a reference filter, smoothing a jumpy setpoint so motors aren't asked for impossible accelerations.