f Force vector, from End Point to Target.
If the force vector is parallel to a then the joint won't rotate. If it is parallel to
b then you're just pulling along the bone, and the joint won't rotate. Armed with this
reasoning, I decided that the torque on the joint was proportional to the sine of the angle
between a and f and the sine of the angle between b and f.
If the End Point is on the target, then you don't want the structure to move any more, but if
the End Point is very far from the target, then you want the structure to move there quickly.
Again, I decided that the torque should also be proportional to the magnitude of the force
One thing the previous two paragraphs do not tell you is which direction the joint needs to move.
So the joint should move in a positive direction of the force points along r, and in
a negative direction of the force points in the opposite direction to r.
So, put these three things together, and you get:
torque = Mag(f) * SinVect(a, f) * SinVect(b, f) * sign(CosVect(r, f)) * Sensitivity
Where SinVect and CosVect return the sin and cos of the angle between two vectors
Mag returns the length of a vector, and sign returns the sign of a number.
Sensitivity is some small scaler constant.
So then you move onto the next joint up the structure. Re-calculate the vectors a,b
and f, and calculate the torque for joint 2. Calculate the torque for all the joints in
the whole structure, then add the torque to each joint to it's angle. The End Point of the
structure should now be closer to the target. Repeat the process continuously, and it should do
it's best to keep touching the target.
Joint axis through bone
Right, this may look a little complicated, but there is nothing new here. This is a linked
structure consisting of four joints. Joint number two, however, rotates about the axis of it's
bone. Same thing as twisting your wrist. It is this joint that is important. As before, vector
a is along the axis of rotation, but this time it is also along the bone. The vector
b now goes from the begining of the joint to the End Point. The vector r is as
usual perpendicular to both.
To reach the target, joint 2 needs to rotate 90 degrees (see right diagram).
And to conclude, vector b is different for joints whose axis of rotation is parallel to
Now that you can do basic Inverse Kinematics, there's loads of adjustments you can make to
the system to make it behave in different ways. As I said before, some solutions are better than
others, either because they look better, or involve less movement, or are more comfortable.
Real tails on real animals rarely have kinks in them. They tend to bend in a fairly smooth
curve. The tail in the demo is made from pairs of joints at right angles to eachother. The
origional tail I wrote tended to kink and bend in unrealistic ways. So I added some springyness
to the joints. This makes the joints reluctant to bend, and the more you bend them, the more the
fight back. This has the effect of spreading the bend evenly over the tail.
Take a look at a dinosaur's tail. It is very thick at the body, becoming thin, and more
flexable towards the end. This property can be simulated in the linked structure by making the
links less and less springy towards the end.
Limbs prefer to be in comfortable positions. This can again be accomplished be adding some
spring to the joints. However, in this case it might be best to have a range of movement for
each joint, over which there is no springyness. This range is considered to be comfortable. As
the joint moves out of the comfortable range, it fights back more and more until it reaches it's
limit, where it refuses to rotate furthur.
It may also be desirable to keep the limb as low as possible. People rarely keep their arms
up in the air for long periods of time unless strictly necessary. So positions should be chosen
where the the least energy is required to hold it there.