分配給不同的父的另一個孩子我有一個包含一個子對象克隆旋轉,並在three.js所
var parent1 = new THREE.Object3D();
parent1.rotation.set(1,2,3);
scene.add(parent1);
var child1= new THREE.Object3D();
child1.rotation.set(0.5,0.1,0.3);
parent.add(child1);
和含有另一個孩子另一個父父對象。
var parent2= new THREE.Object3D();
scene.add(parent2);
var child2= new THREE.Object3D();
childC2.rotation.set(3,1.15,2);
parent.add(child2);
現在我想通過旋轉它的parent2
給child2
相同的旋轉爲child1
。
如前所述here,我可以從我原來child1
讓世界旋轉這樣的:
var quaternion = new THREE.Quaternion();
child1.matrixWorld.decompose(new THREE.Vector3(), quaternion, new THREE.Vector3());
,並將其應用到我的parent2
這樣的:
parent2.quaternion.copy(quaternion);
現在parent2
具有相同像原始的child1
旋轉,但我想它的孩子child2
有像child1
一樣的旋轉(只有通過修改parent2
)
我認爲這可以通過從parent2
和child2
讓世界轉動的差異,並從child1
世界旋轉從其減去此來完成,以糾正的child2
旋轉產生的偏移。 但是我對四元數和矩陣的認識是不好使它工作。
看看這個fiddle,瞭解我的問題。 非常感謝。
UPDATE
我試過WestLangley的方法。如果我的對象只圍繞x軸的旋轉,它的工作原理是這樣的(see fiddle)
//calculate the relative rotation of child2
THREE.SceneUtils.detach(child2, parent2, scene);
var rotX=parent2.rotation.x-child2.rotation.x;
var rotY=parent2.rotation.y-child2.rotation.y;
var rotZ=parent2.rotation.z-child2.rotation.z;
THREE.SceneUtils.attach(child2, scene, parent2);
//combine the relative rotation of child2 and the 'world' rotation of child1
THREE.SceneUtils.detach(child1, parent1, scene);
rotX+=child1.rotation.x;
rotY+=child1.rotation.y;
rotZ+=child1.rotation.z;
THREE.SceneUtils.attach(child1, scene, parent1);
parent2.rotation.set(rotX,rotY,rotZ);//assign it to parent2
如果我的對象是圍繞X,Y旋轉,Z這不再(fiddle)工作。這是萬向節鎖的問題嗎?我是否需要用四元數而不是Euler Objects進行計算?
見http://stackoverflow.com/questions/23002984/three-js-computation-relative-rotation/23003560#23003560 – WestLangley 2014-09-26 15:35:29
使用'Scene.Detach'/'Scene.Attach'方法我只能使它適用於x軸上的旋轉。這是Three.js(68)中的錯誤嗎? – user3271566 2014-10-06 07:00:43
你不能分量減去歐拉旋轉。看看這個小提琴是否做你想要的。 http://jsfiddle.net/715v2hr1/。如果是這樣,我會發佈一個答案。小提琴有點棘手。 – WestLangley 2014-10-06 15:57:44