I´m trying to build a Rubik's Cube using Three.js. In order to turn one side I put all of the small cubes that have to be turned in a group and then rotate it. To specify a turning point, I use this function I found:
//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;
}
}();
When I use it to rotate the group once, everything works, but when I use it twice:
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
nothing happens on the second turn.
I guess this is because the function only applies a rotation to an unturned object instead of adding it. (rotation = 90 instead of rotation += 90)
My question: How could I solve this and let the function remember the previous rotations?