我在cannon.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的文档并不特别有用,所以我还没有想出如何实现它。
发布于 2017-10-19 08:26:03
使用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);https://stackoverflow.com/questions/46810707
复制相似问题