TExcelTAD

FABRIK

Two-Bone IK has an exact formula, but only for two bones. Spines, tails, tentacles, necks, robot arms and fingers have more joints, and there are infinitely many poses that reach a target. FABRIK (Forward And Backward Reaching Inverse Kinematics, Aristidou and Lasenby, 2011) is the iterative solver most game programmers reach for first: it is short, fast, works on positions only and converges in a few iterations.

The algorithm

The chain is a list of joint positions p0 … pn (p0 is the fixed root, pn the end effector) and the segment lengths between them. Each iteration has two passes:

  1. Backward (from the end to the root): put the end effector on the target. Then, for each joint going back toward the root, place it on the line toward its old position, exactly one segment length from the joint after it. The root has now moved away from where it belongs.
  2. Forward (from the root to the end): put the root back on its anchor, and place every following joint on the line toward its current position, one segment length from the joint before it. The end effector has now moved slightly away from the target.

Repeat until the end effector is within a tolerance of the target, or a maximum number of iterations is used. Each pass keeps every segment length exact; only the "end on target" or "root on anchor" condition is broken, and less so every iteration.

Before iterating, check reach: if the target is farther than the sum of the lengths, no iteration can help. Stretch the chain straight toward it instead (one pass, done).

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

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
struct V { double x, y; };
V operator+(V a, V b) { return {a.x + b.x, a.y + b.y}; }
V operator-(V a, V b) { return {a.x - b.x, a.y - b.y}; }
V operator*(V v, double s) { return {v.x * s, v.y * s}; }
double len(V v) { return std::hypot(v.x, v.y); }

// Put `p` on the line toward `from`, exactly `length` away from `from`.
V placeAt(V from, V toward, double length) { V d = toward - from; return from + d * (length / len(d)); }

// FABRIK: returns the number of iterations used. joints[0] is the fixed root.
int fabrik(std::vector<V>& j, const std::vector<double>& lengths, V target, double tolerance, int maxIter, bool verbose) {
    V root = j[0];
    double reach = 0; for (double l : lengths) reach += l;
    size_t n = j.size();
    if (len(target - root) >= reach) {                     // unreachable: straighten toward the target
        for (size_t i = 1; i < n; i++) j[i] = placeAt(j[i - 1], target, lengths[i - 1]);
        return 0;
    }
    for (int iter = 1; iter <= maxIter; iter++) {
        j[n - 1] = target;                                 // backward pass: from the end to the root
        for (size_t i = n - 1; i-- > 0;) j[i] = placeAt(j[i + 1], j[i], lengths[i]);
        j[0] = root;                                       // forward pass: from the root to the end
        for (size_t i = 1; i < n; i++) j[i] = placeAt(j[i - 1], j[i], lengths[i - 1]);
        double err = len(j[n - 1] - target);
        if (verbose) std::cout << "  iteration " << iter << ": end misses by " << err << "\n";
        if (err <= tolerance) return iter;
    }
    return maxIter;
}

void print(const std::vector<V>& j) {
    std::cout << "  joints:";
    for (V p : j) std::cout << " (" << z0(p.x) << ", " << z0(p.y) << ")";
    std::cout << "\n";
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    std::vector<double> lengths = {1, 1, 1, 1};            // four segments, reach 4
    std::vector<V> chain = {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}};
    std::cout << "target (1.5, 2.5):\n";
    int used = fabrik(chain, lengths, {1.5, 2.5}, 0.001, 20, true);
    std::cout << "  solved in " << used << " iterations\n";
    print(chain);
    std::cout << "target (6, 1), out of reach:\n";
    fabrik(chain, lengths, {6, 1}, 0.001, 20, false);
    print(chain);
}
target (1.5, 2.5):
  iteration 1: end misses by 0.065
  iteration 2: end misses by 0.012
  iteration 3: end misses by 0.003
  iteration 4: end misses by 0.001
  solved in 4 iterations
  joints: (0.000, 0.000) (0.994, 0.106) (1.852, 0.620) (1.964, 1.613) (1.500, 2.500)
target (6, 1), out of reach:
  joints: (0.000, 0.000) (0.986, 0.164) (1.973, 0.329) (2.959, 0.493) (3.946, 0.658)

The error drops by roughly a factor of four each iteration, and four iterations get within a millimetre (if the units are metres). The out-of-reach target straightens the chain along the root-to-target direction: each joint is exactly 1 apart on the line toward (6, 1).

How many iterations?

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

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
struct V { double x, y; };
V operator+(V a, V b) { return {a.x + b.x, a.y + b.y}; }
V operator-(V a, V b) { return {a.x - b.x, a.y - b.y}; }
V operator*(V v, double s) { return {v.x * s, v.y * s}; }
double len(V v) { return std::hypot(v.x, v.y); }

// Put `p` on the line toward `from`, exactly `length` away from `from`.
V placeAt(V from, V toward, double length) { V d = toward - from; return from + d * (length / len(d)); }

// FABRIK: returns the number of iterations used. joints[0] is the fixed root.
int fabrik(std::vector<V>& j, const std::vector<double>& lengths, V target, double tolerance, int maxIter, bool verbose) {
    V root = j[0];
    double reach = 0; for (double l : lengths) reach += l;
    size_t n = j.size();
    if (len(target - root) >= reach) {                     // unreachable: straighten toward the target
        for (size_t i = 1; i < n; i++) j[i] = placeAt(j[i - 1], target, lengths[i - 1]);
        return 0;
    }
    for (int iter = 1; iter <= maxIter; iter++) {
        j[n - 1] = target;                                 // backward pass: from the end to the root
        for (size_t i = n - 1; i-- > 0;) j[i] = placeAt(j[i + 1], j[i], lengths[i]);
        j[0] = root;                                       // forward pass: from the root to the end
        for (size_t i = 1; i < n; i++) j[i] = placeAt(j[i - 1], j[i], lengths[i - 1]);
        double err = len(j[n - 1] - target);
        if (verbose) std::cout << "  iteration " << iter << ": end misses by " << err << "\n";
        if (err <= tolerance) return iter;
    }
    return maxIter;
}

int main() {
    std::cout << std::fixed << std::setprecision(4);
    std::vector<double> lengths = {1, 1, 1, 1};
    const std::vector<V> straight = {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}};

    std::cout << "tolerance -> iterations (target (1.5, 2.5))\n";
    for (double tol : {0.1, 0.01, 0.001, 0.0001}) {
        std::vector<V> chain = straight;
        std::cout << "  " << tol << " -> " << fabrik(chain, lengths, {1.5, 2.5}, tol, 50, false) << "\n";
    }
    std::cout << std::setprecision(2) << "target -> iterations (tolerance 0.001, from the straight pose)\n";
    for (V t : {V{0.5, 0.2}, V{-1, 0.5}, V{1.5, 2.5}, V{3.5, 1}, V{-2, -1}}) {
        std::vector<V> chain = straight;
        int used = fabrik(chain, lengths, t, 0.001, 50, false);
        std::cout << "  (" << std::setw(5) << t.x << ", " << std::setw(5) << t.y << ") distance " << len(t) << " -> " << used << "\n";
    }
}
tolerance -> iterations (target (1.5, 2.5))
  0.1000 -> 1
  0.0100 -> 3
  0.0010 -> 4
  0.0001 -> 6
target -> iterations (tolerance 0.001, from the straight pose)
  ( 0.50,  0.20) distance 0.54 -> 4
  (-1.00,  0.50) distance 1.12 -> 2
  ( 1.50,  2.50) distance 2.92 -> 4
  ( 3.50,  1.00) distance 3.64 -> 5
  (-2.00, -1.00) distance 2.24 -> 17

Every extra digit of tolerance costs one or two iterations, so a fixed budget of about 10 iterations with a tolerance near a millimetre is the usual setting. Targets that require the chain to fold back past its root, like (-2, -1) here, converge much more slowly (17 iterations). In a game, the previous frame's solution is the starting pose for this frame, and targets rarely jump across the root, so the budget is almost never exhausted.

Turning positions into rotations

