Monday 7 July 2014

About Rotations in Skeletal Animation

In skeletal animation, for every frame of an animation, the position of a bone’s origin (pivot point) and a bone’s rotation is stored for each bone in a hierarchy of bones.

The hierarchy of bones is also known as a skeleton.

In this article I will only discuss the rotations.

Like any 3DCG object you create in 3D modelling software, each bone has a local coordinate system defined by X,Y and Z axes.

For example I can define a bone as an object where the direction from the origin of the bone to its tip is in the positive Z axis direction and the the top of the bone is in the positive Y axis direction.

In essence I have created an object as shown below.

A bone can be considered a 3D object


I have coloured the tip blue to match the positive Z axis of the 3D modelling program, coloured the top green to match the positive Y axis of the 3D modelling program and coloured one side red to match the positive X axis of the 3D modelling program.

The other side of the bone is coloured magenta and the underside of the bone is coloured yellow.

The local coordinate system X,Y and Z axes of the bone are aligned with the X,Y and Z axes of the 3D modelling program and the local coordinate system is considered to be attached to the object.

The local coordinate system origin (X=0,Y=0,Z=0) is attached to the origin of the object which is the point of the object that is at (X=0, Y=0, Z=0) in the 3D modelling program.

The local coordinate system of the bone we created is shown below.

This bone has been translated from its creation position to show how the local coordinate system is considered to be attached to the bone.

A local coordinate system is attached to the bone

Rotation is measured by determining the amount of turning from a reference orientation.

For example if we nominate the 12 o’clock position as the reference orientation for the minute hand of a clock then for the clock shown below we can say the minute hand has rotated 306 degrees in a clockwise direction or 54 degrees in a counter-clockwise direction.

Clock hand reference

In skeletal animation the reference orientation is obtained by aligning an imaginary bone’s local coordinate system with another coordinate system so the +X, +Y and +Z axes for both systems point in the same directions and the origins coincide.

To get a reference orientation align an imaginary bone with a coordinate system

If we choose to measure a bone’s rotation from a reference orientation aligned with the world coordinate system we get the bone’s absolute rotation.

If we choose to measure a bone’s rotation from a reference orientation aligned with its parent bone’s local coordinate system we get the bone’s relative rotation.

Animation software must always calculate the absolute rotations since they are what it needs to render the pose to screen.

The relative rotations are calculated too and they are what are usually stored in the animation’s file on disk.

Consider a skeleton of two bones posed as shown in the image below.

Two bone skeleton in a pose

To calculate the absolute rotation of each bone imagine a bone with its local coordinate system axes aligned with the world coordinate system’s axes.

We will use this bone’s orientation as a reference to measure the rotations of the two bones.

Reference orientation in world coordinate system

To calculate the rotation of each the two bones imagine how the reference bone needs to be rotated around the world axes to match the orientation of each of the two bones.

Remember that for a right handed coordinated system a clockwise rotation about an axis when the rotation is viewed from the positive end of the axis looking towards the origin of an axis is negative.

One way to match bone1’s orientation is to rotate the reference bone first by –90 degrees around the Z axis and then rotate it by –90 degrees around the X axis.

Since bone2 is in the same orientation as bone1 in this pose it will have the same absolute rotation.

Rotate -90 around Z

Rotate -90 around X

We can write each of these rotations as a quaternion (i j k w) and then multiply the two quaternions to get a single quaternion that represents the total rotation.

See my post on quaternions for details [link].

Rotation1 = (0 0 -0.7071 0.7071)
Rotation2 = (-0.7071 0 0 0.7071)

To combine the rotations we must multiply them in the order Rotation2 x Rotation1.
(-0.7071 0 0 0.7071) x (0 0 -0.7071 0.7071) = (-0.5 -0.5 -0.5 0.5).
Bone1’s absolute rotation = (-0.5 –0.5 -0.5 0.5)
Bone2’s absolute rotation = (-0.5 –0.5 -0.5 0.5)

For the rotation of each bone relative to its parent bone’s local coordinate system imagine a bone with its local coordinate system aligned with the parent bone’s local coordinate system.

To calculate the rotation of each the two bones imagine how the reference bone needs to be rotated around the parent bone’s local axes to match the orientation of each of the two bones.

