相对于局部旋转.js将主体放置在大炮中



我有一个大炮中的身体.js它应用了四元数旋转。我想相对于它的局部旋转沿向量移动 100 个单位。

例如

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并不是特别有用,所以我无法弄清楚如何做到这一点。

使用 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);

最新更新