TExcelTAD
Home › Math for Games › Bézier Curves

Bézier Curves

A Bézier curve is a smooth path defined by a few control points. The curve starts at the first point, ends at the last, and is pulled toward the ones in between without (usually) touching them. Vector drawing tools, font outlines, CSS cubic-bezier() easings, animation curve editors, roads and rails in level editors, and projectile arcs all use them.

Built from lerps

The beautiful part: a Bézier curve is nothing but repeated lerp (de Casteljau's algorithm). For a quadratic curve with points P0, P1, P2:

  1. A = lerp(P0, P1, t) and B = lerp(P1, P2, t)
  2. the curve point is lerp(A, B, t)

A cubic curve (four points, the most common kind) adds one more level. Expanding the lerps gives the familiar polynomial forms:

text
quadratic: B(t) = (1-t)² P0 + 2(1-t)t P1 + t² P2
cubic:     B(t) = (1-t)³ P0 + 3(1-t)²t P1 + 3(1-t)t² P2 + t³ P3
cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec2 { double x, y; };
Vec2 lerp(Vec2 a, Vec2 b, double t) { return {a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t}; }

Vec2 cubicDeCasteljau(Vec2 p0, Vec2 p1, Vec2 p2, Vec2 p3, double t) {
    Vec2 a = lerp(p0, p1, t), b = lerp(p1, p2, t), c = lerp(p2, p3, t);
    Vec2 d = lerp(a, b, t), e = lerp(b, c, t);
    return lerp(d, e, t);
}

Vec2 cubicPolynomial(Vec2 p0, Vec2 p1, Vec2 p2, Vec2 p3, double t) {
    double u = 1 - t;
    double w0 = u * u * u, w1 = 3 * u * u * t, w2 = 3 * u * t * t, w3 = t * t * t;
    return {w0 * p0.x + w1 * p1.x + w2 * p2.x + w3 * p3.x, w0 * p0.y + w1 * p1.y + w2 * p2.y + w3 * p3.y};
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 p0{0, 0}, p1{0, 4}, p2{6, 4}, p3{6, 0};       // an arch
    for (int i = 0; i <= 4; i++) {
        double t = i / 4.0;
        Vec2 a = cubicDeCasteljau(p0, p1, p2, p3, t), b = cubicPolynomial(p0, p1, p2, p3, t);
        std::cout << "t=" << t << "  (" << a.x << ", " << a.y << ")  polynomial (" << b.x << ", " << b.y << ")\n";
    }
}
t=0.000  (0.000, 0.000)  polynomial (0.000, 0.000)
t=0.250  (0.938, 2.250)  polynomial (0.938, 2.250)
t=0.500  (3.000, 3.000)  polynomial (3.000, 3.000)
t=0.750  (5.062, 2.250)  polynomial (5.062, 2.250)
t=1.000  (6.000, 0.000)  polynomial (6.000, 0.000)

Both forms give the same points. The curve rises to a height of 3, not 4: the control points pull, but the curve doesn't reach them.

Play with it

Drag the four control points. The faint lines show the de Casteljau construction for the t on the slider.

html · try it yourself
<canvas id="c" width="380" height="240" style="background:#fafafa;border:1px solid #ddd;display:block;max-width:100%;touch-action:none"></canvas>
<div style="font:13px system-ui;color:#555;margin-top:4px">t <input id="t" type="range" min="0" max="1" step="0.01" value="0.4"> <span id="tv"></span></div>
<script>
  const c = document.getElementById('c'), ctx = c.getContext('2d');
  const slider = document.getElementById('t'), tv = document.getElementById('tv');
  const P = [{ x: 40, y: 200 }, { x: 90, y: 40 }, { x: 290, y: 40 }, { x: 340, y: 200 }];
  const lerp = (a, b, t) => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
  function point(t) {
    const a = lerp(P[0], P[1], t), b = lerp(P[1], P[2], t), d = lerp(P[2], P[3], t);
    const e = lerp(a, b, t), f = lerp(b, d, t);
    return { a, b, d, e, f, p: lerp(e, f, t) };
  }
  function line(pts, color) { ctx.strokeStyle = color; ctx.beginPath(); pts.forEach((q, i) => (i ? ctx.lineTo(q.x, q.y) : ctx.moveTo(q.x, q.y))); ctx.stroke(); }
  function draw() {
    const t = Number(slider.value);
    ctx.clearRect(0, 0, c.width, c.height);
    line(P, '#bbb');
    const curve = []; for (let i = 0; i <= 60; i++) curve.push(point(i / 60).p);
    ctx.lineWidth = 3; line(curve, '#7e57c2'); ctx.lineWidth = 1;
    const s = point(t);
    line([s.a, s.b, s.d], '#90caf9'); line([s.e, s.f], '#26a69a');
    ctx.fillStyle = '#e53935'; ctx.beginPath(); ctx.arc(s.p.x, s.p.y, 6, 0, 7); ctx.fill();
    P.forEach((q, i) => { ctx.fillStyle = '#333'; ctx.fillRect(q.x - 5, q.y - 5, 10, 10); ctx.fillText('P' + i, q.x + 8, q.y - 8); });
    tv.textContent = t.toFixed(2);
  }
  let drag = -1;
  const pos = (e) => { const r = c.getBoundingClientRect(); return { x: (e.clientX - r.left) * c.width / r.width, y: (e.clientY - r.top) * c.height / r.height }; };
  c.addEventListener('pointerdown', (e) => { const m = pos(e); drag = P.findIndex((q) => Math.hypot(q.x - m.x, q.y - m.y) < 12); });
  c.addEventListener('pointermove', (e) => { if (drag >= 0) { P[drag] = pos(e); draw(); } });
  window.addEventListener('pointerup', () => { drag = -1; });
  slider.addEventListener('input', draw);
  draw();
</script>

Tangents

The derivative of the curve tells you the direction of travel at t, which is what a car or a camera following the curve should face. For a cubic:

text
B'(t) = 3(1-t)² (P1 - P0) + 6(1-t)t (P2 - P1) + 3t² (P3 - P2)

At t = 0 the tangent points from P0 toward P1, and at t = 1 from P2 toward P3. That is why, to join two curves smoothly, the last control point of one, the shared end point, and the first control point of the next must lie on one straight line.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>

struct Vec2 { double x, y; };

Vec2 cubicTangent(Vec2 p0, Vec2 p1, Vec2 p2, Vec2 p3, double t) {
    double u = 1 - t;
    double a = 3 * u * u, b = 6 * u * t, c = 3 * t * t;
    return {a * (p1.x - p0.x) + b * (p2.x - p1.x) + c * (p3.x - p2.x),
            a * (p1.y - p0.y) + b * (p2.y - p1.y) + c * (p3.y - p2.y)};
}

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 p0{0, 0}, p1{0, 4}, p2{6, 4}, p3{6, 0};
    for (double t : {0.0, 0.5, 1.0}) {
        Vec2 d = cubicTangent(p0, p1, p2, p3, t);
        double len = std::hypot(d.x, d.y);
        std::cout << "t=" << t << " direction (" << z0(d.x / len) << ", " << z0(d.y / len) << "), speed " << len << "\n";
    }
}
t=0.000 direction (0.000, 1.000), speed 12.000
t=0.500 direction (1.000, 0.000), speed 9.000
t=1.000 direction (0.000, -1.000), speed 12.000