Bone1 has no parent so the world coordinate system is used and we have the exact same situation we had when calculating the the absolute rotation for bone1.

Bone1 reference for relative rotation

To calculate bone2’s relative rotation the reference orientation is the orientation of its parent, bone1.

To measure the rotation you imagine how to rotate bone1 so its orientation matches bone2’s orientation but remember that you will be rotating around bone1’s local X, Y and Z axes which may no longer be aligned with the world axes.

Bone2's reference orientation is bone1's orientation

In this example bone1’s orientation already matches bone2’s orientation so no rotation is required.

The quaternion for no rotation is (0 0 0 1) which is also known as the identity quaternion.

Bone1’s relative rotation = Bone1’s absolute rotation = (-0.5 -0.5 -0.5 0.5)
Bone2’s relative rotation = no rotation = (0 0 0 1)

Lets calculate the absolute and relative rotations for another pose.

New pose

To calculate the absolute rotations we will use a reference orientation aligned to the world axes as shown below.

Absolute reference orientation

To match bone1’s orientation the reference orientation has to be rotated first by 90 degrees around the world Y axis.

First rotation bone1

Next the reference orientation has to be rotated by 180 degrees around the world X axis.

Second rotation bone1

Rotation1 = (0 0.7071 0 0.7071)
Rotation2 = (1 0 0 0)

Rotation = Rotation2 x Rotation1 = (1 0 0 0) x (0 0.7071 0 0.7071) = (0.7071 0 0.7071 0)

To match bone2’s orientation the reference orientation has to be rotated by 180 degrees around the world Y axis.

Rotation bone2

Rotation = (0 1 0 0)

Bone1’s absolute rotation = (0.7071 0 0.7071 0)
Bone2’s absolute rotation = (0 1 0 0)

The relative rotation of bone1 is the same as its absolute rotation since it does not have a parent bone.

To calculate bone2’s relative rotation we imagine a reference orientation in the same orientation as bone1 and rotate it about bone1’s local axes until it matches bone2’s orientation.

Reference orientation relative rotation bone2

To match bone2’s orientation the reference orientation has to be first rotated  by 180 degrees around bone1’s Z axis.

Rotate 180 degrees around bone1 Z axis

Next the reference orientation has to be rotated -90 degrees around bone1’s Y axis.

Rotate -90 degrees around bone1 Y axis

Rotation1 = (0 0 1 0)
Rotation2 = (0 -0.7071 0 0.7071)

Rotation = Rotation2 x Rotation1 = (0 -0.7071 0 0.7071) x (0 0 1 0)

Bone1’s relative rotation = (0.7071 0 0.7071 0)
Bone2’s relative rotation = (-0.7071 0 0.7071 0)

If you know the relative rotations for all the bones in a skeleton you can calculate the absolute rotations for each bone using the following multiplication.

absolute rotation = parent bone’s absolute rotation x relative rotation

Because you need to know the parent bone’s absolute rotation before you can calculate the current bone’s absolute rotation this means you need to calculate the root bone’s absolute rotation first and then calculate the absolute rotation of the child bones and then the absolute rotation of the grandchild bones and so on down the chain.

For pose1 we have:
  • Bone1’s relative rotation = Bone1’s absolute rotation = (-0.5 -0.5 -0.5 0.5)
  • Bone2’s relative rotation = no rotation = (0 0 0 1)
Therefore Bone2’s absolute rotation = (-0.5 -0.5 -0.5 0.5) x (0 0 0 1) = (-0.5 -0.5 -0.5 0.5)

This result matches the absolute rotation we calculated before for Bone2 in pose1.

For pose2 we have:
  • Bone1’s relative rotation = (0.7071 0 0.7071 0) = Bone1’s absolute rotation
  • Bone2’s relative rotation = (-0.7071 0 0.7071 0)
Bone2’s absolute rotation = (0.7071 0 0.7071 0) x (-0.7071 0 0.7071 0) = (0 -1 0 0)

This result doesn’t seem to match the absolute rotation we calculated before but remember that there are two different quaternions that represent the same rotation.

If you have one quaternion representing a rotation the other quaternion that represents the same rotation is obtained by multiplying the first quaternion by -1.

Some animation programs always use the quaternion from the pair that has a positive real number part (w).

