2X2 Rubiks Cube | Trouble with Pivot Points

I have tried several different strategies, and while my rotation function works at first, after I’ve done a handful of turns on more than one axis, something like this happens:
failboat_2
This is my function:

function rotate(row, axis) {
const pivotAt = new BABYLON.Vector3(1.5, 1.5, 1.5);
boxes.forEach(function(box, x) {
if (box.absolutePosition[ axis]== row) {
let relativePosition = pivotAt.subtract(box.position);
box.setPivotPoint(relativePosition);
box.rotation[axis] += Math.PI / 2;
}
});
}

Welcome aboard!

I think we will need a simple repro in the Playground to be able to help.

Thank you! Here you go: https://playground.babylonjs.com/#QQYHSY#4
Put the following in console to see the problem:

rotate(2, ‘y’);
rotate(1, ‘x’);

The order of the rotations matter, rotating around y then x is not the same than rotating around x then y.

Maybe you should try to save all the x/y/z transformations that have been requested and always apply them in the same order. I’m still unsure if this will work in all cases…