Problem with rotating a Rubik's Cube

Hello Everybody,

I’ve been breaking my head over this problem:

I am making a rubik’s cube and want users to be able to solve it by clicking and dragging.

At this point i am as far that I can ‘pick’ a cube and drag ‘up’,‘down’,‘left’,‘right’.
When I do this, the algoritm will know which cube has been picked and which direction it wants to go.
All 8 cubes adjacent to it will turn also in the same rotation.

My problem is that I can only rotate one Axes, for example the Z-axes.
When I then rotate over the Y-axes I can make two things happen:

  • the cubes that have been rotated over a different axe will rotate perfectly over the new axe, the cubes that haven’t been rotated yet will rotate very weard.
  • the cubes that have not been rotated over a different axe will rotate perfectly over the new axe, the cubes that have been rotated already will rotate very weard.

Am I missing something about the quaternion rules?

Rubiks class:

[java]private void rotate(int curX, int curY, int curZ, int direction)
{
if(direction == Data.EAST)
for(int x = 0; x < 3; x++)
for(int z = 0; z < 3; z++)
cube[x][curY][z].rot(Vector3f.UNIT_Y,-1);

    if(direction == Data.WEST)
        for(int x = 0; x &lt; 3; x++)
            for(int z = 0; z &lt; 3; z++)
                cube[x][curY][z].rot(Vector3f.UNIT_Y,1);
    
    if(direction == Data.NORTH)
        for(int x = 0; x &lt; 3; x++)
            for(int y = 0; y &lt; 3; y++)
            {
                cube[x][y][curZ].rot(Vector3f.UNIT_Z,1);
            }
    
    if(direction == Data.SOUTH)
        for(int x = 0; x &lt; 3; x++)
            for(int y = 0; y &lt; 3; y++)
            {
                cube[x][y][curZ].rot(Vector3f.UNIT_Z,-1);
            }
}[/java]

Cube class:

[java]public void rot(Vector3f rot, int direction)
{
Quaternion quat = new Quaternion();
quat.fromAngleAxis(direction * FastMath.HALF_PI/16,rot);

    this.setLocalRotation(this.getLocalRotation().mult(quat));
}[/java]

Sorry, in the above code, the following happens:

  • the cubes that have not been rotated over a different axe will rotate perfectly over the new axe, the cubes that have been rotated already will rotate very weard.
    • to be more precise about ‘weard’: the already rotated cubes will rotate 90 degrees over the X-Axes that hasn’t been used in both rotations.