I'm currently in the process of constructing a Rubik's Cube using Three.js. My method involves grouping all the small cubes that need to be rotated together and then performing the rotation on the group as a whole. To determine the pivot point for the rotation, I came across this useful function:
//Turn Group Around Center function
THREE.Object3D.prototype.rotateAroundWorldAxis = function () {
// rotate object around axis in world space (the axis passes through point)
// axis is assumed to be normalized
// assumes object does not have a rotated parent
var q = new THREE.Quaternion();
return function rotateAroundWorldAxis(point, axis, angle) {
q.setFromAxisAngle(axis, angle);
this.applyQuaternion(q);
this.position.sub(point);
this.position.applyQuaternion(q);
this.position.add(point);
return this;
}
}();
While the function works perfectly for a single rotation, it seems to fail when applied multiple times:
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
No additional change occurs during the second rotation.
This issue may be due to the fact that the function only applies a new rotation instead of adding to the existing one. (rotation = 90 rather than rotation += 90)
So, my question is: How can I modify the function to retain previous rotations?