I have a bug where I copy the Quaternion
of one object and pass it to a function where it gets applied to another object to keep their rotations in sync. I cannot get the Quaternion
to apply to the second object.
Given object 1 is msh
and object 2 is msh2
, this code will not apply the rotation of msh
to msh2
var rot = new THREE.Quaternion().copy(msh.rotation);
msh2.rotation.copy(rot);
You can see this further in this Stack Snippet which contains the problem in the smallest reproducable fashion (but isn't the exact code I'm working with in my real project)
var renderer = new THREE.WebGLRenderer({canvas : $("#can")[0]});
renderer.setSize($("#can").width(), $("#can").height());
var cam = new THREE.PerspectiveCamera(45, $("#can").width() / $("#can").height(), 0.1, 100);
cam.position.set(0,2,6);
cam.quaternion.multiply(new THREE.Quaternion().setFromAxisAngle( new THREE.Vector3( 1, 0, 0 ), -Math.PI/8 ));
var scene = new THREE.Scene();
var geo = new THREE.BoxGeometry(1,1,1);
var mat = new THREE.MeshBasicMaterial({color : 0xff0000});
var msh = new THREE.Mesh(geo,mat);
scene.add(msh);
geo = new THREE.BoxGeometry(1,2,1);
mat = new THREE.MeshBasicMaterial({color : 0xff00ff});
var msh2 = new THREE.Mesh(geo,mat);
msh2.position.set(2,0,0);
scene.add(msh2);
function render() {
requestAnimationFrame( render );
msh.rotateX(0.001);
msh.rotateY(0.002);
//For some reason, this doesn't work??
var rot = new THREE.Quaternion().copy(msh.rotation);
msh2.rotation.copy(rot);
//Yet this does (but it doesn't fit the flow of my
//original project because I don't want to pass
//objects around.
//msh2.rotation.copy(msh.rotation);
renderer.render( scene, cam );
}
render();
<script src="https://cdn.rawgit.com/mrdoob/three.js/dev/build/three.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="can" width="400" height="300"></canvas>
Am I missing something here? I do this with Vector3
all the time so I don't see why I can't do it here...
msh.rotation is an instance of THREE.Euler, but you're trying to copy to/from THREE.Quaternion. msh2.rotation.copy(msh.rotation) should work fine, as would msh2.quaternion.copy(msh.quaternion)