# Rotating a vector with a quaternion (D3DX)

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

## 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 on other sites
Quote:
 Original post by Joni-MattiWhat 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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013598
×