Thread: DirectX rotation matrices

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    DirectX rotation matrices

    I thought I had this working correctly, and it seemed to, but when I payed closer attention as the shape rotated, I noticed that the axes are getting messed up as the rotation increases.

    This is how I'm setting my world matrix
    Code:
    D3DXMatrixRotationX(&matRotationX,fRotationX);
    D3DXMatrixRotationY(&matRotationY,fRotationY);
    D3DXMatrixRotationZ(&matRotationZ,fRotationZ);
    D3DXMatrixTranslation(&matTranslation,fTranslationX,fTranslationY,fTranslationZ);
    g_App.GetDevice()->SetTransform(D3DTS_WORLD,&(matRotationX*matRotationY*matRotationZ*matTranslation));
    I think this is wrong because I'm multiplying the rotation matrices one at a time, so the rotation is being done on partially-transformed axes... not sure how to word that.

    Do I need to be building one composite matrix from fRotationX, fRotationY, fRotationZ, fTranslationX, fTranslationY, and fTranslation in one step? Is there a function for doing this?

    edit: is this perhaps gimbal lock?
    Last edited by confuted; 08-09-2003 at 03:33 PM.
    Away.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Make sure you keep your degrees within [0 - 360). Are you doing this already?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yup, I have
    fRotationX=(float)fmod(fRotationX,(2*D3DX_PI));
    in there for every angle.
    Away.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    To be a bit more specific, after one full rotation on the Y axis, rotation on the X and Z axises do the same or approximately the same thing - obviously incorrect. Playing with the Y rotation more tends to fix it.

    There's a link to the project in my signature if you're interested in looking at it.
    Away.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I did some more research, and from a couple example programs that I found, my problem is indeed gimbal lock I don't understand very well what causes the problem, but evidently I can fix it using quaternions. Could someone explain the cause of gimbal lock clearly, or point me to a site which does?

    edit: I learned to spell
    Last edited by confuted; 08-09-2003 at 06:46 PM.
    Away.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by blackrat364
    I did some more research, and from a couple example programs that I found, my problem is indeed gimbal lock I don't understand very well what causes the problem, but evidently I can fix it using quaternions. Could someone explain the cause of gimbal lock clearly, or point me to a site which does?

    edit: I learned to spell
    It has to do with euler angles. Its when you rotate one axis onto another you lose a degree of freedom. There are some good sites that have little applets demonstrating the problem. Get ready for the exciting world of quaternions.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Alright... last night I read several sites about quaternions, and I basically understand them now. I implemented quaternions into my program several different ways, and they've worked, but I haven't yet managed to avoid gimbal lock with them. Perhaps I haven't fully converted everything I should be changing. I'm currently converting my Euler angles into three quaternions, multiplying the quaternions together, and turning the final quaternion into a matrix. It works, but as I said, it doesn't avoid gimbal lock. Do I need to find a way to ditch the Euler angles all together?
    Away.

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I figured out my problem. I just needed to sit down and look really closely at the math involved, and visualize an example in my head to figure out exactly how it all had to work. If anyone had walked in here, they would have thought I was crazy...eyes closed, hands waving around in strange patterns in the air, and me murmuring "okay" or "no" from time to time. lol.

    edit: except that now my axes aren't rotating with the primitive. I'll have to figure out how to fix that.
    Last edited by confuted; 08-10-2003 at 03:07 PM.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. working with rotation matrices
    By hannibar in forum Game Programming
    Replies: 23
    Last Post: 03-30-2005, 01:10 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM
  5. Rotation matrices
    By gazsux in forum C Programming
    Replies: 3
    Last Post: 01-07-2003, 06:56 PM