# Thread: DirectX rotation matrices

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

2. Make sure you keep your degrees within [0 - 360). Are you doing this already?

3. Yup, I have
fRotationX=(float)fmod(fRotationX,(2*D3DX_PI));
in there for every angle.

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

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

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

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

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

Popular pages Recent additions