使用Three.js将对象的旋转应用于另一个

时间:2015-04-14 11:59:29

标签: javascript three.js

我正在尝试将对象的旋转应用于另一个。我尝试了以下方法但没有成功:

//Rotates much faster than the original object's rotation
boxme1.rotateOnAxis(new t.Vector3(0,1,0), cube.rotation.y);

//The vector and the quaternion part is supposed to get the direction the
//object is facing, however when I apply the vector to a Euler to make the
//rotation, the object just disappears from the scene. I'm not even
//sure if this is the right way to use an Euler.
var vector  = new t.Vector3(0,0,-1);
vector.applyQuaternion( cube.quaternion );
var euler = new t.Euler(vector.x, vector.y, vector.z);
boxme1.rotateOnAxis(new t.Vector3(0,1,0),euler);

//If I try to set the object rotation with the Euler I got above 
//instead of using rotateOnAxis the object doesn't move at all.
boxme1.rotation = euler;

将一个物体旋转到另一个物体的正确方法是什么?问题不在于立方体旋转,因为它按预期旋转。我开始学习Three.js所以我仍然很漂亮。

提前致谢

2 个答案:

答案 0 :(得分:1)

我找到了答案

//First of all we create a matrix and put the rotation of the cube into it
rotObjectMatrix = new THREE.Matrix4();
rotObjectMatrix.makeRotationFromQuaternion(cube.quaternion);
//Next we just have to apply a rotation to the quaternion using the created matrix
boxme1.quaternion.setFromRotationMatrix(rotObjectMatrix);

我仍然不确定为什么制作boxme1.quaternion = cube.quaternion不起作用。

答案 1 :(得分:1)

boxme1.quaternion = cube.quaternion 

由于Javascript实际处理指针的方式而无法工作。将多维数据集的四元数分配给boxme1会使boxme1的任何更改都适用于多维数据集(坏),反之亦然。我会尝试使用以下之一:

最佳解决方案:

boxme1.quaternion.copy(cube.quaternion);

可替换地:

boxme1.quaterion = cube.quaterion.clone();

看起来你有一个解决方案 - 但上面可能会有更多的内存/ CPU效率。