# how can i rotate a cube in an efficient way ???!!!

## Recommended Posts

i know that many people may see this topic bit strange,but of course the problem is not to rotate a cube actually. i made an application to rotate a cube and i know that it is very simple one but the problem that when i rotate the cube it rotate the (X,Y,Z) axis with it !!!! u can see it when i rotate the cube with angle 90 around Y-axis then when i rotate around X-axis it rotate around Z-axis because i rotate the axis with the cube ?!!! Sample of the Code : Gl.glPushMatrix(); Gl.glScalef(45, 45, 45); Gl.glRotatef(cubeAngley, 0, 1, 0); Gl.glRotatef(cubeAnglex, 1, 0, 0); DrawCube(); Gl.glPopMatrix(); private void Oglctrl_KeyDown(object sender, KeyEventArgs e) { // rotating cube if (e.KeyCode == Keys.W) { cubeAnglex -= 5; } else if (e.KeyCode == Keys.S) { cubeAnglex += 5; } else if (e.KeyCode == Keys.A) { cubeAngley -= 5; } else if (e.KeyCode == Keys.D) { cubeAngley += 5; } }

##### Share on other sites
jyk    2094
Are you trying to make it so that when you press 'a' and 'd' the cube always rotates about the world y axis, and when you press 's' and 'w' the cube always rotates about the world x axis?

##### Share on other sites
dmatter    4821
What you're observing is called Gimbal Lock and it's a well known problem that occurs when you use Euler angles (accumulating three angles around the three major axis). The solution is basically to not use them: If you instead accumulate matrices, quaternions or axis-angles then these will not suffer from gimbal lock.

##### Share on other sites
SWIFFT    122
It sounds like you need to use matrices for your rotations instead of calling glrotate. It would be something like:

/////////////////////////////////////////////////
//Create the rotation matrix for the angle you want this frame
//The keyboard input will tell you what axis to create the rotation on.
//i.e. if (e.KeyCode == Keys.W)
// create RoatationMatrixX;
/////////////////////////////////////////////////
matRot = RotationMatrixXYZ(angle);

//Grab the current transform matrix of the cube
matCurrentTransfor = cube.GetTransform();

//Set current transform to the rotation matrix multiplied by transform
matCurrentTransform = matRot * matCurrentTransform;

//Set the transform of the cube to the new transform
cube.SetTransform(matCurrentTransform);

##### Share on other sites
Wodzu    122

Maybe this topic will be helpful for you:

http://www.gamedev.net/community/forums/topic.asp?topic_id=564897&whichpage=1�

##### Share on other sites
SaTANO    507
dmatter is right Euler angles are not good because of Gimbal lock. If you are looking for something you should use, then try this: convert your angles into quaternion and then into matrix so you will only have to multiply your translation matrix with this "quaternion" matrix with.
I think the best source is euclideanspace
Euler into quat
quat into matrix