Monday, February 1, 2016

Inverse Kinematics, Part 2

Progress report.


I created a new class called Armature(). It contains three bones: the upper bone, the lower bone, and the child bone. Those are represented in the picture above by upper leg, lower leg, and foot.

The new limbs are called LL (Left Leg) and RL (Right Leg), which are Armatures. The yellow planes are helpers just so I know which direction the armature is pointing to. They will be hidden during gameplay, obviously.

They can be rotated as other limbs, but they have an additional dial for "stretch" (see orange circle in the picture). This is a value between 0 and 1, where 0 means "totally bent" and 1 means "totally stretched out". So the leg with the 0.8 value in the picture above is stretched out more than the other leg with the 0.4 value.

Since the upper and lower bones are of the same size (and I'm keeping it that way), calculations of their angles whenever stretching changes became extremely simple:

bone1.rotation.x = (PI/2) * (1.0 - this._stretch) * this.bendModifier;
bone2.rotation.x = PI * (1.0 - this._stretch) * -this.bendModifier;

The above calculations are called every time the "stretch" value is changed.

bendModifier allows me to bend the armatures forward or backward. For knees, that modifier is 1 (forward). For elbows, it would be -1 (backward).

I also decided, for now, to make the child bone (foot) a child of the lower bone (lower leg). Despite being part of the Armature constructor, the foot will be able to be rotated independently of the armature. In my previous post, it was not a child of the lower bone, but I realized it would be extra work to not make it so. In this implementation, I didn't need a "socket" for the foot, for example. Adjusting the height (stretch) of the armature is enough to place the foot at its correct place and there's no risk of disconnection anymore.

This is only the first half of the whole IK thing.
Next up: freezing the child bone. This is gonna be a tough one, so see you in a few weeks. ;-)

No comments:

Post a Comment