# Clarifying Blender skeleton animation and OpenGL

I'm looking into implememting skeleton animations. I use Blender to create the armatures, export them using the IQM file format (Inter Quake Model). My first steps are to make sure that I'm getting the right values in my openGL application.

I created a very basic model in blender:

When I print the local matrix of the root bone in Blender I see:

>>> bpy.data.armatures[0].bones[0].matrix_local

Matrix(((1.0, 0.0,  0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, 1.0,  0.0, 0.0),
(0.0, 0.0,  0.0, 1.0)))


Which seems to be a 90º rotation around the X-axis. Why does the matrix_local of this particular bone given me a 90º rotation around the X-axis?

Another question I have is, when I convert this matrix to a quaternion in Blender, I get (0.7071068286895752, 0.7071068286895752, 0.0, 0.0) (w,x,y,z). Though when I create my own quaternion with a 90º rotation around X, I also get (0.707107, 0.707107, 0.000000, 0.000000) (wxyz), though when I convert that back to a matrix using my own math lib, I get:

1.00,  0.00,  0.00,  0.00
0.00,  0.00,  1.00,  0.00
0.00, -1.00,  0.00,  0.00
0.00,  0.00,  0.00,  1.00


It would be great if someone could clarify this a bit!

Thanks

• Could this be related to Blender being Z up, while many other 3D graphics applications and game engines are Y up? – user7952 May 21 '15 at 16:56
• @SixthOfFour if it was related to Z-up instead of Y-up it doesn't explain why the bone has an rotation in the first place right? – roxlu May 21 '15 at 18:48
• @SixthOfFour, good catch with the axis thing but it looks like it could be that Y and Z are reversed. I'd first try to multiply both of them both by -1.0. before swizzling anything. You should be able to diagnose if Y and Z need to be swapped by rotating the root bone by 20 degrees along one of those axis. Right now with both of them at (+/- 1.0) it's hard to tell. – MarcClintDion May 21 '15 at 18:51
• @MarcClintDion Thanks for your coment, but instead of trying out different things I want to understand why I'm getting these values. – roxlu May 21 '15 at 18:55
• I figured out what the rotations mean. In blender meshes/vertices etc.. are in a space with Z-up. Armatures/bones are in Y-up. A bone, aligns with the Y axis, from head to tail. So to align the bone with my mesh, blender needs to rotate it 90º around X, which makes sense now. Thanks everyone for their input. – roxlu May 22 '15 at 13:32

A rotation matrix tells you what to do with the components of a vector indicating the position of a point. For a rotation around X, a vector along X does not change. At 90 degrees, a vector along Y becomes a vector along Z, and a Z vector becomes -Y. Rotation about X causes Z and Y to intermingle.

The fourth row/column do not matter - that is an artifact of doing 3D geometry in projective coordinates. We sometimes call it 'w' and normally just set it to 1.0 and leave it there. The thing being rotated is a vector representing a direction or position.

The rotation matrix does the rotating. Generally, it intermingles components in a two-dimensional subspace, and leaves a one dimension subspace, the axis of rotation, alone.

Quaternions, in contrast, are better thought of (sometimes) as the thing being rotated. No, not rotated, but "acted upon". For quaternions act a bit odd.

The First Quirky Principle of Quaternions is that if we want to deal with a rotation of (theta) degrees, we work on the quaternion by half that angle.

The Second Quirky Principle: instead of having three components like a vector, we add a fourth, conventionally called 'W'. We may call it the "scalar component". All four components are involved when we "act upon" the quaternion in a way corresponding to some rotation. I've taken to calling this action "Spotation" for "Spinor Rotation" - in quantum physics, we use "spinors" which amount to the same thing as quaternions.

Consider a rotation about X, as before. The Y and Z components intermingle, as with a vector, but at half the rotation angle. That is, a 90 degree X rotation which would swap Y and Z (minus signs as appropriate) for a vector, will look like a 45 degree rotation has acted upon the Y and Z components of a quaternion. (Does Blender S.E allow LaTeX?)

Y' = 1/sqrt(2) Y - 1/sqrt(2) Z
Z' = 1/sqrt(2) Y + 1/sqrt(2) Z


But according to the second quirky principle of quaternions, the "spotation" action intermingles the scalar 'W' component and the X component in a similar manner.

W' = 1/sqrt(2) W - 1/sqrt(2) X
X' = 1/sqrt(2) W + 1/sqrt(2) X


The exact algebraic rules for doing this were invented by Hamilton in the late 19th Century.

Generally, a rotation about any axis A will correspond to a "spotation" about the same axis A, but while a given vector that happens to be parallel to A will remain unaffected by the rotation, the corresponding spotation acting on a quaternion whose vector part is along A, will be affected.

We can relate quaternions to rotations by starting off with a quaternion which is pure scalar, that is, w,x,y,z = 1,0,0,0. Then some rotation R about some axis A by angle (theta) will act on the quaternion by converting the W component, being the only nonzero component, into a scalar + vector parallel to A.

Note that one consequence of the Two Quirky Principles of Quaternions is that a 180 degree rotation about any axis, which will just negate any vector perpendicular to the axis of rotation, corresponds to a spotation which looks like a 90 degree rotation. If the axis is along X Y or Z, it cleanly trades (with minus signs as needed) between W, Z, Y or Z. This is a key point to why interpolation of orientation works so nicely with quaternions, as discovered by Ken Shoemake in the 1980s. Negation of a vector does not offer any natural way to find a half-way point, but quaternions provide that nice hint.

Another interesting consequence is that a 360 rotation, easily confused with "no operation", corresponds to a 180 degree spotation - negation of all four components of the spinor. I mean, quaternion. Two different quaternions describe the same orientation of an object. That sometimes leads to some problems when animating spinning, tumbling, rolling objects, but easily fixed by flipping the sign of a quaternion somewhere in the timeline.

This also describes the strange antics of quantum particles such as electrons. As a theoretical physicist, I could blabber on and on about how all this arises from deep and beautiful mathematical relations between the rotation group in three dimensions, SO(3), and its double-covering group SU(2), and elegant math using "multivectors", and more, but that's getting off-topic for this site...

(Note: "Quirky Principles of Quaternions" is a phrase I made up on the spot just now, to avoid dealing with any real math. You won't find it in any textbook.)