Forward versus inverse kinematics
Forward kinematics is straightforward. Given the two joint angles, the position of the hand follows from trigonometry: x = L₁cos θ₁ + L₂cos(θ₁+θ₂), y = L₁sin θ₁ + L₂sin(θ₁+θ₂). Two angles in, one position out.
Inverse kinematics runs the other way. Given a target position, what angles reach it? The equations are nonlinear, there is usually more than one answer, and some targets have none at all. That combination is why inverse kinematics is the harder half of the problem — and why real robots solve it numerically when the geometry gets complicated.
For a planar two-link arm, though, there is a closed form. The trick is to notice that the two links and the line from base to target form a triangle, and triangles obey the law of cosines.
The law of cosines solution
cos θ₂ = (r² − L₁² − L₂²) / (2·L₁·L₂)
θ₁ = atan2(y, x) − atan2(L₂ sin θ₂, L₁ + L₂ cos θ₂)
The first line is the distance from the base to the target. The second applies the law of cosines to the triangle whose sides are L₁, L₂ and r, giving the interior angle at the elbow. The third subtracts the offset that the forearm introduces, leaving the shoulder angle.
Note that cos θ₂ comes from an arccosine, which returns a value between 0 and π. That single-valued result is the elbow down solution. Negating θ₂ gives the mirror configuration, elbow up, which reaches the same point from the other side.
Two answers, both correct. Any reachable point inside the workspace has exactly two joint solutions, related by reflection across the base-to-target line. A real controller picks between them based on joint limits, obstacle avoidance or how far each joint must travel.
Worked example
Take L₁ = 5, L₂ = 3 and a target at (0, 4). The distance is r = 4, so cos θ₂ = (16 − 25 − 9)/(2·5·3) = −18/30 = −0.6. That gives θ₂ = 126.87°, an elbow bent well past a right angle.
For the shoulder, atan2(4, 0) = 90°. The offset term is atan2(3·sin 126.87°, 5 + 3·cos 126.87°) = atan2(2.4, 3.2) = 36.87°. Subtracting gives θ₁ = 53.13°, and the interior elbow angle is 180° − 126.87° = 53.13° as well.
| Quantity | Formula | Value |
|---|---|---|
| Reach r | √(x² + y²) | 4 |
| cos θ₂ | (r² − L₁² − L₂²)/(2L₁L₂) | −0.6 |
| θ₂ | arccos(−0.6) | 126.87° |
| atan2(y, x) | atan2(4, 0) | 90° |
| Offset | atan2(L₂ sin θ₂, L₁ + L₂ cos θ₂) | 36.87° |
| θ₁ | 90° − 36.87° | 53.13° |
Check it forward: x = 5cos 53.13° + 3cos 180° = 3 − 3 = 0, and y = 5sin 53.13° + 3sin 180° = 4 + 0 = 4. The target is hit exactly.
The workspace is an annulus
Not every point is reachable. The arm can extend to at most L₁ + L₂ and can fold to no less than |L₁ − L₂|. Between those two radii the reachable region is a ring, not a disc, and the inner hole is real.
| Region | Distance from base | Status |
|---|---|---|
| Outer limit | r > 8 | Unreachable — the arm is too short |
| Boundary | r = 8 | Fully extended, θ₂ = 0 |
| Workspace | 2 < r < 8 | Two solutions |
| Boundary | r = 2 | Fully folded, θ₂ = 180° |
| Inner hole | r < 2 | Unreachable — the arm cannot fold that tight |
The tool rejects out-of-reach targets with the specific distance it computed, rather than clamping to the boundary. Clamping would silently move your target, which is worse than an error message when you are debugging a robot.
Where inverse kinematics is used
- Industrial robots — a six-axis arm is solved as a chain of simpler sub-problems, often with a closed form for the first three joints.
- Animation — IK rigs place a character's foot on uneven ground by solving for the leg angles rather than keyframing them.
- Prosthetics — mapping an intended hand position to actuator angles in real time.
- Surgical robotics — the tool tip must reach a planned point through a small incision, with the arm constrained by the entry port.
- Game engines — two-bone IK is a standard node, and this formula is exactly what it runs.
Beyond two links the closed form disappears in general, and solvers switch to Jacobian-based iteration, which converges to one solution near a starting guess. The two-link case remains the clearest way to build intuition for what those solvers are doing.
Frequently asked questions
Why are there two solutions?
Because the elbow can bend either way. Folding the forearm clockwise or anticlockwise from the same shoulder direction reaches the same point, producing mirror-image configurations known as elbow up and elbow down.
What does the tool mean by out of reach?
The distance from the base to the target exceeds L₁ + L₂, so even a fully extended arm falls short. The error message reports both distances so you can see how far off the target is.
Why is there a hole in the middle of the workspace?
The arm cannot fold tighter than |L₁ − L₂|, because the links would have to pass through each other. Points closer than that to the base are unreachable no matter how the joints are set.
What is the difference between θ₂ and the interior elbow angle?
θ₂ is the joint angle measured from the straight-ahead extension of link 1, so 0° means a straight arm. The interior angle of the triangle is 180° − |θ₂|. Both are reported because different conventions are used in different fields.
Does this handle a three-link arm?
Not directly. Three links give a one-dimensional family of solutions for most targets, so you need an extra constraint — such as a preferred wrist angle — before the answer is unique. Two-link IK is the building block those solvers use.
What happens when the target is exactly at maximum reach?
cos θ₂ becomes exactly 1, so θ₂ = 0 and the arm is straight. This is a singular configuration: the Jacobian loses rank, and small changes in the target require large changes in joint angle.