3

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();
Arch
  • 507
  • 3
  • 18
  • Could you post the part where you draw the cube too? (and then let me know so I get a notification) – laancelot Jan 30 '20 at 04:05
  • I still can't figure it out. If you're willing to share the whole thing I'll give another go, though. I would need a either the whole thing or at least a runnable example. (unless you already figured it out, in which case I'll be more than happy) – laancelot Feb 02 '20 at 02:39
  • @laancelot I kind of figured out. Actually I moved the cubes and the images as well with lots of ifs checking the orientation of the cube – Gian Marco Cialdi Feb 02 '20 at 10:53
  • This is great, then! Good luck with what's next! – laancelot Feb 02 '20 at 16:34
  • Hi @Gian Marco Cialdi, since you have found a solution it would be great if you could answer your question for anyone else who may be interested. You can answer your own question and accept the answer as well to mark it as answered. – Arch Jul 02 '20 at 16:06

0 Answers0