PDA

View Full Version : Rotation Problem with Rubik´s Cube



faMouZ*
05-23-2009, 05:35 AM
Hi all!

I have a problem with the rotations of my Rubik´s Cube and I hope someone can help me. The problem is that I dont´t know how I can save the positions of the different cubes or do the transformations in the correct order.

First I made one cube with quads. In my draw()-method i made the following transformations.

(it´s just a part of the code, each cube has the same code)

void RubiksCube::draw(void)
{
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 2.0f, -2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
...
}

void RubiksCube::move3rdRow(void)
{
rotY_2 += 90.0f;
}

If I wanna rotate the slices of my cube, the wrong cubes are rotating. But how can I solve this problem, that the correct cubes are rotating. I thougt it would be something with the GL_MODELVIEW or something. I don´t have any clue, sitting on this problem since one week.

Here is a screenshot of the cube.The cubes which i marked with a black line are rotating when i want to rotate e.g the 3rd row.

http://www.bilder-space.de/thumb/23.05NpLd5MeBpwSDMuR.JPG

greetz
faMouZ*

matchStickMan
05-24-2009, 07:48 PM
Hi famouz,

I can't see the screenshot you posted. Could you repost it?

Also your problem should be easily fixable with glPush...() and glPop..()

But to be sure, I need a larger code snippet. :)

matchStickMan
05-24-2009, 07:57 PM
oh wait, I can see the screenshot now. Somehow it didn't load up the first time.

So if the cubes marked with the black line are rotating instead of the 3rd row, would I be correct to state that these 3 cubes were originally part of the 3rd row, or something along that line?

My guess is, to fix your issue, you need to reference the cubes by their new positions instead of their original starting points.

But then again, I need a bigger code chunk to fully understand the problem.

faMouZ*
05-25-2009, 02:47 AM
Now, here is the full code of my Cube.But I forget the rotation on the z-Axis -.- Hopefully it´s not so hard to implement this.

void RubiksCube::drawCube(void)
{
glColor3f(0.0f,0.0f,0.0f);
glLineWidth(4.0f);
glutWireCube(2);

glBegin(GL_QUADS); // begin drawing a cube

// Front Face
glColor3f(0.0f,0.0f,1.0f);
glNormal3f( 0.0f, 0.0f, 1.0f); // front face points out of the screen on z.
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left

// Back Face
glColor3f(1.0f,0.0f,0.0f);
glNormal3f( 0.0f, 0.0f,-1.0f); // back face points into the screen on z.
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left

// Top Face
glColor3f(0.0f,1.0f,0.0f);
glNormal3f( 0.0f, 1.0f, 0.0f); // top face points up on y.
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right

// Bottom Face
glColor3f(1.0f,0.5f,0.0f);
glNormal3f( 0.0f, -1.0f, 0.0f); // bottom face points down on y.
glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right
glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right

// Right face
glColor3f(1.0f,1.0f,-1.0f);
glNormal3f( 1.0f, 0.0f, 0.0f); // right face points right on x.
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left

// Left Face
glColor3f(1.0f,1.0f,1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f); // left face points left on x.
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left

glEnd();

}


// Konstruktor
RubiksCube::RubiksCube(void)
{
rotX = 0.0f;
rotY = 0.0f;
rotZ = 0.0f;
}


void RubiksCube::move1stRow(void)
{
rotY_0 += 90.0f;
}

void RubiksCube::move1stRowBack(void)
{
rotY_0 += -90.0f;
}

void RubiksCube::move2ndRow(void)
{
rotY_1 += 90.0f;
}

void RubiksCube::move2ndRowBack(void)
{
rotY_1 += -90.0f;
}

void RubiksCube::move3rdRow(void)
{
rotY_2 += 90.0f;
}

void RubiksCube::move3rdRowBack(void)
{
rotY_2 += -90.0f;
}

void RubiksCube::move1stColumn(void)
{
rotX_0 += 90.0f;
}

void RubiksCube::move1stColumnBack(void)
{
rotX_0 += -90.0f;
}

void RubiksCube::move2ndColumn(void)
{
rotX_1 += 90.0f;
}

void RubiksCube::move2ndColumnBack(void)
{
rotX_1 += -90.0f;
}

void RubiksCube::move3rdColumn(void)
{
rotX_2 += 90.0f;
}

void RubiksCube::move3rdColumnBack(void)
{
rotX_2 += -90.0f;
}

void RubiksCube::moveZAxis1stRow(void)
{
rotZ_0 += 90.0f;
}

void RubiksCube::moveZAxis2ndRow(void)
{
rotZ_1 += 90.0f;
}

void RubiksCube::moveZAxis3rdRow(void)
{
rotZ_2 += 90.0f;
}

void RubiksCube::moveZAxis1stRowBack(void)
{
rotZ_0 += -90.0f;
}

void RubiksCube::moveZAxis2ndRowBack(void)
{
rotZ_1 += -90.0f;
}

void RubiksCube::moveZAxis3rdRowBack(void)
{
rotZ_2 += -90.0f;
}



// Ausgabe des Würfels
void RubiksCube::draw(void)
{

// WÜRFEL OBERE EBENE
//Würfel hinten rechts
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 2.0f, -2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 2.0f, -2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel hinten links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 2.0f, -2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel rechts vorne
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 2.0f, 2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 2.0f, 2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel vorne links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 2.0f, 2.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel links neben Zentrum
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 2.0f, 0.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel rechts neben Zentrum
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 2.0f, 0.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 2.0f, 0.0f);
glRotatef(rotY_2, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();

// WÜRFEL UNTERE EBENE
//Würfel hinten rechts
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, -2.0f, -2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -2.0f, -2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel hinten links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, -2.0f, -2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel rechts vorne
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, -2.0f, 2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -2.0f, 2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel vorne links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, -2.0f, 2.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel links neben Zentrum
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, -2.0f, 0.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel rechts neben Zentrum
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, -2.0f, 0.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -2.0f, 0.0f);
glRotatef(rotY_0, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();

// WÜRFEL MITTLERE EBENE
//Würfel rechts hinten
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 0.0f, -2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel hinten links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 0.0f, -2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel rechts vorne
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 0.0f, 2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel Mitte
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel vorne links
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 0.0f, 2.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
//Würfel links neben Zentrum
glPushMatrix();
glRotatef(rotX_0, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 0.0f, 0.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel rechts neben Zentrum
glPushMatrix();
glRotatef(rotX_2, 1.0f, 0.0f, 0.0f);
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCube();
glPopMatrix();
// Würfel im Zentrum
glPushMatrix();
glRotatef(rotX_1, 1.0f, 0.0f, 0.0f);
glRotatef(rotY_1, 0.0f, 1.0f, 0.0f);
drawCubeCenter();
glPopMatrix();

}

void RubiksCube::drawCubeCenter(void)
{
glutSolidCube(2);
}

faMouZ*
05-26-2009, 02:51 AM
It´s very strange that I can rotate a slice on the x-axis and then on the y-axis without problems.But if I want to rotate any other slice or rotate first the slices on the y-axis and then on the x-axis, the wrong cubes are rotating. PLEASE HELP ME!!!