The following video demos a system for making geometry based constraints for a CCD solution, based on some of the notes and ideas presented by Jonathan Blow here. In the clip, an arm with 15 constrained ball joints tracks a target placed by the mouse. Each joint is constrained by a polygonal geometry approximating a circle, but any convex 2D shape could have been used.
IK links and constraints can be represented by the following simple structures.
After a link’s transforms are set by an iteration of CCD, a link’s
rotation may violate the constraint and must be rotated back to the constraint primitive. In this scheme, the final and first vertexes of a constraint primitive are connected so that a constraint always forms a loop.
The closest-point math is the result of a pen and paper calculation and could stand a bit of optimization. I’ve left it the way it is to make it a bit easier to see what’s going on.
In the last line, the quaternion constructor
quat(next, limit) is the minimal rotation from the vector
limit, something like what Blow presents near the end of his article.