问题
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