Multiple Rotations Around X,Y,Z

Started by
11 comments, last by David20321 22 years, 9 months ago
Ok here''s my problem, I have an xrotation,yrotation and zrotation for my object. The problem is if I rotate in any direction all the rotation axes change. If I do a 90 degree rotation on the x-axis, the z-axis becomes the y-axis and the y-axis becomes the z-axis, thus throwing off all my rotations. Is there any way to fix this? I don''t want to have to go through every vertex applying my custom rotation code because it would not be hardware accelerated (I think) -David-
Advertisement
make sure you use glPushMatrix() and glPopMatrix() between rotations as well as glTranslatef()
Yes! There are kangaroos in Australia but I haven't seen them...yet
No, no, I don''t think that''s the problem, what I mean is that right now if I have a cube and I rotate it 45 degrees on the x axis and then 90 degrees on the y axis I want it to be the same as if I rotate it it -45 degrees on the x axis and then rotate it 180 degrees on the y axis. I want it to rotate around the global axes, not the local ones.

-David-
the problem is called ''gimbal lock'' its caused by using eular angles to represent rotations.
use a different method to store rotations eg matrices or quaternions
What you need to do is keep a rotation matrix for the object. When you want to rotate it, just convert your angle and axis (in normal form) of rotation to a quaternion, then convert that to a rotation matrix, then rotate your original rotation matrix by that rotation matrix.

When drawing the object, use glMultMatrix to muliply the current modelview matrix by your stored rotation matrix.

I think there is also a way to do it without converting to a quaternion first, but I''m not sure of the maths.

All of the operations described above can be found in the
Matrix and Quaternion FAQ.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

The key is to specify rotations in the local coordsys of the object. This is the way eg. 3dsMax solves the problem. Then you transform the object through it's local to world matrix. The whole thing can be multiplied into a single matrix, so you can get hw acceleration. If you absolutely want to specify rotations in world space, then you have to transform them back to the local coordsys first (using the inverse world matrix), but be sure to use the angle,axis representation otherwise you'll run into the gimbal lock again.

ben:
The code to directly create a matrix from an axis, angle and pivot is:

void mtx_AxisAnglePivotToMatrixH(fvector Axis, float angle, fvector Pivot, matrix4 M){   	float s, t, c, sx, sy, sz, tx, ty;	c=cos(angle); s=sin(angle); t=1.0-c;      	sx=s*Axis.x; sy=s*Axis.y; sz=s*Axis.z;	tx=t*Axis.x; ty=t*Axis.y;	M[0]=tx*Axis.x+c;	M[1]=tx*Axis.y-sz;	M[2]=tx*Axis.z+sy;	M[4]=tx*Axis.y+sz;	M[5]=ty*Axis.y+c;	M[6]=ty*Axis.z-sx;	M[8]=tx*Axis.z-sy;	M[9]=ty*Axis.z+sx;	M[10]=t*Axis.z*Axis.z+c;	M[12]=Pivot.x-M[0]*Pivot.x-M[4]*Pivot.y-M[8]*Pivot.z;	M[13]=Pivot.y-M[1]*Pivot.x-M[5]*Pivot.y-M[9]*Pivot.z;	M[14]=Pivot.z-M[2]*Pivot.x-M[6]*Pivot.y-M[10]*Pivot.z;        M[3]=M[7]=M[11]=0.0;        M[15]=1.0;} 


A.H aka Blueshift


Edited by - Blueshift on July 26, 2001 10:13:17 AM
I don''t think hes talking about gimbal lock however, what i think the problem is is the order of rotations. It seems to me that glRotatef does not multiply the current matrix by the new matrix, but multiplies the rotation matrix by the current matrix.
i.e.
if R is the rotation matrix, and M is the current matrix (modelview or whatever ur using).
It seems glRotate does this M = RxM
NOT M=MxR as it says it does
(However this may be complete crap.)
This would explain why in all of the samples i''ve done you Translate then rotate which normally would cause a translation when you rotate as you are not rotating about the origin.
normally  M = Rotation x Translation{  glTranslate  glRotatef}        |                           |                |          |                         +---+              |  +---+        |                         | | |              |  |   |     +-----+                   ---|-+-|---     ------+--|---|-------|--+--|---      ==>          | | |    ==>       |  |   |     +--|--+                      +---+              |  +---+        |                           |                |        |                           |         |or M = Translation x Rotation{  glRotatef  glTranslate}                                                  |        |                  |                     +-+-+        |                  |                     | | |        |                  |                     | | |     +-----+               |  +-----+            +-+-+-----|--+--|---   ==> -----+--|-----|--  ==>       |     +--|--+               |  +-----+       -------+-------        |                  |                       |        |                  |                       |        |                  |                       |

Try reversing your rotation equations. It may work (or possibly your computer will just explode)
Stinger:
No, glRotate is doing M = M * R.
The reason why everything seems reversed is in the nature of matrix math: The matrix that is multiplied _last_ will be applied _first_ to the object.

Eg.
M = S * R1 * T * R2

multiplying a vertex by this matrix will cause the following (in this order !):
- rotate the vertex by R2
- translate it by T
- rotate it by R1
- scale it by S

Everything is reversed. This behaviour is very important when doing object transformation hierarchies, this is where glPushMatrix and glPopMatrix are extremly usefull.

A.H aka Blueshift

Blueshift:
what''s a pivot, and why is it needed? Surely just an angle and axis is enough to describe a rotation?

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

ben:

>what''s a pivot, and why is it needed? Surely just an angle and axis is enough to describe a rotation?

Yes, angle and axis are enough to describe a rotation around an axis that goes through the origin. If you don''t want to rotate through the origin but through a certain point of rotation, then this point is a pivot.

It''s equivalent to:
glTranslate(Pivot.x, Pivot.y, Pivot.z)
glRotate(...)
glTranslate(-Pivot.x, -Pivot.y, -Pivot.z)

It is just convenient and faster to have it all in a single matrix, saves 2 matrix multiplies

Just set M12, M13 and M14 to 0, if you don''t like it.

A.H aka Blueshift

This topic is closed to new replies.

Advertisement