How to rotate CannonJS RigidBody?

Does anyone know how I can rotate CannonJS (physical library) CANNON.RigidBody? I am trying to make the object rotate with the camera, so that both of them are directed in the same direction. I know that I need to change the quaternion, but this does not work correctly:

mPlayer.objectBody.quaternion.set(0, mPlayer.yawObject.rotation.y, 0, 1);

It also changes the Y-position of the object, not just the rotation.

Here is the demo (WASD to move the red rectangle - this is what I want to rotate)
Here is the main script

At the moment, it automatically rotates based on physics. Thanks for the help!

EDIT:
I have a job now. But he does not rotate completely (all 360 degrees) and the angle he rotates is not quite right. If someone can take a look and see that I'm wrong, I would really appreciate it! :)

The same link as before , but now the rectangle / body is below the camera, so I see if it rotates correctly.

I added this code so that it rotates:

mPlayer.objectBody.quaternion.y = mPlayer.yawObject.rotation.y;
mPlayer.objectBody.quaternion.w = 1;
mPlayer.objectBody.quaternion.normalize();  

To save you from viewing the code, mPlayer.yawObject.rotation.yset in the MouseMove event:

var onMouseMove = function ( event ) {
    var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
    var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;

    mPlayer.yawObject.rotation.y -= movementX * 0.002;
    mPlayer.pitchObject.rotation.x -= movementY * 0.002;

    mPlayer.pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, mPlayer.pitchObject.rotation.x ) );
};

Thanks again!

+5
source share
1 answer

Solved the problem with the help of the developer Cannon.JS. I am now using:

mPlayer.objectBody.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0), mPlayer.yawObject.rotation.y);
+6
source

All Articles