I want to match the rotation of a reference object in three.js. The object3d I want to rotate and the reference object might have different parents in different local transforms, but I want them to align rotation-wise in the global space.
function matchRotationOfReference(myObj3D, referenceObj3D) {
// what do I do here?
}
I know how to do this for position vectors like so:
var pos = new THREE.Vector3();
function matchPositionOfReference(myObj3D, referenceObj3D) {
referenceObj3D.updateMatrixWorld();
pos.setFromMatrixPosition(referenceObj3D.matrixWorld);
myObj3D.parentEl.worldToLocal(pos);
myObj3D.position.copy(pos);
}
Note: I'd normally ask what you have tried so far, because it sounds like a fairly easy thing to accomplish. But when I tried it myself, I wasn't able to make it work, and I had to jump through several hoops to make it work correctly.
In the example code below, I start with two
Group
s, each containing a cube. EachGroup
also has its own transformation/rotation. This makes the cubes initially face in different directions.In the
animate
function, you can see when the "Spin" checkbox is checked, I tear apart (decompose
) theredCube
's world matrix, saving itsquaternion
(used for rotation) intotempQ
. I do the same thing togreenCube
. I then re-build (compose
)greenCube
's world matrix, using its original translation and scale, but usingredCube
's quaternion. (And remember, this is the world quaternion, so it's not affected by local transformations.)This works because I took a direct shortcut to the most important part of transforming the object: its world matrix. I disabled updating matrices across the board, which removes a lot of the convenience of three.js, but it allowed me to work directly with the world matrices without anything overwriting them.
Here's hoping someone can find a more elegant solution for you.