相对于本地旋转在 cannon.js 中定位一个 body
Position a body in cannon.js relative to local rotation
我在 cannon.js 中有一个应用了四元数旋转的 body。我想相对于它的局部旋转沿向量移动它 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);
相对于世界而不是本地旋转移动 body。我想我需要在应用四元数后添加一个向量,但是 cannon.js 的文档并不是特别有用,所以我一直无法弄清楚如何去做。
使用Quaternion#vmult method to rotate a vector and 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);
我在 cannon.js 中有一个应用了四元数旋转的 body。我想相对于它的局部旋转沿向量移动它 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);
相对于世界而不是本地旋转移动 body。我想我需要在应用四元数后添加一个向量,但是 cannon.js 的文档并不是特别有用,所以我一直无法弄清楚如何去做。
使用Quaternion#vmult method to rotate a vector and 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);