TExcelTAD
Home › Game Physics & Animation › Two-Bone IK

Two-Bone IK

Keyframed animation knows nothing about the world. A walk cycle authored on flat ground puts the feet in the air on stairs and inside the ground on slopes; a "reach for the lever" clip misses the lever by 10 cm when the character stands a little off. Inverse kinematics (IK) fixes the pose at runtime: given where the end of a chain should be (the foot, the hand), compute the joint rotations that put it there.

Forward kinematics is what Skeletons and Poses did: angles in, positions out. Inverse kinematics goes the other way: position in, angles out. For a general chain there are many solutions (or none), so solvers iterate, as FABRIK does. But the most common case in games has an exact, cheap answer: a two-bone chain (hip–knee–ankle, shoulder–elbow–wrist).

The triangle

Hip, knee and target form a triangle whose three sides are all known:

  • a = upper bone length (thigh),
  • b = lower bone length (shin),
  • d = distance from the root to the target.

The law of cosines gives every angle of a triangle from its sides:

text
angle at the root  = acos((a² + d² - b²) / (2ad))    between the upper bone and the root→target line
angle at the knee  = acos((a² + b² - d²) / (2ab))    between the two bones (180° = straight)

The upper bone points along atan2(target) rotated by the root angle, and the lower bone bends by 180° - knee angle relative to it. Two checks keep the solver robust:

  • Clamp d to [|a - b|, a + b]. A target beyond reach makes the acos argument leave [-1, 1] (NaN!). Clamped, the chain points straight at the target, as close as it can get.
  • Choose the bend side. The triangle can fold either way; a sign picks which.
cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>

const double PI = 3.141592653589793, DEG = PI / 180;
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
struct V { double x, y; };

// Two-bone IK in 2D. Root at the origin, bone lengths a (upper) and b (lower).
// bendSign = +1 or -1 picks which side the middle joint (elbow/knee) bends to.
struct Solution { double rootAngle, midAngle; V mid, end; };
Solution solve(V target, double a, double b, double bendSign) {
    double d = std::hypot(target.x, target.y);
    d = std::clamp(d, std::fabs(a - b), a + b);   // out of reach: get as close as possible
    // Law of cosines: interior angle at the root between the upper bone and the root->target line,
    // and the interior angle at the middle joint between the two bones.
    double atRoot = std::acos(std::clamp((a * a + d * d - b * b) / (2 * a * d), -1.0, 1.0));
    double atMid  = std::acos(std::clamp((a * a + b * b - d * d) / (2 * a * b), -1.0, 1.0));
    double toTarget = std::atan2(target.y, target.x);
    Solution s;
    s.rootAngle = toTarget + bendSign * atRoot;                    // absolute angle of the upper bone
    s.midAngle  = -bendSign * (PI - atMid);                        // lower bone relative to the upper bone
    s.mid = {a * std::cos(s.rootAngle), a * std::sin(s.rootAngle)};
    double lowerAbs = s.rootAngle + s.midAngle;
    s.end = {s.mid.x + b * std::cos(lowerAbs), s.mid.y + b * std::sin(lowerAbs)};
    return s;
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double a = 2.0, b = 1.5;                                  // thigh and shin
    std::cout << "reach " << std::fabs(a - b) << " .. " << a + b << "\n";
    for (V target : {V{2.5, 1.0}, V{0.0, 3.0}, V{1.0, -0.5}, V{5.0, 2.0}, V{0.2, 0.1}}) {
        Solution s = solve(target, a, b, +1);
        double err = std::hypot(s.end.x - target.x, s.end.y - target.y);
        std::cout << "target (" << target.x << ", " << std::setw(6) << target.y << "): root " << std::setw(8) << z0(s.rootAngle / DEG)
                  << " deg, joint " << std::setw(8) << z0(s.midAngle / DEG) << " deg, end (" << z0(s.end.x) << ", "
                  << std::setw(6) << z0(s.end.y) << "), miss " << err << "\n";
    }
}
reach 0.500 .. 3.500
target (2.500,  1.000): root   55.120 deg, joint  -80.406 deg, end (2.500,  1.000), miss 0.000
target (0.000,  3.000): root  116.384 deg, joint  -62.720 deg, end (0.000,  3.000), miss 0.000
target (1.000, -0.500): root   21.305 deg, joint -146.443 deg, end (1.000, -0.500), miss 0.000
target (5.000,  2.000): root   21.801 deg, joint    0.000 deg, end (3.250,  1.300), miss 1.885
target (0.200,  0.100): root   26.565 deg, joint -180.000 deg, end (0.447,  0.224), miss 0.276

The first three targets are reached exactly (the miss is 0). The fourth, at distance 5.4, is beyond the 3.5 reach: the leg straightens (joint angle 0°) and points at the target, missing by 1.885. The last target, closer than |a - b|, folds the leg completely (-180°). Real feet never get that close to their hip, but tools and debug targets do, so the clamp must be there.

The pole vector: which way the knee points

In 2D, the bend side is a sign. In 3D the knee can point anywhere on a circle around the hip–foot line, and a pole vector (pole target, hint, swivel) picks the plane: the knee bends toward a point placed in front of the character (in front of the knee for legs, behind the elbow for arms). The solver builds the triangle in the plane containing the root, the target and the pole.

