I know this might be market as duplicate, but no one answered my one week's ago question, so I ask again:
I'm making a simple 3D puzzle, where you have to rotate a n*n number of cubes, you do that using the x, y, and z axis.
The problem is, that whenever I rotate a cube, for example on y axis (turns rightward), and then I want to rotate it on the
x axis, it won't go upward as expected, but it would rotate to the left, like if it was on the z axis.
I believe the problem are the axes themselves, so I'm asking: how do I not let the axes move along with the shape, and
make them keep their own position?
Here's the code for the puzzle cube rotation:
// This is the x axis rotation,
// the others are the same, just
// with Y and Z instead of X
private void turnX()
{
final double inc = HALF_PI / (frameRate / 6); // Increment step for the animation
if (angX < endX)
angX += inc;
if (angX >= endX)
{
angX = endX;
endX += HALF_PI;
rotateX = false;
}
}
// Capture mouse input
public void mouseInput()
{
// x,y distance from the start position of the cube to the actual position
// (translated to width / 2, height / 2)
final float xDistance = position.x + (width - l * puzzleWidth) / 2 + l * (puzzleWidth - 2);
final float yDistance = position.y + (height - l * puzzleHeight) / 2 + l * (puzzleHeight - 2);
if ((mouseX > xDistance && mouseX < xDistance + l) &&
(mouseY > yDistance && mouseY < yDistance + l))
{
if (mouseButton == RIGHT && !rotateY && !rotateZ)
rotateX = true;
else if (mouseButton == LEFT && !rotateX && !rotateZ)
rotateY = true;
else if (mouseButton == CENTER && !rotateX && !rotateY)
rotateZ = true;
}
}
Any help would be appreciated, and I would like, if possible, make it without any third-party libraries.
EDIT
As @laancelot asked, here's the drawing part of the puzzle cube, it's kind of long with its all beginShape() and endShape():
final float h = (float) 1 / puzzleWidth;
final float w = (float) 1 / puzzleHeight;
pushMatrix();
translate(position.x + (l / 2) * (puzzleWidth - 3),
position.y + (l / 2) * (puzzleHeight - 3));
rotateY(angY);
rotateX(angX);
rotateZ(angZ);
// Front
beginShape();
texture(textures[0]);
vertex(-l/2, -l/2, l/2, u, v); // Alto-sinistra
vertex(l/2, -l/2, l/2, u + w, v); // Alto-destra
vertex(l/2, l/2, l/2, u + w, v + h); // Basso-destra
vertex(-l/2, l/2, l/2, u, v + h); // Basso-sinistra
endShape(CLOSE);
// Rear
beginShape();
texture(textures[1]);
vertex(-l/2, -l/2, -l/2, u + w, v);
vertex(l/2, -l/2, -l/2, u, v);
vertex(l/2, l/2, -l/2, u, v + h);
vertex(-l/2, l/2, -l/2, u + w, v + h);
endShape(CLOSE);
// Right
beginShape();
texture(textures[2]);
vertex(l/2, -l/2, l/2, u, v);
vertex(l/2, -l/2, -l/2, u + w, v);
vertex(l/2, l/2, -l/2, u + w, v + h);
vertex(l/2, l/2, l/2, u, v + h);
endShape(CLOSE);
// Left
beginShape();
texture(textures[3]);
vertex(-l/2, -l/2, l/2, u + w, v);
vertex(-l/2, -l/2, -l/2, u, v);
vertex(-l/2, l/2, -l/2, u, v + h);
vertex(-l/2, l/2, l/2, u + w, v + h);
endShape(CLOSE);
// Top
beginShape();
texture(textures[4]);
vertex(-l/2, -l/2, l/2, u, v + h);
vertex(-l/2, -l/2, -l/2, u, v);
vertex(l/2, -l/2, -l/2, u + w, v);
vertex(l/2, -l/2, l/2, u + w, v + h);
endShape(CLOSE);
// Bottom
beginShape();
texture(textures[5]);
vertex(l/2, l/2, l/2, u + w, v);
vertex(l/2, l/2, -l/2, u + w, v + h);
vertex(-l/2, l/2, -l/2, u, v + h);
vertex(-l/2, l/2, l/2, u, v);
endShape(CLOSE);
popMatrix();