Altering DirectX matrices directly with transforms.

This is a discussion on Altering DirectX matrices directly with transforms. within the Game Programming forums, part of the General Programming Boards category; I'm coming across a lot of instances when I would have an existing D3DXMatrix representing the transform (translation, rotation, and ...

  1. #1
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27

    Altering DirectX matrices directly with transforms.

    I'm coming across a lot of instances when I would have an existing D3DXMatrix representing the transform (translation, rotation, and scaling) of an object. However, I often need to alter the transform by a very simple transom, like moving it on the y axis a certain amount or rotating it about the x axis by a certain amount.

    I suppose I could put the transom i want into a vector or quaternion, make a matrix out of that, and multiply the existing matrix by that to get the final transform, but that seems wasteful. Is there an easier way to apply a vector or quaternion to a matrix?

    Also, for directx functions that use matrices, such as SetTransform, if we alter or delete the matrix after we have sent its pointer to the function, will that mess things up, or is does the function just use it for multiplication and then doesn't care about what happens to the matrix afterward (meaning that you're have to keep calling the function and sending the matrix every time you want to alter it)?

    Thanks!
    Last edited by Vick jr; 05-24-2010 at 06:08 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    D3DXVec3TransformCoord
    D3DXVec3TransformCoordArray

  3. #3
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Bubba View Post
    D3DXVec3TransformCoord
    D3DXVec3TransformCoordArray
    Those can alter a vector by a matrix. not alter a matrix by a vector. But I think I figured it out now.

    Anyway, if I set my directx device to use a certain matrix as the view matrix, using something like

    Code:
    d3ddev->SetTransform(D3DTS_VIEW, &matView);
    will I have to do this again every time I change matView to reflect those changes?

    Also, does anyone have an example of a camera class?

    Thanks!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You don't transform matrices by vectors you transform vectors by matrices.

  5. #5
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Bubba View Post
    You don't transform matrices by vectors you transform vectors by matrices.
    OK. I get it. I had been trying to have a vector represent position, and use it combined with a rotation matrix to make a final transform matrix. I need to make the vector into a matrix first, then multiply it, or rotate the vector by the matrix and use that.

    Anyway on to my second question: do I always have to re-send my view matrix to my directx device for changes I've made to the matrix to take effect?

    Also, one more question: If it build a matrix to transform an object, would the inverse of that matrix be the view matrix if that object was the camera?

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    1. You can create a rotation matrix from a 3D vector with x representing yaw, y pitch, and z roll.
    2. If the view matrix has changed you must resend the re-calculated view matrix to the hardware. A good practice is to keep a dirty flag in the camera. Set it to false when you calc the view matrix. Set it to true when any part of the camera's orientation/position changes. This way you aren't re-calcing a matrix you already performed the calculations for.
    3. The inverse of a matrix is essentially an 'undo' for matrix transforms. Inverse world = local.

  7. #7
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Bubba View Post
    1. You can create a rotation matrix from a 3D vector with x representing yaw, y pitch, and z roll.
    2. If the view matrix has changed you must resend the re-calculated view matrix to the hardware. A good practice is to keep a dirty flag in the camera. Set it to false when you calc the view matrix. Set it to true when any part of the camera's orientation/position changes. This way you aren't re-calcing a matrix you already performed the calculations for.
    3. The inverse of a matrix is essentially an 'undo' for matrix transforms. Inverse world = local.
    I got my camera class working perfectly! I'm storing the yaw, pitch, and roll in floats and the position as a vector. I change the yaw/pitch/roll when i want the camera to look somewhere then recalculate the rotation vector. To move it, I have another vector to represent movement along the local axises. I transform that by the rotation matrix to get movement in world space, then add that to the position vector, and recalculate the matrix of the position translation. Any time I move or look, I also recalculate the final transform matrix by multiplying the rotation matrix by the position matrix, and taking the inverse of that.
    It could probably be more efficient, but it seems to work as intended. Thank you for your help!

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    To move it, I have another vector to represent movement along the local axises.
    Moving the camera requires the up, right, and look vectors. They can either be stored or extracted from the final view matrix.

  9. #9
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Bubba View Post
    Moving the camera requires the up, right, and look vectors. They can either be stored or extracted from the final view matrix.
    I was going to forgo the position/movement vectors and use matrices instead, so I wouldn't have to construct matrices from them later. However I now have another problem.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    What do you mean forego them? How would you do that?

    A D3D camera is composed of the following:

    D3DXVECTOR3 m_vecLook;
    D3DXVECTOR3 m_vecRight;
    D3DXVECTOR3 m_vecUp;
    D3DXVECTOR3 m_vecPos;

    or

    D3DXVECTOR3 m_vecPos;
    D3DXQUATERNION m_vecRot;

    In the latter type of camera you extract the up, look, and right vectors from the view matrix created by the quaternion.

  11. #11
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Bubba View Post
    What do you mean forego them? How would you do that?

    A D3D camera is composed of the following:

    D3DXVECTOR3 m_vecLook;
    D3DXVECTOR3 m_vecRight;
    D3DXVECTOR3 m_vecUp;
    D3DXVECTOR3 m_vecPos;

    or

    D3DXVECTOR3 m_vecPos;
    D3DXQUATERNION m_vecRot;

    In the latter type of camera you extract the up, look, and right vectors from the view matrix created by the quaternion.
    Yes, you could store a camera like that, and create a view matrix with a function like D3DXMatrixLookAtLH , which creates a matrix from the camera position, look at position, and up direction. Then you would use that as your view matrix. However, I decided to create my view matrix directly because I could then use yaw/pitch/roll instead of quaternions or look-at positions. Right now my camera is still described in the form of a yaw/pitch/roll vector and position vector. Those vectors are converted to corresponding rotation and translation matrices, which are multiplied to get a transform matrix, and inverted to get the final view matrix.
    Both methods still require vectors and corresponding matrices which basically represent the same rotation or translation information. Instead of using vectors to store my yaw/pitch/roll and position, I could use the corresponding matrices directly and just alter the relevant elements. Since I'm already storing and using those matrices it wouldn't take up any more memory, but it would be more efficient because I wouldn't have to convert the vectors/quaternions to matrices before using them.

    Of course, there are probably even better ways to do it. Mine seems to work they way i want it to for now.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I never mentioned the D3DXMatrixLookAtLH function in my posts. I do not use it to compute my view matrix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 06:16 PM
  3. DirectX rotation matrices
    By confuted in forum Game Programming
    Replies: 7
    Last Post: 08-10-2003, 03:05 PM
  4. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM
  5. DirectX matrices
    By Magos in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2002, 02:58 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21