'Moving cannonJS object in 3D according to its quaternion
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.
Solution 1:[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);
Solution 2:[2]
It's possible I'm missing something, but the accepted answer seems to answer a different question.
For me forward acceleration was as simple as doing this:
const enginePower = new CANNON.Vec3(0, 0, 500); // engine strength
const origin = new CANNON.Vec3(0, 0, 0); // where the ship is pushed from
physicsBody.applyLocalImpulse(enginePower, origin); // push it "forward"
Note that my ship points in the z-direction after import, so the power (500) is at z. You may need to change yours depending on your own ship's "forward" direction. As your ship rotates in the world, that "forward" direction does not change relative to the ship (but does change relative to the world) so things should work consistently.
For rotation, you can do something like this:
const turnLeft = new CANNON.Vec3(0, 2500, 0); // yaw, left
const turnRight = new CANNON.Vec3(0, -2500, 0); // yaw, right
// etc..
physicsBody.applyTorque(turnLeft);
As a bonus, if you want a constant top speed without worrying too much about realism, you can add linear and angular damps to your body. Note that this may require increasing engine and torque, depending on your taste.
physicsBody.linearDamping = 0.9;
physicsBody.angularDamping = 0.9;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Don McCurdy |
Solution 2 |