2017-10-18 58 views
0

我有一个cannon.js中的物体,它已经应用了四元数旋转。我想将它沿着一个向量相对于它的局部旋转移动100个单位。在Cannon.js中定位一个物体相对于局部旋转

let body = new CANNON.Body({ mass: 0 }); 
body.quaternion.setFromAxisAngle(new CANNON.Vec3(0,0,1),(2*Math.PI)/6); 
body.position.set(0,0,100); //this is wrong 

使用body.position.set(x, y, z);相对于世界,而不是局部旋转的身体动作。我想我需要在应用四元数之后添加一个向量,但cannon.js的文档并不是特别有用,所以我还没有能够解决如何去做。

+0

我不熟悉cannon.js,但我认为你必须使用'transformAllPoints'。见[**这**](https://github.com/schteppe/cannon.js/issues/58) – Rabbid76

回答

1

使用Quaternion#vmult方法旋转矢量,并将Vec3#add添加到位置结果。

let body = new CANNON.Body({ mass: 0 }); 
body.quaternion.setFromAxisAngle(new CANNON.Vec3(0,0,1),(2*Math.PI)/6); 
let relativeVector = new CANNON.Vec3(0,0,100); 

// Use quaternion to rotate the relative vector, store result in same vector 
body.quaternion.vmult(relativeVector, relativeVector); 

// Add position and relative vector, store in body.position 
body.position.vadd(relativeVector, body.position);