Each of my objects has a unique parent for X-rotation, Y-rotation, and Z-rotation. They are all interconnected in a specific order: the X-rotation object is a child of the Y-rotation object, while the Y-rotation object is a child of the Z-rotation object.
I am working on implementing a feature that allows users to rotate all objects in the scene simultaneously (contained within a single Object3D). When this main Object3D is rotated, the program needs to calculate each object's absolute position and rotation relative to the world so that it can output new values for each object accordingly.
To achieve this, I have set up the objects to move their position inside the "scene-rotator", which is an Object3D, to be their absolute position relative to the world. Now, I am trying to ensure that the rotation of each object reflects its absolute rotation relative to the world, adjusting as the "scene-rotator" is rotated. Due to issues with the setFromRotationMatrix method, I had to run it separately for each parent object to accurately retrieve the rotations.
The following code snippet captures how I aim to determine the absolute rotation of the object relative to the world:
var beforeRotForX = new THREE.Euler();
beforeRotForX.setFromRotationMatrix(objects[i].parent.matrixWorld, "ZYX");
var beforeRotForY = new THREE.Euler();
beforeRotForY.setFromRotationMatrix(objects[i].parent.parent.matrixWorld, "ZYX");
var beforeRotForZ = new THREE.Euler();
beforeRotForZ.setFromRotationMatrix(objects[i].parent.parent.parent.matrixWorld, "ZYX");
objects[i].userData.sceneBeforeRotAbs = {
x: beforeRotForX.x,
y: beforeRotForY.y,
z: beforeRotForZ.z
};
Subsequently, the program should apply the calculated absolute rotation to each object's relative rotation:
objects[i].parent.rotation.x = objects[i].userData.sceneBeforeRotAbs.x;
objects[i].parent.parent.rotation.y = objects[i].userData.sceneBeforeRotAbs.y;
objects[i].parent.parent.parent.rotation.z = objects[i].userData.sceneBeforeRotAbs.z;
While this process works correctly when the Y-rotation of the second parent falls within -90 through 90 degrees, it does not yield accurate results if the Y-rotation exceeds these bounds:
// Results of absolute world rotation when the Y-rotation of the second parent is set to 91 degrees
objects[i].userData.sceneBeforeRotAbs.x === 3.141592653589793
objects[i].userData.sceneBeforeRotAbs.y === 1.5533438924131038
objects[i].userData.sceneBeforeRotAbs.z === 0