In 2D the same idea reduces to "which side of the root→target line is the pole?", answered by the sign of a 2D cross product:

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

const double PI = 3.141592653589793, DEG = PI / 180;
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}; }

// Two-bone IK in 2D. Root at the origin, bone lengths a (upper) and b (lower).
// bendSign = +1 or -1 picks which side the middle joint (elbow/knee) bends to.
struct Solution { double rootAngle, midAngle; V mid, end; };
Solution solve(V target, double a, double b, double bendSign) {
    double d = std::hypot(target.x, target.y);
    d = std::clamp(d, std::fabs(a - b), a + b);   // out of reach: get as close as possible
    // Law of cosines: interior angle at the root between the upper bone and the root->target line,
    // and the interior angle at the middle joint between the two bones.
    double atRoot = std::acos(std::clamp((a * a + d * d - b * b) / (2 * a * d), -1.0, 1.0));
    double atMid  = std::acos(std::clamp((a * a + b * b - d * d) / (2 * a * b), -1.0, 1.0));
    double toTarget = std::atan2(target.y, target.x);
    Solution s;
    s.rootAngle = toTarget + bendSign * atRoot;                    // absolute angle of the upper bone
    s.midAngle  = -bendSign * (PI - atMid);                        // lower bone relative to the upper bone
    s.mid = {a * std::cos(s.rootAngle), a * std::sin(s.rootAngle)};
    double lowerAbs = s.rootAngle + s.midAngle;
    s.end = {s.mid.x + b * std::cos(lowerAbs), s.mid.y + b * std::sin(lowerAbs)};
    return s;
}
double cross(V a, V b) { return a.x * b.y - a.y * b.x; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double thigh = 2.0, shin = 1.5;
    V hip{0, 0}, foot{0.3, -3.2};          // a leg hanging down, the foot slightly forward (+x)
    for (double sign : {+1.0, -1.0}) {
        Solution s = solve(foot, thigh, shin, sign);
        std::cout << "bendSign " << (sign > 0 ? "+1" : "-1") << ": knee at (" << std::setw(6) << z0(s.mid.x) << ", " << s.mid.y << ")\n";
    }
    // The pole: a point the knee should point toward (in front of the character).
    for (V pole : {V{2.0, -1.5}, V{-2.0, -1.5}}) {
        double sign = cross(foot - hip, pole - hip) > 0 ? +1 : -1;  // which side of the hip->foot line?
        Solution s = solve(foot, thigh, shin, sign);
        std::cout << "pole (" << std::setw(6) << pole.x << ", " << pole.y << ") -> knee at (" << std::setw(6) << z0(s.mid.x)
                  << ", " << s.mid.y << "), foot reached at (" << z0(s.end.x) << ", " << s.end.y << ")\n";
    }
}
bendSign +1: knee at ( 0.857, -1.807)
bendSign -1: knee at (-0.506, -1.935)
pole ( 2.000, -1.500) -> knee at ( 0.857, -1.807), foot reached at (0.300, -3.200)
pole (-2.000, -1.500) -> knee at (-0.506, -1.935), foot reached at (0.300, -3.200)

Both solutions put the foot exactly on the target, but only one of them is a knee; the other is the leg of a flamingo walking backwards. Moving the pole to the other side flips the bend. In 3D rigs the pole is usually a control object placed in front of the knee, or computed from the animation's own knee position before IK, so the IK keeps the animator's knee direction.

Foot placement, the classic use

Ground-adapting feet run every frame after the animation pose is computed:

  1. Cast a ray (or a sphere) down from above each animated foot to find the ground (Raycasts, Shape Casts and Overlaps).
  2. Move the IK target to the hit point (plus the ankle height), and rotate the foot to the ground normal.
  3. Lower the pelvis by the larger of the two foot offsets, otherwise the leg on the lower step can't reach and stays straight.
  4. Solve two-bone IK for each leg, then blend the IK result in with a weight (fade it out while the foot is in the air, and during jumps).

Hands on weapons, ledges and doors use the same solver, usually with the target taken from a socket on the other object.

In engines

Engine Two-bone IK
Unity Animation Rigging package: TwoBoneIKConstraint (root, mid, tip, target, hint) on a Rig driven by a RigBuilder; for humanoid avatars also Animator.SetIKPosition / SetIKPositionWeight in OnAnimatorIK
Unreal the Two Bone IK node in the AnimGraph, Control Rig's Basic IK node, and IK Rig assets (with goals and solvers, also used for retargeting)
Godot 4.6 TwoBoneIK3D, part of the new IK framework built on SkeletonModifier3D (see FABRIK)

Test yourself

Which formula gives the knee angle of a two-bone chain with bone lengths a, b and root-to-target distance d?

  1. atan2(d, a + b)
  2. acos((a² + b² - d²) / (2ab))
  3. asin(d / (a + b))
  4. (a + b) / d

Why must d be clamped to [|a - b|, a + b] before solving?

  1. For performance
  2. Outside that range no triangle exists and the acos argument leaves [-1, 1], producing NaN
  3. To keep the knee bending forward
  4. Because quaternions require it

What does the pole vector control?

  1. The length of the bones
  2. The plane the chain bends in, so the knee or elbow points the right way
  3. The blend weight of IK
  4. The speed of the solver