0

I am making a rubix cube game in OpenGL and I have run into a problem with rotations. When doing multiple rotations (x, y, z) the axis gets rotated as well (ie when doing an x-axis rotation of 90 or 270 deg the y-axis becomes the z-axis and z-axis becomes the y-axis).
Is there any way to stop the axis from rotating but allowing the object to translate or is there a trick to get around this.

Here is a small part of my code for the translations.

glPushMatrix();
	glRotatef(xrot, 1.0, 0.0, 0.0); //rotates the unit cube into place
	glRotatef(yrot, 0.0, 1.0, 0.0);
	glRotatef(zrot, 0.0, 0.0, 1.0);
		
	glTranslatef(xoff, yoff, zoff);  //translates the unit cube into place
		
	glRotatef(xrotset, 1.0, 0.0, 0.0); //rotates the unit cube in its place 
	glRotatef(yrotset, 0.0, 1.0, 0.0);
	glRotatef(zrotset, 0.0, 0.0, 1.0);

	glBegin(GL_QUADS); //draws the cube
		for( int i = 0; i < unitIndices.size(); i++ )
		{
			GLint *curIndices;
			curIndices = unitIndices[i].getVals();
			glColor3fv(unitColors[i].getVals());
			glNormal3fv(unitNorms[i].getVals());
			glVertex3fv(unitVerts[curIndices[0]].getVals());
			glVertex3fv(unitVerts[curIndices[1]].getVals());
			glVertex3fv(unitVerts[curIndices[2]].getVals());
			glVertex3fv(unitVerts[curIndices[3]].getVals());
		}	
	glEnd();
glPopMatrix();

This is just how I have the drawing for each unit cube 27 of these make up the "rubix cube". The problem lies in the xrot, yrot and zrot lines.

Thanks.

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by firstPerson
1

>>Is there any way to stop the axis from rotating but allowing the object to translate or is there a trick to get around this.

Yes, use push and pop matrix for that, as well as return the original coordinate back in place , see below :

//save current matrix for only the code between the push/pop
glPushMatrix();
//rotation code here
glPopMatrix();

And when you want to reset coordinates, after translation to an object,

glTranslatef(x,y,z); //moves object to a certain coordinates
//blah blah
glTranslatef(-x,-y,-z); //resets the coordinate back to its regular position, thus the blah blah has its own relative positioning.
0

The program runs perfectly and draws fine but when you do a x rotation the y axis changes so then when you do a y rotation on that it ends up rotating the z axis. I kinda dropped this mini project for now just because I see now if I want it to work I have to change a whole bunch of stuff.

0

Just remember that when rotating an object, you should rotate the world in opposite direction to make the world coordinate the same relative to that object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.