The arch starts going straight up, moves horizontally at the top, and ends going straight down.

Constant speed along a curve

Notice the speed (the tangent's length) changes with t: equal steps in t are not equal steps in distance. For a train on a track that must move at a constant speed, build an arc-length table: sample the curve finely, accumulate the distance, and look up the t for each distance.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

struct Vec2 { double x, y; };
Vec2 cubic(Vec2 p0, Vec2 p1, Vec2 p2, Vec2 p3, double t) {
    double u = 1 - t, w0 = u * u * u, w1 = 3 * u * u * t, w2 = 3 * u * t * t, w3 = t * t * t;
    return {w0 * p0.x + w1 * p1.x + w2 * p2.x + w3 * p3.x, w0 * p0.y + w1 * p1.y + w2 * p2.y + w3 * p3.y};
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 p0{0, 0}, p1{0, 4}, p2{6, 4}, p3{6, 0};
    const int N = 200;
    std::vector<double> dist(N + 1, 0);              // dist[i] = arc length up to t = i/N
    Vec2 prev = p0;
    for (int i = 1; i <= N; i++) {
        Vec2 p = cubic(p0, p1, p2, p3, double(i) / N);
        dist[i] = dist[i - 1] + std::hypot(p.x - prev.x, p.y - prev.y);
        prev = p;
    }
    double total = dist[N];
    std::cout << "curve length ~ " << total << "\n";

    for (int k = 0; k <= 4; k++) {                   // 5 evenly spaced points by distance
        double want = total * k / 4;
        int i = 0;
        while (i < N && dist[i + 1] < want) i++;
        double seg = dist[i + 1] - dist[i];
        double f = seg > 0 ? (want - dist[i]) / seg : 0;
        double t = (i + f) / N;
        Vec2 p = cubic(p0, p1, p2, p3, t);
        std::cout << "distance " << want << " -> t=" << t << " at (" << p.x << ", " << p.y << ")\n";
    }
}
curve length ~ 9.515
distance 0.000 -> t=0.000 at (0.000, 0.000)
distance 2.379 -> t=0.235 at (0.838, 2.157)
distance 4.757 -> t=0.500 at (3.000, 3.000)
distance 7.136 -> t=0.765 at (5.162, 2.157)
distance 9.515 -> t=1.000 at (6.000, 0.000)

The t values for even distances are not evenly spaced. Engines' spline components do this for you: Unity's Splines package (SplineUtility.GetPointAtLinearDistance), Unreal's USplineComponent::GetLocationAtDistanceAlongSpline, Godot's Curve3D.sample_baked.

Test yourself

Which points does a cubic Bézier curve always pass through?

  1. All four control points
  2. Only the first and last control points
  3. Only the middle two
  4. None of them

What is de Casteljau's algorithm built from?

  1. Matrix inverses
  2. Repeated lerps between neighboring points
  3. Square roots
  4. Random numbers

Why do equal steps in t not give equal distances along the curve?

  1. Because of floating-point rounding
  2. The curve's speed (tangent length) varies with t
  3. Because t must be squared
  4. They do give equal distances