1

I'm attempting to create a rubiks cube simulator using JavaFX and am having issues with rotating more than one face which causes issues with conflicting parts of the cube.

Starting up the fxml file: the cube is imported via different .dae files which are saved into an object and then the objects are put into an arraylist.

"Home Screen" Displayed

Clicking on the buttons on the left (Specifically F and R) will rotate the corresponding face of the cube (Front(Green), Right(Red)...)

Clicking on the F button once calls this section of code below where the current segments of the cube on the front face are received and rotated clockwise around the centre of the cube as one by adding them to a group. This results in the expected outcome being displayed

public void FButtonA(MouseEvent me) {
        if (me.isPrimaryButtonDown()) {
            ArrayList<CubePiece> group = getGroup("F");
            Group nodeGroup = new Group();

            Rotate rotate = new Rotate();
            rotate.setPivotZ(-120);
            rotate.setAxis(Rotate.Z_AXIS);
            rotate.setAngle(90);

            for (int i = 0; i < group.size(); i++) {
                nodeGroup.getChildren().add(group.get(i).node);
            }
            nodeGroup.getTransforms().add(rotate);
            PlayCubePane.getChildren().add(nodeGroup);

            moveGroupNodeMap("F", true);

        } else if (me.isSecondaryButtonDown()) {
            ArrayList<CubePiece> group = getGroup("F");
            Group nodeGroup = new Group();

            Rotate rotate = new Rotate();
            rotate.setPivotZ(-120);
            rotate.setAxis(Rotate.Z_AXIS);
            rotate.setAngle(-90);

            for (int i = 0; i < group.size(); i++) {
                nodeGroup.getChildren().add(group.get(i).node);
            }
            nodeGroup.getTransforms().add(rotate);
            PlayCubePane.getChildren().add(nodeGroup);

            moveGroupNodeMap("F", false);
        }
    }

The issue arises when I attempt to rotate another face of the cube once I've already rotated the front face. For example: clicking the R button once calls this similar section of code but causes the few pieces of the cube which are part of the group already rotated to not follow the same pattern as those untouched.

This screenshot shows what is displayed: all other parts of the right face have been rotated as expected but the 3 parts that were previously rotated are on the top face wrongly rotated 90 degrees anti clockwise causing a gap to form. As I rotate the individual cube pieces as a group, I'm unable to counter rotate them to fix this and even if I could the logic for that would be seemingly difficult especially when the cube is rotated involving more faces.

Any help or guidance will be greatly appreciated as I've been racking my brain with this for many days. If you'd like me to post other parts of my code then please let me know.

Disclaimer (ish): I did originally have animations for the rotations of the faces but have removed them whilst I sort this issue.

luk3
  • 11
  • 1
  • 3
    This is pretty complex topic to be answered in a StackOverflow question. Study [RubiksFX](https://github.com/jperedadnr/RubikFX) source code and [Jpereda’s blog](http://jperedadnr.blogspot.com/2014/04/rubikfx-solving-rubiks-cube-with-javafx.html). – jewelsea Jan 03 '22 at 08:01

0 Answers0