Moving cannonJS object in 3D according to its quaternion

岁酱吖の 提交于 2021-02-08 03:50:44

问题


I would like to achieve a space ship like move on a cannonJS body. (Im really a beginner) I found some examples but none of them are exactly what I'm looking for.

As i know the correct way to move an object, is to change its velocity.

Here's what I have done here: http://codepen.io/Tomo0613/pen/xVjqqK enter code here But i definitely have problems with understanding quaternions.

Is there a way to update, the vector of the body according to its quaternion as i rotate it, or that's always related to the world? Resulting: when the body is accelerating only on the Z axis, it would always move in the direction, where it's facing.


回答1:


The velocity on the Body is always in world coordinates. For this case, you probably want to keep track of the local velocity (e.g. vec(0,0,+1)) in a variable, and then convert that to world velocity and apply it to the Body every time rotation changes.

var localVelocity = new CANNON.Vec3(0, 0, 1);
var worldVelocity = body.quaternion.vmult(localVelocity);
body.velocity.copy(worldVelocity);

Or, a faster but harder-to-read version of the same code:

var localVelocity = new CANNON.Vec3(0, 0, 1);
body.quaternion.vmult(localVelocity, body.velocity);


来源:https://stackoverflow.com/questions/36695768/moving-cannonjs-object-in-3d-according-to-its-quaternion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!