相对于局部旋转在 cannon.js 中定位身体 [英] Position a body in cannon.js relative to local rotation

查看:50
本文介绍了相对于局部旋转在 cannon.js 中定位身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a body in cannon.js that has a quaternion rotation applied to it. I want to move it 100 units along a vector relative to it's local rotation.

例如

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 的文档并不是特别有用,所以我无法弄清楚如何去做.

Using 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.

推荐答案

使用 Quaternion#vmult 旋转向量的方法和 Vec3#add 将结果添加到位置.

Use the Quaternion#vmult method to rotate a vector and Vec3#add to add the result to the position.

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 中定位身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