Repeated Rotation of Objects

Hey guys, I have been trying to build a 3D rubiks cube simulator to go along with a solving algorithim but am having trouble with the rotation of the spatial objects that make up the cube. Each of the spatial objects is a blender model which I have imported into jmonekey. I am able to rotate them around one axis perfectly such as a front or top rotation, but if i try to do a top then a front rotation, the spatials that have already been rotated around one axis do not properly rotate around the other axis. From what I have researched it seems to be coming from the fact that the spatials are not storing their rotational state after being rotated, but I do not know how to fix this. I have tried everything from creating an entire new cube everytime the user rotates it with spatials having their original rotation applied + the new rotation and that didn’t work. I also tried adding the new rotations cumatively to the old rotations but it doesnt work. I had previosuly tried to use nodes to rotate each face but there were multiple bugs in the order of rotations as well as the problem I’m experiencing now.

The way I intend for my program to work is that the user presses a key which triggers one of the rotation methods (i posted two of those below). That method swaps spatials in the ArrayList and then triggers the rotateCubes method which will translate the cube to the correct position and then correctly rotate it (this is whats broken).

*This code will rotate the top face by 90 degrees clockwise

public void rotateTopNorm(){
		List<Integer> cubesAffected = Arrays.asList(0,1,2,9,10,11,18,19,20);
		for(int i = 0; i < cube.getCubesArray().size(); i ++){
			if (cubesAffected.contains(i)){
				cube.getCubesArray().get(i).setRotate(true);
			}
		}
		//array restructuring
		//corners
		Collections.swap(cube.getCubesArray(), 0, 2);
		Collections.swap(cube.getCubesArray(), 0, 20);
		Collections.swap(cube.getCubesArray(), 0, 18);
		
		//edges
		Collections.swap(cube.getCubesArray(), 1, 11);
		Collections.swap(cube.getCubesArray(), 1, 19);
		Collections.swap(cube.getCubesArray(), 1, 9);
		
		rotateCubes(0,90,0);
	}

*This is the rotate method that uses the rotations passed from one of the other methods to rotate the cubes

    public void rotateCubes(float rotX, float rotY, float rotZ) {
		for (int i = 0; i < cube.getCubesArray().size(); i++) {
			if (cube.getCubesArray().get(i).getRotate()) {

				cube.getCubesArray().get(i).getSpatialObject()
						.setLocalTranslation(cube.getIndividualCubeOffsets().get(i));
				cube.getCubesArray().get(i).getSpatialObject().rotate(FastMath.DEG_TO_RAD * rotX,
						FastMath.DEG_TO_RAD * rotY, FastMath.DEG_TO_RAD * rotZ);

				cube.getCubesArray().get(i).setRotate(false);
			}
		}
	}

*After 2 rotations (top turn then front turn)

Thanks for any feedback! I have been trying to solve this problem for days now.

Might be a stoupid idea but maybe worth a try:

  • have an empty node
  • determine what needs to be rotated
  • fill that node with the stuff you want to rotate
  • determine the axe of rotation
  • rotate the node on that axe

Would allow you to attach a control to the node and so you could have an “animation” of the rotation.

Idea: Treat every of the 26 sub-cubes separately. Like so:

(User or algorithm starts a rubic rotation)
Show the rotation animation (optional step)
Replace all cubes so that they don’t have a rotation anymore.
(Ready for next rubic rotation now)

You have 26 Node and every Node holds one of the 26 cubes.
Then you have a Node in the middle (for the rotation animation).
Then you attach the cubes to the new Node they now belong to.
The Node(s) store the rotation quaternion - attaching and detaching the cubes does the rest.
If you don’t show a rotation animation, then things become much easier.