admin 管理员组文章数量: 1086019
Does anyone know how I can rotate a CannonJS (the physics library) CANNON.RigidBody
? I'm trying to make the object rotate with the camera, so both are facing the same direction. I know I have to modify the quaternion, but this doesn't 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's a demo (WASD to move the red rectangle - which is what I want to rotate)
Here's the main script
At the moment it automatically rotates based on the physics. Thanks for the help!
EDIT:
I've sort of got it working now. But it doesn't rotate fully (the whole 360 degrees) and the angle it rotates isn't quite right. If someone could take a look and see what's wrong I would really appreciate it! :)
Same link as before but the rectangle/body is below the camera now, so I can see if it's rotating correctly.
I added this code to make it rotate:
mPlayer.objectBody.quaternion.y = mPlayer.yawObject.rotation.y;
mPlayer.objectBody.quaternion.w = 1;
mPlayer.objectBody.quaternion.normalize();
To save you looking through the code, mPlayer.yawObject.rotation.y
is set 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!
Does anyone know how I can rotate a CannonJS (the physics library) CANNON.RigidBody
? I'm trying to make the object rotate with the camera, so both are facing the same direction. I know I have to modify the quaternion, but this doesn't 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's a demo (WASD to move the red rectangle - which is what I want to rotate)
Here's the main script
At the moment it automatically rotates based on the physics. Thanks for the help!
EDIT:
I've sort of got it working now. But it doesn't rotate fully (the whole 360 degrees) and the angle it rotates isn't quite right. If someone could take a look and see what's wrong I would really appreciate it! :)
Same link as before but the rectangle/body is below the camera now, so I can see if it's rotating correctly.
I added this code to make it rotate:
mPlayer.objectBody.quaternion.y = mPlayer.yawObject.rotation.y;
mPlayer.objectBody.quaternion.w = 1;
mPlayer.objectBody.quaternion.normalize();
To save you looking through the code, mPlayer.yawObject.rotation.y
is set 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!
Share Improve this question edited Apr 9, 2013 at 0:29 Joey Morani asked Apr 7, 2013 at 21:06 Joey MoraniJoey Morani 26.6k33 gold badges89 silver badges132 bronze badges1 Answer
Reset to default 9Solved the problem with help from the Cannon.JS developer. I'm now using:
mPlayer.objectBody.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0), mPlayer.yawObject.rotation.y);
本文标签: javascriptHow to rotate a CannonJS RigidBodyStack Overflow
版权声明:本文标题:javascript - How to rotate a CannonJS RigidBody? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744012902a2518482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论