Rotating a vector with a quaternion (D3DX)

Started by
0 comments, last by Zakwayda 16 years, 5 months ago
I wanted to transform a vector with a quaternion without converting the quaternion to a matrix. I know that I can transform a vector with a quaternion with the following equation: vTransformed = q * vec * inverse(q) So I tried the following code using D3DX:

D3DXVECTOR3 vec(1.0f, 2.0f, 3.0f); // This will be transformed
D3DXQUATERNION qVec(vec.x, vec.y, vec.z, 0.0f);

D3DXVECTOR3 vAxis(2.0f, 1.0f, 0.0f);

D3DXQUATERNION quat;
D3DXQuaternionRotationAxis(&quat, &axis, 2.0f);
D3DXQuaternionNormalize(&quat, &quat);

D3DXQUATERNION quatInv;
D3DXQuaternionInverse(&quatInv, &quat);
D3DXQUATERNION qResult = quat * qVec * quatInv;

D3DXVECTOR3 vTransformed(qResult.x, qResult.y, qResult.z);

But when I compared the result to a vector transformed by the matrix equivalent, I found out that vTransformed is actually incorrect. Then I found that in order to get the correct vector, I have to multiply the quaternions in reverse order, so in my code qResult would be: qResult = quatInv * qVec * quat; What am I missing in here? Why do I have to multiply the quaternions in reverse order?
Advertisement
Quote:Original post by Joni-Matti
What am I missing in here? Why do I have to multiply the quaternions in reverse order?
Because (IINM) DX uses 'reverse' quaternion multiplication order to reflect the use of row-vector matrix transforms. Therefore, expressions involving quaternion products need to be reversed relative to their 'normal' versions.

This topic is closed to new replies.

Advertisement