Originally Posted by
cronodragon
Now which do I use to transform the 3D vector with the quaternion?
The easiest way is to convert your quaternion to matrix form. One of the reasons doing so is that DirectX works with matrices when it comes to transformations.
Code:
Q54. How do I convert a quaternion to a rotation matrix?
--------------------------------------------------------
Assuming that a quaternion has been created in the form:
Q = |X Y Z W|
Then the quaternion can then be converted into a 4x4 rotation
matrix using the following expression (Warning: you might have to
transpose this matrix if you (do not) follow the OpenGL order!):
AŹ¶ 2 2 AŹ¶
AŹ¶ 1 - (2Y + 2Z ) 2XY + 2ZW 2XZ - 2YW AŹ¶
AŹ¶ AŹ¶
AŹ¶ 2 2 AŹ¶
M = AŹ¶ 2XY - 2ZW 1 - (2X + 2Z ) 2YZ + 2XW AŹ¶
AŹ¶ AŹ¶
AŹ¶ 2 2 AŹ¶
AŹ¶ 2XZ + 2YW 2YZ - 2XW 1 - (2X + 2Y ) AŹ¶
AŹ¶ AŹ¶
If a 4x4 matrix is required, then the bottom row and right-most column
may be added.
The matrix may be generated using the following expression:
xx = X * X;
xy = X * Y;
xz = X * Z;
xw = X * W;
yy = Y * Y;
yz = Y * Z;
yw = Y * W;
zz = Z * Z;
zw = Z * W;
mat[0] = 1 - 2 * ( yy + zz );
mat[1] = 2 * ( xy - zw );
mat[2] = 2 * ( xz + yw );
mat[4] = 2 * ( xy + zw );
mat[5] = 1 - 2 * ( xx + zz );
mat[6] = 2 * ( yz - xw );
mat[8] = 2 * ( xz - yw );
mat[9] = 2 * ( yz + xw );
mat[10] = 1 - 2 * ( xx + yy );
mat[3] = mat[7] = mat[11] = mat[12] = mat[13] = mat[14] = 0;
mat[15] = 1;
The resulting matrix uses the following positions:
AŹ¶ mat[0] mat[4] mat[ 8] mat[12] AŹ¶
M = AŹ¶ mat[1] mat[5] mat[ 9] mat[13] AŹ¶
AŹ¶ mat[2] mat[6] mat[10] mat[14] AŹ¶
AŹ¶ mat[3] mat[7] mat[11] mat[15] AŹ¶
The above code was taken from Matrix and Quaternion FAQ (if you use that code, you'll have to transpose the matrix though). Note that D3DX might have a conversion function already.
Bookmarks