Game Development Reference
Figure 9.7 Rotating the last link using the Cylic Coordinate Descent technique for IK
Unfortunately, the angle does not imply direction and we are interested in
both the angle and the direction. Since the cross product of two vectors
gives a perpendicular vector, we can use this to determine the direction.
By extending the vectors from two to three dimensions, we then have a z
value. The sign of this z value will give the direction in which to rotate.
a =( a . x , a . y , 0) and b =( b . x , b . y , 0)
b =( a . y * 0- 0 * b . y , 0 * b . x - a . x * 0, a . x * b . y - a . y * b . x )
We are only interested in the sign of the third term. So the sign of
a . x * b . y - a . y * b . x
defines the direction of rotation.
When using the algorithm, we start by iterating through the link chain to
find the last link. We then create unit vectors from the pivot point of the link
to the chain end and from the pivot point to the target. The inverse cosine
of the dot product of these two vectors gives the angle of rotation and the
third term of the result of the three-value vector cross product gives the
direction of rotation. We generate a rotation matrix using this information
and record the current chain end position.