• Advertisement
Sign in to follow this  

Rotating a vector with a quaternion (D3DX)

This topic is 3731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement