A cinematic camera following a spline through waypoints by advancing t at a constant rate speeds up and slows down; why, and how do you make it move at constant speed?
answer
- parameter is not distance
- equal t per segment, unequal lengths
- table of t against distance
- binary search, then inverse lerp
basics
~20 sThe parameter t is not distance: the curve's speed |P'(t)| varies within a segment, and equal t per segment gives long segments more distance per second. Build an arc-length table, then map distance travelled back to t each frame.
solid answer
~40 sA spline's parameter measures progress through the polynomial, not metres. Within one cubic segment `|P'(t)|` changes as the control points bunch and spread, and a path with a 2 m segment and an 18 m segment that gives each half of the sweep moves the camera nine times faster on the long one. The fix is reparameterising by arc length: sample the curve at many `t` values, accumulate chord lengths into a table of `(t, s)` pairs, and each frame advance distance, `d += speed * dt`, binary-search the table for the bracketing samples and inverse-lerp between them to get `t`. Easing then goes on distance, not on `t`. A Newton step `t -= (s(t) - d) / |P'(t)|` refines a coarse table, and the table must be rebuilt when a waypoint moves.
code
pseudocode · 26 linesfunction buildTable(curve, n):
table = [(t = 0, s = 0)]
prev = curve.evaluate(0)
s = 0
for i = 1 to n:
t = i / n
p = curve.evaluate(t)
s = s + length(p - prev)
table.append((t = t, s = s))
prev = p
return table
function paramAtDistance(table, d):
last = table.count - 1
d = clamp(d, 0, table[last].s)
lo = 0
hi = last
while lo < hi: // first index with s >= d
mid = floor((lo + hi) / 2)
if table[mid].s < d: lo = mid + 1
else: hi = mid
if lo == 0: return 0
a = table[lo - 1]
b = table[lo] // a.s < d <= b.s, so b.s > a.s
f = (d - a.s) / (b.s - a.s)
return lerp(a.t, b.t, f)go deeper
Know that a spline's parameter t is not distance, so equal steps in t do not give equal steps along the path.
Explain building a table of t against accumulated chord length and mapping distance back to t with a binary search and an inverse lerp.
Diagnose uneven camera speed from its symptom, weigh chord tables against quadrature and Newton refinement, and ease distance rather than t.
Decide where reparameterisation lives in the path tooling, baked at authoring time or computed at runtime, and what accuracy cinematics need.
## The symptom and its cause A camera on a spline path that advances its parameter at a fixed rate, `t += dt / duration`, almost never moves at a fixed speed. It dawdles through clusters of close waypoints, races along long stretches, and can speed up and slow down inside a single segment as the curve bends. The cause is that **the parameter is not distance**. The camera's speed in metres per second is `|P'(t)|`, metres per unit of `t`, multiplied by how fast `t` advances, and `|P'(t)|` is not constant. Two effects stack: - **between segments**: a global parameter is usually split evenly, so each segment gets the same share of time however long it is. With a 2 m segment and an 18 m segment sharing a 10-second sweep, each takes 5 s: 0.4 m/s on the short one and 3.6 m/s on the long one, a factor of nine; - **within a segment**: a cubic's speed `|P'(t)|` changes as its control points bunch and spread, so even a lone segment does not move uniformly. ## Measuring arc length The fix is to move by **arc length** `s`, the distance travelled along the curve, and convert distance back into a parameter. The arc length from 0 to `t` is the integral of `|P'(u)|`, and for a general cubic that integral has no closed form in elementary functions, so it is computed numerically: - **chord sums**: sample the curve at many parameter values and add the straight distances between consecutive samples; simple, and always slightly short, because a chord is never longer than the arc it spans; - **Gaussian quadrature** of `|P'(t)|`: a handful of well-placed samples per segment gives a very accurate length for a smooth segment; - **adaptive subdivision**: split each segment, with de Casteljau for a Bezier, until the pieces are nearly straight, then sum the pieces. ## Building and querying the table The common production approach is a lookup table of `(t, s)` pairs: 1. sample each segment at `n` evenly spaced parameter values and accumulate chord lengths, storing the running total `s` beside each `t`; 2. each frame, advance the distance, `d += speed * dt`, instead of the parameter; 3. binary-search the table for the first sample whose `s` is at least `d`; 4. inverse-lerp `d` between that sample's `s` and the previous sample's, and lerp their `t` values by the same fraction; 5. evaluate the curve at the resulting `t`. The table costs `O(n)` to build and `O(log n)` per lookup. It must be rebuilt, at least for the affected segments, whenever a waypoint moves. The code example shows steps 3 and 4. ## Accuracy and cost | Technique | Build cost | Per-frame cost | Error | |---|---|---|---| | uniform `t` | none | evaluate the curve | speed varies freely | | `t += speed * dt / abs(P'(t))` | none | one derivative | first-order; drifts over time; unstable where the curve nearly stops | | chord table and inverse lerp | `n` samples per segment | binary search and evaluate | small; shrinks roughly with the square of the sample spacing | | table plus one Newton step | the same | one more derivative and length evaluation | close to exact | The **Newton step** refines a table guess. Since `ds/dt = |P'(t)|`, the correction is `t = t - (s(t) - d) / |P'(t)|`, where `s(t)` is measured precisely, for example by quadrature from the nearest table sample. Where `|P'(t)|` is close to zero, at a cusp or a stationary point, the step can jump wildly, so fall back to bisection there. ## Easing and other refinements - **Ease distance, not the parameter**: `d = totalLength * ease(time / duration)` gives a camera that starts and stops gently while covering the path evenly; easing `t` instead stacks the curve's own speed changes on top of the ease. - **Per-segment tables** keep rebuilds local: store each segment's length and a cumulative offset, find the segment by binary search on the offsets, then search within it. - **Degenerate spans**: two waypoints at the same position make a zero-length span. Searching for the first sample whose `s` is at least `d` guarantees the bracketing pair has different distances, so the inverse lerp never divides by zero. - **Look-ahead points**: a camera that aims at a point a fixed distance ahead on the path needs the same distance-to-parameter lookup for that point. ## How to answer it in an interview A strong answer states the diagnosis in one sentence (the parameter is not distance), names both layers (unequal segments, uneven speed inside a segment), and then walks the table: build, search, inverse lerp, evaluate, rebuild on edit. Adding that chord sums underestimate the length, and that easing belongs on distance, shows the candidate has shipped it.
- Why is advancing t by speed * dt / |P'(t)| each frame not enough on its own?It is a first-order step: it assumes `|P'(t)|` stays constant across the frame, so every frame leaves a small error and the errors accumulate into drift, which matters when the camera must arrive on cue. Where `|P'(t)|` approaches zero the step explodes. It is a cheap fallback for gameplay objects, but cinematics usually use a table, optionally refined with a Newton step.
- How do you ease the camera in and out once it moves by arc length?Ease the distance: `d = totalLength * ease(time / duration)`, then look up `t` from the table. The speed along the path is then exactly the ease curve's slope scaled by `totalLength / duration`, independent of how the spline is parameterised. Easing `t` instead would multiply the ease by the curve's own uneven speed.
- What should happen to the arc-length table when a designer drags a waypoint in the editor?Rebuild the tables of the segments that waypoint influences (four for Catmull-Rom, the adjacent ones for a Bezier chain), then recompute the cumulative offsets after them. Decide whether a camera already moving keeps its distance or its fraction of the total length, since the total changes; keeping the fraction avoids a jump at the end of the path.
- Uniform steps in t give uniform steps in distance along a spline.
- Giving each segment an equal share of t fixes uneven speed.
- A cubic's arc length has a simple closed formula you can evaluate directly.
- Summing chord lengths overestimates the true arc length.
- Applying an easing curve to t is the same as easing the distance.