Rotation causes shift in position when object is assymetric around origin

I am building a rubik cube simulation based the great work of others I found here Animate rotateOnWorldAxis - #5 by PavelBoytchev.

However, I am trying to generalize the logic to an n-sized rubik cube and not just 3 sided.
I have got almost everything except for even sided cubes the rotation seems to cause a weird shift in position of the layer. I suspect this is due to the assymetry around origin. I tried a few tricks to try to translate to get it to come back to the correct position but it does not seem to work.

Before rotation:

After rotation:

Note: When building the cube I am trying to center it to the scene so it starts with (- size / 2) and goes till ((size / 2) - 1), so for even 4 sided one this would be from -2 to 1.

  1. If there are some, doublecheck the parts of that use rounding - like Math.floor and Math.ceil. L
  2. Use Box3 to debug origins of the parts you’re trying to rotate.
1 Like

As I was writing this question, I had an eureka moment. The issue is because of the asymmetry of the coordinates for even sided rubik cubes. So what if I shifted the center by 0.5 such that the coordinates go from [-1.5, -0.5, 0.5, 1.5], instead of [-2, -1, 0, 1] ? As I switched to this logic, the rotation issue resolved itself. Since now the cube is symmetric around the origin, the rotation does not cause this shift in position anymore.