简体   繁体   English

在cannon.js中相对于局部旋转定位身体

[英]Position a body in cannon.js relative to local rotation

I have a body in cannon.js that has a quaternion rotation applied to it. 我在cannon.js中有一个应用于四元数旋转的主体。 I want to move it 100 units along a vector relative to it's local rotation. 我想沿着向量相对于其局部旋转将其移动100个单位。

Eg 例如

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

Using body.position.set(x, y, z); 使用body.position.set(x, y, z); moves the body relative to the world rather than the local rotation. 使身体相对于世界而不是局部旋转。 I think I need to add a vector after applying the quaternion to it, but the docs for cannon.js aren't particularly helpful so I've not been able to work out how to do it. 我认为在将四元数应用到它之后需要添加一个向量,但是cannon.js的文档并不是特别有用,因此我无法弄清楚该如何做。

Use the Quaternion#vmult method to rotate a vector and Vec3#add to add the result to the position. 使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM