Results 1 to 5 of 5

Thread: Quaternions and DirectX

  1. #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. #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. #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. #4

    Quaternions and DirectX

    Quote 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.

  5. #5

    Quaternions and DirectX

    Isn't that what D3DXMatrixRotationQuaternion does?

    EDIT:

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

Bookmarks

Posting Permissions

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