Search code examples
matrixrotationquaternionsglkit

GLKit quaternion code not agreeing with matrix code?


I must be misunderstanding something about GLKit's handling of quaternions and rotation matrices. In the following snippet, I would expect matrices a and b to end up with identical contents (subject to floating point errors)...

GLKQuaternion q = GLKQuaternionMakeWithAngleAndAxis(M_PI / 2, 1, 1, 1);
GLKMatrix3 a = GLKMatrix3MakeWithQuaternion(q);
GLKMatrix3 b = GLKMatrix3MakeRotation(M_PI / 2, 1, 1, 1);

However, they don't agree. Not even close. In column major order, the arrays contain...

a.m[0]=0.000000 b.m[0]=0.333333
a.m[1]=1.000000 b.m[1]=0.910684
a.m[2]=0.000000 b.m[2]=-0.244017
a.m[3]=0.000000 b.m[3]=-0.244017
a.m[4]=0.000000 b.m[4]=0.333333
a.m[5]=1.000000 b.m[5]=0.910684
a.m[6]=1.000000 b.m[6]=0.910684
a.m[7]=0.000000 b.m[7]=-0.244017
a.m[8]=0.000000 b.m[8]=0.333333

I thought that both GLKQuaternionMakeWithAngleAndAxis and GLKMatrix3MakeRotation each took radians, x, y, z in order to represent a rotation of the specified radians around the specified axis. And I thought that GLKMatrix3MakeWithQuaternion was intended to convert from the quaternion representation to the matrix representation.

So, why don't those agree? Do I need to normalize the axis before the quaternion creation? This does, in fact, seem to fix the problem but I don't believe it is documented that way.


Solution

  • From GLKQuaternion.h

    /* Assumes the axis is already normalized. */

    static inline GLKQuaternion GLKQuaternionMakeWithAngleAndAxis(float radians, float x, float y, float z);

    So yes, you do need to normalize the axis before creating the quaternion.