View Full Version : Quaternions and DirectX

cronodragon

02-01-2006, 04:37 PM

Hi! Have a good new year.

I want to begin using quaternions, and would like to know what functions are there, in DirectX, to work with those. I suppose that just I need to transform yaw, pitch and roll angles into quaternions and make the transformations of vertices with them. Are there functions already implemented, or do I have to make my own from theory?

Best regards!

-Marco

Most of these methods are implemented in D3DX. You can start looking here (http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c_Summer_04/directx/graphics/reference/d3dx/functions/math/mathfunctions.asp), which is a list of all available math functions.

Of course, you can always take a book on 3D math and write these functions yourself.

cronodragon

02-01-2006, 05:10 PM

Thanks. To build a quaternion from euler angles, should I create an identity quaternion with D3DXQUATERNIONIdentity, and then use 3 calls to D3DXQUATERNIONRotationAxis to set the rotation angles in each axis?

EDIT:

Oops, I found this one that does all the dirty work: D3DXQUATERNIONRotationYawPitchRoll

Now which do I use to transform the 3D vector with the quaternion?

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.

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 (http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q54) (if you use that code, you'll have to transpose the matrix though). Note that D3DX might have a conversion function already.

cronodragon

02-01-2006, 06:57 PM

Isn't that what D3DXMatrixRotationQuaternion does?

EDIT:

Ok, I did it! Now I have my engine running with quaternions! Thanks! :D

Powered by vBulletin® Version 4.2.5 Copyright © 2019 vBulletin Solutions Inc. All rights reserved.