Hi,

I contact you because in my software I use quaternion to do so rotation and in a specific context I need to convert my quaternion to a rotation matrix.

In fact, my software use a formula for this and it works fine for the work I have do until now. Last week I have added some code of someone else, he is also doing so rotation with quaternion but with another formula.

What I have discover is that my formula and his formula are rotating in the inverse direction !!! And I don't know why because both should have the same rotation result.

This stuff is driving me nuts... so if someone understand what is happening ?

Here is the pseudo-code of my formula :

Matrix3 rotation = Matrix3.Identity;

float length = (x * x) + (y * y) + (z * z) + (w * w);

float s = (length > 0f) ? 2f / length : 0f;

float xx = x * x;

float yy = y * y;

float zz = z * z;

float xw = x * w;

float yw = y * w;

float zw = z * w;

float xz = x * z;

float xy = x * y;

float yz = y * z;

rotation.M00 = 1f - (s * yy + s * zz);

rotation.M10 = s * xy - s * zw;

rotation.M20 = s * xz + s * yw;

rotation.M01 = s * xy + s * zw;

rotation.M11 = 1f - (s * xx + s * zz);

rotation.M21 = s * yz - s * xw;

rotation.M02 = s * xz - s * yw;

rotation.M12 = s * yz + s * xw;

rotation.M22 = 1f - (s * xx + s * yy);

The pseudo code of his formula

Matrix4 mat = Matrix4.Identity;

float ww = w * w;

float xx = x * x;

float yy = y * y;

float zz = z * z;

// Diagonal elements

mat.M00 = ww + xx - yy - zz;

mat.M11 = ww - xx + yy - zz;

mat.M22 = ww - xx - yy + zz;

// 0,1 and 1,0 elements

float q03 =w * z;

float q12 =x * y;

mat.M01 = 2.0f * (q12 - q03);

mat.M10 = 2.0f * (q03 + q12);

// 0,2 and 2,0 elements

float q02 = w * y;

float q13 = x * z;

mat.M02 = 2.0f * (q02 + q13);

mat.M20 = 2.0f * (q13 - q02);

// 1,2 and 2,1 elements

float q01 = w * x;

float q23 = y * z;

mat.M12 = 2.0f * (q23 - q01);

mat.M21 = 2.0f * (q01 + q23);