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 xaxis, the zaxis becomes the yaxis and the yaxis becomes the zaxis, 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
Multiple Rotations Around X,Y,Z
Started by David20321, Jul 25 2001 03:25 PM
12 replies to this topic
Sponsor:
#3 Members  Reputation: 122
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
David
#5 GDNet+  Reputation: 838
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.
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.
#6 Members  Reputation: 122
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:
A.H aka Blueshift
Edited by  Blueshift on July 26, 2001 10:13:17 AM
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.0c;
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.ysz;
M[2]=tx*Axis.z+sy;
M[4]=tx*Axis.y+sz;
M[5]=ty*Axis.y+c;
M[6]=ty*Axis.zsx;
M[8]=tx*Axis.zsy;
M[9]=ty*Axis.z+sx;
M[10]=t*Axis.z*Axis.z+c;
M[12]=Pivot.xM[0]*Pivot.xM[4]*Pivot.yM[8]*Pivot.z;
M[13]=Pivot.yM[1]*Pivot.xM[5]*Pivot.yM[9]*Pivot.z;
M[14]=Pivot.zM[2]*Pivot.xM[6]*Pivot.yM[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 Members  Reputation: 122
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.
Try reversing your rotation equations. It may work (or possibly your computer will just explode)
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)
#8 Members  Reputation: 122
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
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
#10 Members  Reputation: 122
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
>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
#12 Members  Reputation: 122
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.
This way i think it works forward not backward. I hadn''t really thought about it going backward
[ 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
#13 Members  Reputation: 122
Posted 27 July 2001  02:56 AM
Yep, OpenGL uses rowmajor 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 columnbased matrices, if you want to avoid nightlong bugtracking sessions
A.H aka Blueshift
Don''t mix row and columnbased matrices, if you want to avoid nightlong bugtracking sessions
A.H aka Blueshift