Sign in to follow this  
Nader El Masry

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

Recommended Posts

Nader El Masry    100
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Wodzu    122
I had the same problem.

I was thinking about this and rotating cube for hours :)

Maybe this topic will be helpful for you:

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this