For inverse kinematics (IK), the position of the end point is known, and we need to find the angles of the joints. This is a much harder problem, there may be many possible answers, or there may not be a set of angles that would reach to that point. Although this is hard to do, it is useful, for example, when posing a model it is useful to be able to drag the extremities with a mouse and have the arms/links position themselves appropriately.
There are two approaches to solving inverse kinematics:
- Analytical - requires a lot of trigonometry or matrix algebra
- Iterative - better if there are lots of links and degrees of freedom.
IK - Analytical approach
If there are only two or three links then it may be possible to solve it analytically. One possibly might be to draw out the arm with the angles shown on it, then solve for the angles using geometry, a good knowledge of trig identities would help here. The problem is that this is not really very general approach.
Another analytical approach is to represent each links rotation and translation by a matrix. The end point is then given by all these matrixes multiplied together, so we just need to solve this matrix equation. Then find what rotation each matrix represents.
There may be many solutions or there may not be any solutions. In other words there are lots of ways to reach to a given point, or it may be out of reach.
If there are many solutions, then you might need to apply additional constraints. For instance, human joints can only bend within certain limits.
IK - Iterative approach
This is a more general approach for programming complex chains.
Start off with the joints in any position, then move each of the joints in turn, so that each movement takes the endpoint toward the target.
Starting with the joint nearest the end point, rotate the joint so that the current end point moves toward the required end point. Then do the same with the next joint toward the base and so on until the base is rotated. Then keep repeating this, until the end point is close enough to the required end point or if further iterations are not moving it closer to the required point.
It may be possible to have a more realistic strategy than this, for instance, if I am using my arm to pick up an object then, if the object is a long way away, I will move the bigger joints in the arm, then as the hand gets closer the smaller joints of the hand are used for the fine adjustments.
The angle of rotation for each joint is found by taking the dot product of the vectors from the joint to the current point and from the joint to the desired end point. Then taking the arccos of this dot product.
To find the sign of this angle (ie which direction to turn), take the cross product of these vectors and checking the sign of the Z element of the vector.
For more information about calculating the angle to turn see the lookAt function here.
For a more detailed suggestion of how to store this information see these pages. These pages suggest how we can specify constraints to make different types of joints.