Yet More Gimbal Locking...

Started by
1 comment, last by CombatWombat 21 years, 10 months ago
Hello, I''m having difficulty getting my 3d rotations to work properly. I''m storing the rotation as 3 matrices which are then multed together before the object is rendered. Problem is, the axis are still flipping with each other and generally going out of whack... Heres the relevent code...
  

float XMatrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
float YMatrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
float ZMatrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };

void Control()    // called once every frame

{
		if(keys[VK_LEFT])
		{
			xtheta ++;
		}
		if(keys[VK_RIGHT])
		{
			xtheta --;
		}
		if(keys[VK_DOWN])
		{
			ztheta --;
		}
		if(keys[VK_UP])
		{
			ztheta ++;
		}
		if(keys[''Z''])
		{
			ytheta --;
		}
		if(keys[''X''])
		{
			ytheta ++;
		}

	
		ZMatrix[0] = cos((ztheta) * (3.14159 / 180));
		ZMatrix[1] = -sin((ztheta) * (3.14159 / 180));
		ZMatrix[4] = sin((ztheta) * (3.14159 / 180));
		ZMatrix[5] = cos((ztheta) * (3.14159 / 180));
		
		YMatrix[0] = cos((ytheta) * (3.14159 / 180));
		YMatrix[2] = sin((ytheta) * (3.14159 / 180));
		YMatrix[8] = -sin((ytheta) * (3.14159 / 180));
		YMatrix[10] = cos((ytheta) * (3.14159 / 180));
		
		XMatrix[5] = cos((xtheta) * (3.14159 / 180));
		XMatrix[6] = -sin((xtheta) * (3.14159 / 180));
		XMatrix[9] = sin((xtheta) * (3.14159 / 180));
		XMatrix[10] = cos((xtheta) * (3.14159 / 180));
}

// DrawGLScene...

        Control();
        glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -50.0f);
	glMultMatrixf(ZMatrix);
	glMultMatrixf(YMatrix);
	glMultMatrixf(XMatrix);

        // draw object;

  
Whats going on? Why am I still getting the evil gimbal with matrices?
Advertisement
As long as you will be doing your transformation in three separate steps, you will get gimbal locking. Whether you use glRotate() or glMultMatrix() does not change this.

You need to make the transformation in one, single, step.
Quaternions, quaternions, quaternions, quaternions !

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quaternions.

(just thought I''d back fruny up there).

Actually you can avoid gimbal lock without them (e.g. using axis-angle) but you need them to interpolate rotations which you will most definitely want to do. If you absolutely insist on using Euler angles (don''t) you can avoid gimbal lock by writing routines which check if a rotation is greater than 90 and, if so, split the operation into rotations of less than 90, resolving to a single matrix at each step. This is a lame hack which you shouldn''t bother with. Don''t even know why I mentioned it really.

Geocyte Has Committed Suicide.
Geocyte Has Committed Suicide.

This topic is closed to new replies.

Advertisement