I'm eager to develop a virtual reality shooting game for browsers, utilizing Three.js and Ammo.js for the physics engine and rigid bodies. Although I've successfully set up the VR headset, controllers, and loaded models, I encountered an issue with the bullets not firing correctly from the gun as intended. In a previous attempt without using Ammo.js, I relied on "vector.applyQuaternion" from the Three.js documentation, which effectively launched the bullets from the gun's top. However, I am struggling to find a similar solution while incorporating ammo.js into the project.
Code snippet without ammo.js
... function handleController( controller ) {
if ( controller1.userData.isSelecting ) {
bullet1.position.set( controller1.position.x , controller1.position.y + 0.018 , controller1.position.z -0.01);
bullet1.userData.velocity.x = 0;
bullet1.userData.velocity.y = 10;
bullet1.userData.velocity.z = 10;
bullet1.userData.velocity.applyQuaternion( controller1.quaternion );
scene.add(bullet1);
}
if ( controller2.userData.isSelecting ) {
bullet2.position.set( controller2.position.x , controller2.position.y + 0.018 , controller2.position.z -0.01 );
bullet2.userData.velocity.x = 0;
bullet2.userData.velocity.y = 10;
bullet2.userData.velocity.z = 10;
bullet2.userData.velocity.applyQuaternion( controller2.quaternion );
scene.add(bullet2);
}
} ...
function render() {
handleController( controller1 );
handleController( controller2 );
var delta = clock.getDelta()
bullet1.position.x -= bullet1.userData.velocity.x * delta;
bullet1.position.y -= bullet1.userData.velocity.y * delta;
bullet1.position.z -= bullet1.userData.velocity.z * delta;
bullet2.position.x -= bullet2.userData.velocity.x * delta;
bullet2.position.y -= bullet2.userData.velocity.y * delta;
bullet2.position.z -= bullet2.userData.velocity.z * delta;
renderer.render( scene, camera );
}
Code with ammo.js
... // The code provided in this section focuses on creating rigid bodies for bullets using Ammo.js
The outcome is that the bullets are generated but exhibit movement solely along the z-axis. Specifically, the velocity of bullet2 varies upon moving the controller along the x-axis, indicating that the bullets are solely restricted to the z-axis motion. While bullet1 aligns with movements across all axes, it fails to follow the controller's rotation. Furthermore, attempting to incorporate quaternion rotation like in the previous example results in an error due to the absence of LinearVelocity property. Instead, only getLinearVelocity and setLinearVelocity methods are supported in ammo.js.