Closed-Form Inverse Kinematics in 3D by Building a One-Button Robot Gripper Game in MATLAB
An analytic solution for a 4-joint arm with a three finger gripper, verified against forward kinematics and implemented in under 10,000 characters
Introduction
Inverse kinematics sounds like a graduate topic, but you do it every time you reach for a cup. Your brain works out how far to turn your shoulder and bend your elbow so your hand lands on the cup. A robot has to do the same arithmetic. In KINEMATIX 3D, a one button game for the MATLAB One Button Game Mini Hack, that arithmetic becomes the game. You tap three times to choose where a three finger robot hand should go: once for direction, once for distance, and once for height. The game then solves the joint angles with a closed-form formula and moves the arm there. It closes the fingers around a glowing ball and drops it in a bin. I checked the formula against 20,000 random targets, and the hand landed within about 5x10^-16 meters of each one, which is about the rounding limit of double-precision arithmetic.
Demo

Play it: KINEMATIX 3D: Aim the Claw
The first tap locks the direction when the cyan laser points at the ball. The second tap locks the reach when the pink marker sits on the ball's floor ring. The third tap locks the height, and a live readout showed how far you are from the ball. After that, the arm solves, reaches, grips, and carries the ball to the bin, while a floating score pop-up and a screen flash report the result.
The arm in plain English
The arm has four joints: a base that turns, a shoulder, an elbow, and a wrist. The wrist is always adjusted so the hand points straight down, like a claw machine. The forward problem is easy: give me the joint angles and I can find where the hand is. The game needs the reverse: given the point you choose, find the joint angles.
Tips & Tricks
Trick 1: Turn first, then solve a flat problem
Every target has a direction around the base, a horizontal distance, and a height. Turning the base to face the target handles the direction. After that, the shoulder, elbow, and wrist all move in one vertical plane, so you only need the distance and height. In MATLAB, atan2 finds the direction, and it returns the four quadrant inverse tangent, so it keeps working when the target is behind or beside the base. hypot finds the distance, and it avoids underflow and overflow. Because the hand points down, the wrist has to sit one hand length above the grasp point.
Trick 2: Let a triangle find the elbow
The shoulder, elbow, and wrist form a triangle with three known sides: the two links and the straight line from shoulder to wrist. A triangle's angles are fixed by its sides, which is the law of cosines. In vector form it is a dot product: when two vectors meet at angle, the square of their sum grows with the cosine of that angle. Inverting it gives the elbow angle. It gives two answers, because a two link chain generally has two solutions, often called elbow up and elbow down. The game picks elbow up and it clamps the cosine to between -1 and 1 so acos never fails.
Once the elbow bend is known, the arm is a rigid shape. The direction to the wrist is the upper arm's direction plus a fixed offset angle, so the shoulder angle is the direction to the wrist minus that offset. This is a rotation matrix argument. The textbook two link derivation does it in the same order: the elbow angle is computed first, and then the shoulder angle. Finally, the three angles must add up to -90 degrees so the hand stays pointing down.
For a worked example, take the target (0.8,0.3,0.5). The solution is a yaw of 20.6 degrees, a shoulder of 70.8 degrees, an elbow of -103.2 degrees, and a wrist of -57.6 degrees. The live script below does this step by step.
Trick 3: Draw it in 3D
A figure is the fatest sanity check. A dashed line on the floor shows the target's distance and direction, and a vertical stem shows its height. The arm's links sit above them at the solved angles, and the short vertical hand shows the wrist condition.
Trick 4: Prove it with forward kinematics
The forward direction is easy: add up the links using rotation matricies. Feeding the inverse solution back through forward kinematics should return the original target. I tried this on 20,000 random targets in the game's aim box, and the target miss was about 5x10^-16 metres. For that box, the elbow cosine stayed between about -0.67 and 0.24, safely inside the legal range of -1 to 1, so every target is reachable.
Trick 5: Keep the animation smooth and flicker free
I learned four things the hard way:
- Create each graphics line once, then only update its coordinates every frame.
- Blend the joint angles with a smooth curve. The hand stays level automatically, because the three angles add up to -90 degrees at both ends and a straight blend keeps that sum, so the claw's path curves but never tilts.
- Detect a button press,not a hold, so one tap is one action.
- Never draw two coincident lines to fake a highlight. In 3D they fight over deoth and the arm shimmers, which my first version did while troubleshoiting the code.
Live script: the proof, the code, and 3D plots
run('KINEMATIX3D_Inversekinematics.m');
Concluding thoughts
Inverse kinematics does not always need an iterative solver. For this arm, a dot product and a rotation matrix give the elbow and shoulder in closed form, and forward kinematics verifies the answer to rounding precision. A one button game turns that algebra into something players can feel: each tap fixes one coordinate, and the solver does the rest.
The method has limits. It handles one elbow up branch, assumes a vertical grasp, and ignores joint limits. For general robots, Mathworks' Robotics System Toolbox goes further: its inverseKinematics solver is gradient descent based, and its documentation points to analyticalInverseKinematics for closed form solutions. Try changing the link lenghts or the aim box and rerun the live script. Or add the elbow down branch by flipping one sign.
References


