Moving at Constant Speed Along a Curve
The parameter is not distance: equal steps in bunch up where the curve is slow and spread out where it's fast. To move at a steady speed, measure the curve's length as a function of , store it in a table, and each frame look up the that sits at the distance you want.
What the symbols mean
- Arc length so far
The distance along the curve from its start to the point at parameter t.
- Speed
How fast the point moves per unit of the parameter, at parameter u.
- Dummy variable
The parameter running from 0 up to t inside the integral.
What the symbols mean
- Table entry
The approximate distance along the curve up to t_k, the sum of the first k chord lengths.
- Number of rows
More rows give shorter chords and a more accurate table.
What the symbols mean
- Wanted distance
How far along the curve you want to be, like speed × time.
Explanation
Arc length is simply the distance you'd measure along a curve with a piece of string, and the trouble this lesson fixes is that isn't that distance. As runs from to at a steady rate, a point on a Bézier curve races along some stretches and crawls along others, because its speed is (the length of the velocity from Bernstein Polynomials and Tangents), and that changes as the handles pull. The formula only says: distance is speed added up over time. Then we turn it around: given the distance we want, find the that gets us there. It's a little calculus and a little bookkeeping; bear with it.
See the problem. Take a track with control points , , and . Its speed is at the start (three times the first handle), drops to about in the middle and comes back to at the end. So if a train steps by the same amount every frame, it shoots out of the station, crawls through the S-bend at a quarter of the speed, and speeds up again. Halfway through the run in it has already covered of the track. There's an animation further down with two trains on this track, one stepping evenly and one stepping distance evenly.
Length is speed added up. Over a tiny step the point moves by and , so by Pythagoras it travels . Add up all the tiny steps from to and you get the integral at the top (see The Definite Integral and arc length in Parametric Curves). By the Fundamental Theorem, : the distance grows at the speed, as it should.
Why there's no formula to look up. For a cubic Bézier, is the square root of a degree-4 polynomial, and its integral almost never has a closed form. So the length is computed numerically, in one of two ways:
- Chords. Sample the curve at and add up the straight distances between neighbors. Chords cut corners, so the sum is always a little short, and it closes in on the true length as grows.
- Gauss–Legendre quadrature. Evaluate the speed at a few cleverly chosen values of and add them with weights. The two-point rule at the top is already exact for any cubic polynomial, and the rules with more points that Pomax's primer on Bézier curves uses get a curve's length almost exactly from a couple dozen speed samples.
A few special curves do have exact lengths: ones whose speed happens to be a polynomial. The examples use one.
The lookup table. Run the chord method once when the curve is created and keep every running total: a table of rows with and the whole length. The only ever increase, which is what makes the next step work.
Inverting: from distance to . To be a distance along, find the row where (walk the table, or binary search since it's sorted). Then do an inverse lerp to see how far is between and , and a lerp to go the same fraction between and : the formula at the top, straight from Inverse Lerp, Remap and Clamp. For more precision, polish with a Newton step , the same trick a timing curve uses in Cubic-Bezier Timing Curves.
Moving at constant speed. Each frame, add to the distance traveled , look up the for , and draw the object at . Now the object covers the same distance every second, however the handles are set. And once motion is measured in distance, you can ease it too: run through an ease-in-out from Timing: Why Motion Needs Easing and the train pulls away and brakes smoothly, whatever the shape of the track.
// Build once: running chord lengths s[k] at t = k/N. curve(t) returns a point {x, y}.
function buildTable(curve: (t: number) => { x: number; y: number }, N: number): number[] {
const s = [0];
let prev = curve(0);
for (let k = 1; k <= N; k++) {
const p = curve(k / N);
s.push(s[k - 1] + Math.hypot(p.x - prev.x, p.y - prev.y));
prev = p;
}
return s;
}
// Each frame: which t is distance d along the curve?
function tAtDistance(s: number[], d: number): number {
const N = s.length - 1;
let k = 1;
while (k < N && s[k] < d) k++; // or a binary search
const f = (d - s[k - 1]) / (s[k] - s[k - 1]); // inverse lerp
return (k - 1 + f) / N; // lerp between t = (k - 1)/N and k/N
}
A common mistake. It's tempting to think is halfway along the curve, since it's halfway from to . It's only halfway through the parameter. Where the handles are long the point moves fast, so equal steps in give unequal steps in distance, and can be anywhere from well short of the middle to well past it. Stepping evenly is fine for drawing a curve, but not for moving something along it at a steady speed.
See it
Worked examples
A curve has control points , , and . Find its exact length, and how far along it the point is.
- 1
Start with the three handle arrows: , and .
A track is the cubic Bézier with control points , , and . Build a 4-row lookup table of distances at from chords, then use it to find the that is units along the track.
- 1
Find the Bernstein weights at : , , and .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A train runs on a straight track along the -axis, following the cubic Bézier with control points , , and , in meters. How far along the track is it at ?
A curve has control points , , and . Find its speed at .
Estimate the length of the cubic Bézier with control points , , and using two chords, from to to .
A curve has control points , , and . Find its exact length.
A toy train's track has this distance table, from chords: at the distances are meters. The train must be m along the track. Which should the game use?
A curve has control points , , and ; its speed is and its length is . At what is a point moving along it exactly halfway along, units from the start? Give to 3 decimal places.
A cubic Bézier has control points , , and , so its velocity is . Estimate its length with the two-point Gauss–Legendre rule, which averages the speed at .
A game moves a coin along a Bézier curve by adding to every frame. The curve's speed is near both ends and in the middle. What does the player see?
The hardest one here. Get it, and you've got this lesson.
A model train runs at a constant m/s along the track with control points , , and , in meters, starting at . Where is it s after it starts? Give the coordinates to 2 decimal places, then .
Where it's used
Games use arc-length tables whenever something has to move steadily along a spline: trains and moving platforms, cameras on rails, enemies on patrol routes and racing lines. Animation tools offer a "constant velocity" option on motion paths that does exactly this reparameterization. Text set along a curved path spaces its letters by arc length, and dashed strokes measure their dashes the same way. CNC machines and robot arms need it too: their feed rate is a speed along the tool path, so the controller turns distance into the curve's parameter thousands of times a second.