This repository showcases the implementation of two popular Inverse Kinematics (IK) algorithms: Cyclic Coordinate Descent (CCD) and Forward and Backward Reaching Inverse Kinematics (FABRIK). These algorithms are commonly used in robotics, computer graphics and animation to solve the problem of positioning an end-effector for a skeleton (such as a robotic arm or a character's foot) at a desired location by manipulating its joint angles.
Inverse Kinematics algorithms iteratively adjust joint angles in a chain of bones to minimize the distance between the end-effector's current position and the target. Both the CCD and FABRIK algorithms support variable-length bone chains and use iterative methods. As a result, both algorithms may fail to converge if the target remains unreachable after a specified number of iterations.
CCD iteratively adjusts each joint starting from the end-effector and working backward towards the root. At each step, it modifies the angle of the current joint to minimize the distance between the end-effector and the target position.
- Implementation: src/CCD.h
FABRIK works by saving all joint-positions in a list and iteratively updating these in two passes:
-
Forward Pass: The end-effector (last joint) is placed at the target position, and each joint is adjusted to ensure it remains within reach of the connecting bone.
-
Backward Pass: The root joint (first joint) is placed back at the origin and each joint position is readjusted accordingly again.
After the positions are updated, the new bone angles are calculated based on the updated joint positions.
- Implementation: src/FABRIK.h
- Clone the Repository:
git clone --recurse-submodules https://github.com/chFleschutz/inverse-kinematics-algorithms.git
-
Ensure you have CMake installed or your IDE supports CMake (e.g. Visual Studio).
-
Generate the project files or open the folder directly in your IDE.
-
Build and run the
ik-console-demo
project for a small console-based demo.
-
Ensure Qt (Version 6.9.0 recommended) is installed.
-
Regenerate the project files after installing Qt.
-
Build and run the
ik-gui-demo
project.
To use the CCD or FABRIK solver:
- Include the headers:
#include "CCD.h"
#include "FABRIK.h"
- Create a
Skeleton
and add bones:
Skeleton skeleton;
skeleton.addBone(1.5f, glm::radians(30.0f));
skeleton.addBone(1.0f, glm::radians(30.0f));
skeleton.addBone(1.0f, glm::radians(30.0f));
- Create an
IKSolver
and callsolve
:
CCD solver;
int maxIterations = 100;
float precision = 0.01f;
glm::vec2 target{ 2.0f, 1.0f };
solver.solve(skeleton, target, maxIterations, precision);
Note: This project uses GLM for vector math (
glm::vec2
).
Check the demo folder for example usages of the CCD and FABRIK algorithms.