2014-09-26 33 views
0

分配給不同的父的另一個孩子我有一個包含一個子對象克隆旋轉,並在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); 

現在我想通過旋轉它的parent2child2相同的旋轉爲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

我認爲這可以通過從parent2child2讓世界轉動的差異,並從child1世界旋轉從其減去此來完成,以糾正的child2旋轉產生的偏移。 但是我對四元數和矩陣的認識是不好使它工作。

看看這個fiddle,瞭解我的問題。 enter image description here 非常感謝。

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進行計算?

+0

見http://stackoverflow.com/questions/23002984/three-js-computation-relative-rotation/23003560#23003560 – WestLangley 2014-09-26 15:35:29

+0

使用'Scene.Detach'/'Scene.Attach'方法我只能使它適用於x軸上的旋轉。這是Three.js(68)中的錯誤嗎? – user3271566 2014-10-06 07:00:43

+0

你不能分量減去歐拉旋轉。看看這個小提琴是否做你想要的。 http://jsfiddle.net/715v2hr1/。如果是這樣,我會發佈一個答案。小提琴有點棘手。 – WestLangley 2014-10-06 15:57:44

回答

0

所以你想要旋轉一個父對象,以便父對象的子對象具有特定的方向,並且你希望該特定方向與不同父對象的子對象的方向相匹配。呼!

這裏是做的一個方法:

// detach the children from their parents and add them to the scene 
THREE.SceneUtils.detach(child2, parent2, scene); 
THREE.SceneUtils.detach(child1, parent1, scene); 

// here is the tricky part: add the second parent as a child of its former child 
THREE.SceneUtils.attach(parent2, scene, child2); 

// copy the first child's rotation, and make sure the matrix is updated 
child2.rotation.copy(child1.rotation); 
child2.updateMatrixWorld(true); 

// detach the parent from the child and add it to the scene 
THREE.SceneUtils.detach(parent2, child2, scene); 

// put the children back where the were before 
THREE.SceneUtils.attach(child2, scene, parent2); 
THREE.SceneUtils.attach(child1, scene, parent1); 

這裏是一個小提琴:http://jsfiddle.net/715v2hr1/

three.js所r.68