1. ## Quaternions and DirectX

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

2. ## Quaternions and DirectX

Most of these methods are implemented in D3DX. You can start looking here, 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.

3. ## Quaternions and DirectX

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?

4. ## Quaternions and DirectX

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&#58;

Q = |X Y Z W|

Then the quaternion can then be converted into a 4x4 rotation
matrix using the following expression &#40;Warning&#58; you might have to
transpose this matrix if you &#40;do not&#41; follow the OpenGL order!&#41;&#58;

AŹ¶        2     2                                      AŹ¶
AŹ¶ 1 - &#40;2Y  + 2Z &#41;   2XY + 2ZW         2XZ - 2YW       AŹ¶
AŹ¶                                                     AŹ¶
AŹ¶                          2     2                    AŹ¶
M = AŹ¶ 2XY - 2ZW         1 - &#40;2X  + 2Z &#41;   2YZ + 2XW       AŹ¶
AŹ¶                                                     AŹ¶
AŹ¶                                            2     2  AŹ¶
AŹ¶ 2XZ + 2YW         2YZ - 2XW         1 - &#40;2X  + 2Y &#41; AŹ¶

AŹ¶                                                     AŹ¶

If a 4x4 matrix is required, then the bottom row and right-most column
The matrix may be generated using the following expression&#58;

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&#91;0&#93;  = 1 - 2 * &#40; yy + zz &#41;;
mat&#91;1&#93;  =     2 * &#40; xy - zw &#41;;
mat&#91;2&#93;  =     2 * &#40; xz + yw &#41;;
mat&#91;4&#93;  =     2 * &#40; xy + zw &#41;;
mat&#91;5&#93;  = 1 - 2 * &#40; xx + zz &#41;;
mat&#91;6&#93;  =     2 * &#40; yz - xw &#41;;
mat&#91;8&#93;  =     2 * &#40; xz - yw &#41;;
mat&#91;9&#93;  =     2 * &#40; yz + xw &#41;;
mat&#91;10&#93; = 1 - 2 * &#40; xx + yy &#41;;
mat&#91;3&#93;  = mat&#91;7&#93; = mat&#91;11&#93; = mat&#91;12&#93; = mat&#91;13&#93; = mat&#91;14&#93; = 0;
mat&#91;15&#93; = 1;

The resulting matrix uses the following positions&#58;

AŹ¶ mat&#91;0&#93;  mat&#91;4&#93; mat&#91; 8&#93; mat&#91;12&#93; AŹ¶
M = AŹ¶ mat&#91;1&#93;  mat&#91;5&#93; mat&#91; 9&#93; mat&#91;13&#93; AŹ¶
AŹ¶ mat&#91;2&#93;  mat&#91;6&#93; mat&#91;10&#93; mat&#91;14&#93; AŹ¶
AŹ¶ mat&#91;3&#93;  mat&#91;7&#93; mat&#91;11&#93; mat&#91;15&#93; 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.

5. ## Quaternions and DirectX

Isn't that what D3DXMatrixRotationQuaternion does?

EDIT:

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•