• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Multiple Rotations Around X,Y,Z

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1David20321  Members

Posted 25 July 2001 - 03:25 PM

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-

### #2Xtreme  Members

Posted 25 July 2001 - 07:30 PM

make sure you use glPushMatrix() and glPopMatrix() between rotations as well as glTranslatef()

### #3David20321  Members

Posted 25 July 2001 - 07:51 PM

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-

### #4zedzeek  Members

Posted 25 July 2001 - 08:21 PM

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

### #5benjamin bunny  GDNet+

Posted 26 July 2001 - 02:56 AM

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.

### #6Blueshift  Members

Posted 26 July 2001 - 03:08 AM

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

### #7_Stinger  Members

Posted 26 July 2001 - 03:32 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)

### #8Blueshift  Members

Posted 26 July 2001 - 04:31 AM

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

### #9benjamin bunny  GDNet+

Posted 26 July 2001 - 04:53 AM

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

### #10Blueshift  Members

Posted 26 July 2001 - 07:58 AM

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

### #11zedzeek  Members

Posted 26 July 2001 - 10:45 AM

### #12_Stinger  Members

Posted 26 July 2001 - 04:23 PM

Yeah i think it''s because i used to do my matricies differently using a column based thingy.
[ a b c d][ e f g h][ i j k l][ m n o p]where ''x = ax + ey + iz + m''y = bx + fy + jz + n''z = cx + gy + kz + o''w = dx + hy + lz + p

This way i think it works forward not backward. I hadn''t really thought about it going backward

### #13Blueshift  Members

Posted 27 July 2001 - 02:56 AM

Yep, OpenGL uses row-major transformation matrices. You have to invert your multiplication order to get the same thing. The advantage is that you can directly load your own matrices with glLoadMatrix() or glMultMatrix() instead of converting first.
Don''t mix row- and column-based matrices, if you want to avoid nightlong bug-tracking sessions

A.H aka Blueshift

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.