← Math
Math

Springs, damping, and feedback

Why does the bottom sheet fly off the screen?

Use position error and velocity to simulate a spring over bounded time steps.

Outcome

Step a damped spring correctly, choose a rest rule that needs both position and speed, and bound the step so dropped frames cannot destabilise it.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Stiffness
How hard the spring pulls per pixel of distance from the target.
Damping
A force against the velocity that removes energy, so the motion dies down.
Overshoot
How far the value passes the target before coming back.
Settling
Reaching a state close to the target and slow enough to stop the animation.
Stability
Whether a step-by-step simulation stays bounded; too large a step can make it grow instead.

Why this matters

Springs make interfaces feel physical, and the same model describes thermostats, autoscalers and camera followers: a correction proportional to the error, and damping that keeps it from oscillating. Simulating one frame at a time adds two decisions of your own: when to stop, and how large a step the simulation can take.

This lesson assumes velocity, acceleration and multiplying rates by time.

Feedback with position and velocity

Feedback uses the current state to correct a difference from a target. A spring applies a restoring force proportional to position error; damping opposes velocity. Mass converts their combined force into acceleration. Position alone cannot describe the motion because the object may already be moving.

Recall that multiplying acceleration by elapsed time changes velocity, and velocity over time changes position. Repeating those updates approximates the continuous spring. The integration step and rest thresholds are additional numerical and design choices; a small step suitable for one set of constants is not a universal stability guarantee.

01A spring is feedback

A spring is feedback

Your mobile app’s bottom sheet springs open to 320 px. A rewrite of the animation code made it stop with a visible jump, and on slow phones it sometimes shoots far past the top of the screen when the app is busy loading results.

The sheet’s spring pulls with a force proportional to its distance from 320 px, stiffness k =500, while damping c =30 pushes against its velocity. With mass m =1, acceleration is a = (−k(x −320) −cv)/m. Position x is in pixels, v in pixels/second; k/m has units second⁻² and c/m second⁻¹. Low damping overshoots and swings; high damping creeps in slowly; this pair overshoots by about 7 px and settles in about a third of a second.

React Native Reanimated’s withSpring takes stiffness, damping, mass and rest thresholds; react-spring calls the same pair tension and friction.

02One step at a time

One step at a time

Each frame updates the velocity from the acceleration, then the position from the new velocity. This order is semi-implicit Euler: v_new = v + ah, then x_new = x + v_new h, with h in seconds. It is useful for spring simulation, although its stability still depends on stiffness, damping, mass and step size. From rest, one 16 ms frame gives v = 160,000 × 0.016 = 2,560 px/s and x = 40.96 px.

Flip the damping sign and it adds energy: after five frames the sheet is at 888 px instead of 248.

03Close is not at rest

Close is not at rest

At frame 19 the sheet is 0.37 px from its target, but on its way back from the overshoot at 25.9 px/s. Stopping there freezes it mid-swing, and the next layout snaps it. Requiring both under 0.5 px and under 5 px/s stops it at frame 22.

Why not stop on speed alone?

04A long frame is a big step

A long frame is a big step

Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The sheet's spring (stiffness 500, damping 30) and one 100 ms frame while the app is busy.
const step = (x: number, v: number, h: number): [number, number] => {
  const nv = v + (-500 * (x - 320) - 30 * v) * h;

  return [x + nv * h, nv];
};

let [x, v] = step(0, 0, 0.1); // one step for the whole frame

console.log(`one step:  x=${x.toFixed(1)} v=${v.toFixed(1)}`);
[x, v] = [0, 0];

for (let i = 0; i < 17; i++) [x, v] = step(x, v, 0.1 / 17); // steps no longer than 6 ms

console.log(`17 steps: x=${x.toFixed(1)} v=${v.toFixed(1)}`);

When the app is busy, a frame can take 100 ms. One step that long multiplies the spring force by a step the integrator cannot follow: the sheet lands at 1,600 px, five times its target, and the next steps grow. Splitting the frame into steps no longer than 6 ms keeps it on course. For the interrupted trace, eight 16ms frames are followed by two 100ms frames, then fifty 16ms frames. The largest overshoot observed at frame boundaries is 848.54px with one step per frame and 10.04px with bounded sub-steps. To cover a frame dt without exceeding maximum h_max, choose an integer n with dt/n ≤ h_max, so the smallest valid count is ceil(dt/h_max); use dt/n for every step. At 100ms and 6ms this is 17 steps of about 5.88ms. Rest and recorded overshoot are checked once per frame, not after each sub-step.

05What the simulation does not promise

What the simulation does not promise

06Fix the bottom sheet

Fix the bottom sheet

Repair the sheet’s force direction and its rest decision, then preserve elapsed time while splitting slow frames into bounded steps. The same spring must survive the busy-device trace.

In the frontend The sheet’s spring in React Native and Svelte Optional

Animation libraries expose related settings, with different numerical conventions. Current Reanimated uses an energy threshold for rest; Svelte Spring uses its own stiffness/damping scale and precision checks on distance and per-step displacement. Neither is a parameter-for-parameter implementation of the lab’s 0.5px/5px-per-second rule. Reanimated runs animation work on the UI thread so a busy JavaScript thread need not stall it.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The bottom sheet in React Native with Reanimated: the spring runs on the UI thread,
// so a busy JavaScript thread does not hand it one huge step.
import { withSpring } from "react-native-reanimated";

export const SHEET_SPRING = {
  stiffness: 500,
  damping: 30,
  mass: 1
  // Current Reanimated owns an energy-based rest rule.
  // Its default energyThreshold is not the lab’s pair of rest tolerances.
};

export function openSheet(offset: { value: number }) {
  offset.value = withSpring(320, SHEET_SPRING);
}

Check this idea later
In a stable simulation of this spring, you increase damping from 30 to 60. What happens to the overshoot?
A 50 ms frame with a 6 ms maximum step. How many sub-steps?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The sheet and its constants are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.