Springs and Damping
Springs are everywhere in games: suspension on a car, a wobbly antenna, a camera that lags behind the player, a soft rope, the "juice" bounce on a menu button. Inside physics engines, springs are also how soft constraints work, including the contacts in Box2D v3. This lesson builds a spring, shows how it explodes, and then fixes it the way engines do.
Hooke's law plus damping
A spring pulls toward its rest length with a force proportional to the stretch x, and a damper resists the speed:
F = -k · x - c · vkis the stiffness (N/m). Higher means a faster, harder spring.cis the damping (N·s/m). Higher means the oscillation dies sooner.
Raw k and c are awkward to tune because the right values depend on the mass. Two derived numbers are mass-independent and much easier to reason about:
- natural frequency
ω = sqrt(k / m)(orf = ω / 2πin hertz, oscillations per second); - damping ratio
ζ = c / (2 · sqrt(k · m)): below 1 the spring overshoots and wobbles (underdamped), at exactly 1 it returns as fast as possible without overshooting (critically damped), above 1 it creeps back slowly (overdamped).
#include <iostream>
#include <iomanip>
#include <cmath>
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
int main() {
std::cout << std::fixed << std::setprecision(3);
const double m = 1, k = 100, dt = 1.0 / 240; // f = 10/2pi = 1.59 Hz
std::cout << "zeta | t=0.10 t=0.25 t=0.50 t=1.00 t=2.00 | lowest x\n";
for (double zeta : {0.1, 0.5, 1.0, 2.0}) {
double c = zeta * 2 * std::sqrt(k * m);
double x = 1, v = 0, lowest = 1; // released from a 1 m stretch
std::cout << std::setw(5) << zeta << " | ";
for (int i = 1; i <= 480; i++) {
double a = (-k * x - c * v) / m;
v += a * dt; // semi-implicit Euler
x += v * dt;
if (x < lowest) lowest = x;
if (i == 24 || i == 60 || i == 120 || i == 240 || i == 480) std::cout << std::setw(7) << z0(x) << " ";
}
std::cout << "| " << std::setw(6) << z0(lowest) << "\n";
}
}zeta | t=0.10 t=0.25 t=0.50 t=1.00 t=2.00 | lowest x
0.100 | 0.551 -0.582 0.117 -0.328 0.071 | -0.729
0.500 | 0.644 -0.031 -0.068 -0.002 0.000 | -0.159
1.000 | 0.723 0.281 0.042 0.001 0.000 | 0.000
2.000 | 0.814 0.547 0.281 0.074 0.005 | 0.005
With ζ = 0.1 the spring overshoots to about -0.73 m and is still swinging after two seconds. At ζ = 0.5 it overshoots a little once. At ζ = 1 it never goes past zero (the "lowest x" column stays at 0) and settles fastest. At ζ = 2 it is slow. For gameplay springs (cameras, UI, follow behaviour) critical damping is usually the target; a small overshoot (ζ ≈ 0.6–0.8) reads as "bouncy but controlled". Frame-Rate-Independent Damping in Math for Games covers the closed-form critically damped spring (SmoothDamp), which is exact at any frame rate.
Why stiff springs explode
A spring force is computed from the current stretch and then applied for a whole step. If the step is long compared with the spring's period, the force overshoots, the next force is even larger in the other direction, and the energy grows every step. For semi-implicit Euler, the limit is ω · dt < 2. At 60 Hz that is ω < 120, which is k < 14400 · m. Explicit Euler is worse: it gains energy at any stiffness (see Euler, Semi-Implicit Euler and Verlet).
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
// Released from x = 1 with no damping; report the largest |x| during the second second.
double run(double k, bool semiImplicit) {
const double m = 1, dt = 1.0 / 60;
double x = 1, v = 0, peak = 0;
for (int i = 1; i <= 120; i++) {
double a = -k * x / m;
if (semiImplicit) { v += a * dt; x += v * dt; }
else { x += v * dt; v += a * dt; } // explicit Euler: old v moves x
if (i > 60) peak = std::max(peak, std::fabs(x));
}
return peak;
}
int main() {
std::cout << std::setprecision(3);
std::cout << " k | omega*dt | explicit peak | semi-implicit peak\n";
for (double k : {100.0, 1000.0, 10000.0, 14000.0, 15000.0, 40000.0}) {
std::cout << std::setw(7) << long(k) << " | " << std::setw(8) << std::sqrt(k) / 60 << " | "
<< std::setw(13) << run(k, false) << " | " << std::setw(12) << run(k, true) << "\n";
}
} k | omega*dt | explicit peak | semi-implicit peak
100 | 0.167 | 4.78 | 1
1000 | 0.527 | 1.65e+06 | 1.04
10000 | 1.67 | 2.21e+34 | 1.81
14000 | 1.97 | 2.19e+41 | 6
15000 | 2.04 | 1.96e+42 | 4.06e+21
40000 | 3.33 | 8.96e+64 | 3.63e+114
An undamped spring released from 1 m should keep a peak of exactly 1. Explicit Euler gains energy even at k = 100, and at higher stiffness the value runs off to astronomical numbers. Semi-implicit Euler stays bounded right up to ω · dt ≈ 2 (the peaks grow above 1 near the limit because the method is increasingly inaccurate, but they don't keep growing) and then explodes too. In a game this is the car suspension that launches the car into orbit when someone sets the stiffness to 50000, or a ragdoll that vibrates apart at low frame rates. Damping doesn't save you: past the limit, the damper overshoots as well.
Soft constraints: springs that can't explode
Engines avoid the problem by solving springs implicitly: instead of using the force at the start of the step, they solve for the velocity that the spring will have at the end of the step. For a spring holding a body at a target, that gives three coefficients computed once per step from the frequency ω, damping ratio ζ and step h (this is the form Box2D v3 uses for its soft contacts and joints):
a1 = 2ζ + hω biasRate = ω / a1
a2 = h · ω · a1 massScale = a2 / (1 + a2)
impulseScale = 1 / (1 + a2)Each step the solver applies the impulse -massScale · m · (v + biasRate · C) - impulseScale · accumulatedImpulse, where C is the constraint error (the stretch). With one solver pass per step the accumulated term is zero. As the frequency goes to infinity, massScale goes to 1 and biasRate to 1/h: the spring becomes a rigid constraint that removes the whole error in one step, but never more. That is the key property: you cannot make it explode by making it stiffer.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
// A body held at x = 0 by a soft constraint, released from x = 1. Tuned by hertz and damping ratio.
double soft(double hertz, double zeta, double& atQuarter) {
const double m = 1, h = 1.0 / 60, pi = 3.141592653589793;
double omega = 2 * pi * hertz;
double a1 = 2 * zeta + h * omega, a2 = h * omega * a1;
double biasRate = omega / a1, massScale = a2 / (1 + a2);
double x = 1, v = 0, peak = 0;
for (int i = 1; i <= 120; i++) {
double C = x; // error: distance from the target
double impulse = -massScale * m * (v + biasRate * C);
v += impulse / m;
x += v * h;
if (i == 15) atQuarter = x;
if (i > 60) peak = std::max(peak, std::fabs(x));
}
return peak;
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << "hertz | k = m*omega^2 | x at 0.25 s | peak in 2nd second\n";
for (double hz : {1.0, 3.0, 30.0, 1000.0}) {
double atQuarter = 0;
double peak = soft(hz, 0.7, atQuarter);
double k = std::pow(2 * 3.141592653589793 * hz, 2);
std::cout << std::setw(5) << int(hz) << " | " << std::setw(13) << long(k) << " | " << std::setw(11) << z0(atQuarter) << " | " << z0(peak) << "\n";
}
}hertz | k = m*omega^2 | x at 0.25 s | peak in 2nd second
1 | 39 | 0.451 | 0.015
3 | 355 | -0.021 | 0.000
30 | 35530 | 0.000 | 0.000
1000 | 39478417 | 0.000 | 0.000
At 1 Hz the body is still 45 cm out after a quarter second, at 3 Hz it is already back (with a 2 cm overshoot, as expected for ζ = 0.7), and at 30 Hz and 1000 Hz (stiffnesses of 35000 and 39 million, far past the explicit limit) it simply goes to the target and stays there. Very high frequencies are still not free: Box2D's contactHertz comment warns that raising it speeds up overlap recovery but can introduce jitter, because the constraint then fights every small error at full strength. But it never explodes.
Springs in engines
| Engine | Spring components | Tuned by |
|---|---|---|
| Unity | SpringJoint (spring, damper), ConfigurableJoint drives (positionSpring, positionDamper) |
stiffness and damping |
| Unreal | Physics Constraint linear and angular drives, UPhysicsSpringComponent for simple raycast springs |
strength and damping |
| Godot | Generic6DOFJoint3D springs, DampedSpringJoint2D (stiffness, damping) |
stiffness and damping |
| Box2D v3 | b2DistanceJointDef with enableSpring, hertz, dampingRatio; soft contacts via contactHertz |
frequency and damping ratio |
When an engine only takes stiffness and damping, compute them from the frequency and ratio you want: k = m · (2πf)² and c = 2 · m · ζ · 2πf. Your design notes stay meaningful when someone changes the mass.
Test yourself
A spring has damping ratio ζ = 1. How does it behave when released?
- It oscillates forever
- It returns as fast as possible without overshooting
- It overshoots once
- It never moves
At 60 Hz with semi-implicit Euler, roughly which stiffness makes a 1 kg undamped spring blow up?
- k = 100
- k = 1000
- k = 15000 and above
- Any stiffness is safe
Why can an implicit soft constraint be made arbitrarily stiff without exploding?
- It ignores mass
- It solves for the end-of-step velocity, and in the stiff limit it just removes the full error in one step
- It clamps the force to 1 N
- It uses double precision