FABRIK outputs positions, but a skeleton stores rotations. After solving, walk the chain from the root: for each bone, find the rotation that turns its old direction (from the animated pose) into the new direction p[i+1] - p[i], and apply it to the bone's local rotation. In 3D that is the shortest-arc rotation between two vectors, or a look rotation if the twist around the bone also matters.

Constraints

An unconstrained FABRIK chain bends anywhere, which looks fine for a tentacle and wrong for a knee or a spine. FABRIK's strength is that constraints slot into the passes: after placing each joint, project it back into the allowed region relative to the previous segment. Typical constraints:

  • a hinge (elbow, knee): keep the joint in a plane, and limit the bend angle;
  • a cone (shoulder, neck): keep the segment within a maximum angle of the parent's direction;
  • twist limits, applied when converting back to rotations.

With constraints the solver may need more iterations and can settle on a pose that misses the target slightly. That is usually the right answer: a neck that can't turn further shouldn't.

Follow the pointer

The demo runs FABRIK on a six-segment chain anchored at the bottom. Move the pointer over the canvas (or drag on a touch screen); without input, the target drifts along a figure eight. The dashed circle is the chain's reach: outside it the target turns red and the chain straightens toward it. The counter shows how many iterations the solver used this frame.

html · try it yourself
<style>
  html, body { margin: 0; height: 100%; background: #0e1018; overflow: hidden; }
  canvas { display: block; margin: 0 auto; width: min(100vw, 177.78vh); height: auto; outline: none; touch-action: none; }
</style>
<canvas width="480" height="270"></canvas>
<script>
const cv = document.querySelector('canvas'), ctx = cv.getContext('2d');
const W = cv.width, H = cv.height;
const SEGMENTS = 6, LEN = 30, REACH = SEGMENTS * LEN, TOLERANCE = 0.1, MAX_ITER = 20;
const root = { x: W / 2, y: H - 20 };
let joints = Array.from({ length: SEGMENTS + 1 }, (_, i) => ({ x: root.x, y: root.y - i * LEN }));
let target = { x: 330, y: 120 }, pointerInside = false, iterations = 0, miss = 0, reachable = true;

function placeAt(from, toward, length) {
  const dx = toward.x - from.x, dy = toward.y - from.y, d = Math.hypot(dx, dy) || 1e-9;
  return { x: from.x + dx * length / d, y: from.y + dy * length / d };
}

function solve() {
  const dist = Math.hypot(target.x - root.x, target.y - root.y);
  reachable = dist < REACH;
  iterations = 0;
  if (!reachable) {                                   // straighten toward the target
    joints[0] = { ...root };
    for (let i = 1; i <= SEGMENTS; i++) joints[i] = placeAt(joints[i - 1], target, LEN);
    miss = dist - REACH;
    return;
  }
  for (let it = 1; it <= MAX_ITER; it++) {
    joints[SEGMENTS] = { ...target };                 // backward pass
    for (let i = SEGMENTS - 1; i >= 0; i--) joints[i] = placeAt(joints[i + 1], joints[i], LEN);
    joints[0] = { ...root };                          // forward pass
    for (let i = 1; i <= SEGMENTS; i++) joints[i] = placeAt(joints[i - 1], joints[i], LEN);
    iterations = it;
    miss = Math.hypot(joints[SEGMENTS].x - target.x, joints[SEGMENTS].y - target.y);
    if (miss <= TOLERANCE) break;
  }
}

function draw() {
  ctx.fillStyle = '#0e1018'; ctx.fillRect(0, 0, W, H);
  ctx.fillStyle = '#263238'; ctx.fillRect(0, root.y, W, H - root.y);
  ctx.setLineDash([4, 6]); ctx.strokeStyle = '#37474f'; ctx.lineWidth = 1;
  ctx.beginPath(); ctx.arc(root.x, root.y, REACH, 0, Math.PI * 2); ctx.stroke(); ctx.setLineDash([]);
  ctx.strokeStyle = '#4fc3f7'; ctx.lineWidth = 6; ctx.lineCap = 'round'; ctx.lineJoin = 'round';
  ctx.beginPath(); ctx.moveTo(joints[0].x, joints[0].y);
  for (const j of joints) ctx.lineTo(j.x, j.y);
  ctx.stroke();
  joints.forEach((j, i) => {
    ctx.fillStyle = i === 0 ? '#90a4ae' : '#e1f5fe';
    ctx.beginPath(); ctx.arc(j.x, j.y, i === 0 ? 7 : 4, 0, Math.PI * 2); ctx.fill();
  });
  ctx.strokeStyle = reachable ? '#ffca28' : '#ef5350'; ctx.lineWidth = 2;
  ctx.beginPath();
  ctx.moveTo(target.x - 8, target.y); ctx.lineTo(target.x + 8, target.y);
  ctx.moveTo(target.x, target.y - 8); ctx.lineTo(target.x, target.y + 8);
  ctx.stroke();
  ctx.fillStyle = '#fff'; ctx.font = '14px system-ui, sans-serif'; ctx.textAlign = 'left';
  ctx.fillText(reachable ? `iterations: ${iterations}   miss: ${miss.toFixed(2)} px`
                         : `out of reach by ${miss.toFixed(0)} px: chain straightened`, 10, 22);
  ctx.fillStyle = '#90a4ae';
  ctx.fillText(pointerInside ? 'following the pointer' : 'move the pointer over the canvas', 10, 42);
}

function toCanvas(e) {
  const r = cv.getBoundingClientRect();
  return { x: (e.clientX - r.left) * W / r.width, y: (e.clientY - r.top) * H / r.height };
}
cv.addEventListener('pointermove', (e) => { pointerInside = true; target = toCanvas(e); });
cv.addEventListener('pointerdown', (e) => { pointerInside = true; target = toCanvas(e); });
cv.addEventListener('pointerleave', () => { pointerInside = false; });

requestAnimationFrame(function frame(now) {
  if (!pointerInside) {                               // idle: drift along a figure eight
    const t = now / 1000;
    target = { x: root.x + 170 * Math.sin(t * 0.7), y: 120 + 70 * Math.sin(t * 1.4) };
  }
  solve();
  draw();
  requestAnimationFrame(frame);
});
</script>

Things to try: move the target close to the root (the chain coils up), drag it across the root from one side to the other (watch the iteration count rise briefly), and pull it far outside the circle.

FABRIK, CCD and the engines

CCD (Cyclic Coordinate Descent) is the other classic iterative solver: it rotates one joint at a time, from the end to the root, to point the end effector at the target. It is just as short to write, but tends to curl the joints near the end first and can look unnatural; FABRIK spreads the bend more evenly. Jacobian-based solvers handle many goals at once and are used for full-body IK.

Engine Multi-bone IK
Unity Animation Rigging: ChainIKConstraint (an iterative solver for longer chains), plus MultiAimConstraint for look-at
Unreal Control Rig's FABRIK and CCDIK nodes, AnimGraph FABRIK and CCDIK nodes, and Full Body IK in Control Rig / IK Rig
Godot 4.6 a new IK framework on SkeletonModifier3D: the base class IKModifier3D, with TwoBoneIK3D, SplineIK3D, FABRIK3D, CCDIK3D and JacobianIK3D

Note: Godot's older SkeletonIK3D node (deprecated) predates this framework; on Godot 4.6 and later, prefer the IKModifier3D family. Its documentation was still being written when 4.6 shipped, so check the class reference for your version.

Test yourself

What does the backward pass of FABRIK do?

  1. Rotates each joint toward the target, end first
  2. Puts the end effector on the target and pulls each joint toward it, keeping segment lengths
  3. Moves the root to the target
  4. Computes the Jacobian

The target is farther from the root than the sum of the segment lengths. What should FABRIK do?

  1. Iterate until the maximum iteration count
  2. Stretch the segments
  3. Straighten the chain along the root-to-target direction and stop
  4. Return the previous pose

How are joint constraints usually added to FABRIK?

  1. They cannot be
  2. After placing each joint in a pass, project it back into its allowed region relative to the previous segment
  3. By running CCD afterwards
  4. By shortening the bones