So (0 -1 0 0) is the same rotation as –1 x (0 -1 0 0) = (0 1 0 0) which matches the absolute rotation we calculated before.

When you first create a skeleton of bones for your model you are in fact creating a pose as well since each of the bones has a rotation from the reference orientation.

The pose of the skeleton when you create the skeleton is known as the bind pose or rest pose.

Rather than store rotations measured with respect to the reference orientation we have been using in this article it is more common for animation software to store the bone rotations with respect to the bone’s orientation in the bind pose.

Say pose 1 is the bind pose for our skeleton.

Bind pose

We will calculate the rotation for each bone in pose 2 using the bind pose orientation as the reference orientation.

Pose 2

If you rotate the reference orientation around the world axes to match a bone’s orientation you get an absolute rotation relative to the bind pose.

The reference orientation for bone1 is shown below.

Bind pose reference for bone1

To match bone1’s orientation the reference orientation has to be rotated –90 degrees around the world Z axis.

-90 degrees about Z axis

Rotation = (0 0 -0.7071 0.7071)

The reference orientation for bone2 is shown below.

Bind pose reference orientation for bone2

To match bone2’s orientation the reference orientation has to be rotated first by 90 degrees around the world Z axis.

90 degrees about Z axis

Next the reference orientation is rotated –90 degrees around the world Y axis.

-90 degrees about Y axis

Rotation1 = (0 0 0.7071 0.7071)
Rotation2 = (0 -0.7071 0 0.7071)

Rotation2 x Rotation1 = (0 -0.7071 0 0.7071) x (0 0 0.7071 0.7071) = (0.5 -0.5 0.5 0.5)

Bone1’s absolute rotation from bind pose = (0 0 -0.7071 0.7071)
Bone2’s absolute rotation from bind pose = (0.5 -0.5 0.5 0.5)

The absolute rotations we calculated before for pose 2 are the real absolute rotations but they can be considered as composed of two rotations.

First a bone is rotated to the bind pose orientation from its standard reference orientation and then it is rotated from the bind pose orientation to its pose orientation.

absolute rotation = absolute rotation from bind pose x absolute rotation of bind pose

From the above equation, if we know the absolute rotation and the absolute rotation of the bind pose we can calculate the absolute rotation from the bind pose to the pose as follows.

Multiply both sides of the equation by the inverse of the absolute rotation of the bind pose.

absolute rotation x inverse of absolute rotation of bind pose
=
absolute rotation from bind pose x absolute rotation of bind pose x inverse of absolute rotation of bind pose
We multiply both sides on the right by this inverse because a rotation quaternion multiplied by its own inverse equals the identity quaternion.

absolute rotation x inverse of absolute rotation of bind pose
=
absolute rotation from bind pose x (0 0 0 1)

Any quaternion multiplied by the identity quaternion is itself.

absolute rotation x inverse of absolute rotation of bind pose = absolute rotation from bind pose

The inverse of a rotation quaternion (which are unit quaternions) is obtained by negating the i, j and k parts of the quaternion.

For example if q = (i -j k w) then the inverse of q which is designated q-1 = (-i j -k w).

For pose 2 we have:
  • Bone1’s absolute rotation = (0.7071 0 0.7071 0)
  • Bone1’s absolute rotation of bind pose = (-0.5 -0.5 -0.5 0.5)
  • Bone2’s absolute rotation = (0 1 0 0)
  • Bone2’s absolute rotation of bind pose = (-0.5 -0.5 -0.5 0.5)

Bone1’s absolute rotation from the bind pose is then:

(0.7071 0 0.7071 0) x (0.5 0.5 0.5 0.5) = (0 0 0.7071 -0.7071)

This matches our result above if multiplied by -1.

Bone2’s absolute rotation from the bind pose is then:

(0 1 0 0) x (-0.5 -0.5 -0.5 0.5) = (0.5 -0.5 0.5 0.5)

This matches the result we calculated before.

I think the relative rotations, relative to the bind pose are calculated from the absolute rotation from the bind pose and stored in the animation file. I will update with details when I confirm.

For further reading see the following site which gets to the crux of skeletal animation.

mmmovania.blogspot.com/2012/11/skeletal-animation-and-gpu-skinning.html



No comments:

Post a Comment