From 0d4d8ca0e6443b405fbe04c4290fa0bcbb33800a Mon Sep 17 00:00:00 2001 From: RogeredBacon Date: Fri, 8 Sep 2017 16:02:51 +0000 Subject: [PATCH] fixed up physics and bullets and planes and crates #4 #5 --- src/controls.js | 4 ++-- src/init/init.js | 4 +++- src/letsMove.js | 12 ++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/controls.js b/src/controls.js index c24fa9a..2a41b61 100644 --- a/src/controls.js +++ b/src/controls.js @@ -27,7 +27,7 @@ const init = () => { movements.canJump = false; } break; - case 17: + case 70: movements.shooting = true; break; } @@ -50,7 +50,7 @@ const init = () => { case 68: // d movements.right = false; break; - case 17: + case 70: movements.shooting = false; break; } diff --git a/src/init/init.js b/src/init/init.js index d3d974e..79cfe11 100644 --- a/src/init/init.js +++ b/src/init/init.js @@ -29,9 +29,10 @@ const init = () => { // Cannon init const world = new CANNON.World(); - world.gravity.set(0, -9.82, 0); + world.gravity.set(0, -20, 0); world.broadphase = new CANNON.NaiveBroadphase(); world.solver.iterations = 10; + world.solver = new CANNON.SplitSolver(new CANNON.GSSolver()); // shape is shape of geometry/wireframe const shape = new CANNON.Box(new CANNON.Vec3(10, 10, 10)); // body is it being effected by forces. @@ -45,6 +46,7 @@ const init = () => { world.addBody(body); const groundShape = new CANNON.Plane(); + groundShape.collisionResponse = true; const groundBody = new CANNON.Body({ mass: 0 }); groundBody.addShape(groundShape); groundBody.position.set(0, -14, 0); diff --git a/src/letsMove.js b/src/letsMove.js index 36f7f42..aebc076 100644 --- a/src/letsMove.js +++ b/src/letsMove.js @@ -44,6 +44,7 @@ module.exports = function ( if (bullets[index].alive == false) { cannonBullets.splice(index, 1); bullets.splice(index, 1); + world.remove(cannonBullets[index]); continue; } // bullets[index].position.add(bullets[index].velocity); @@ -54,14 +55,15 @@ module.exports = function ( if (movements.shooting) { // shoot.bullet(scene); const bullet = new THREE.Mesh( - new THREE.SphereGeometry(0.5, 8, 8), + new THREE.SphereGeometry(1.3, 8, 8), new THREE.MeshBasicMaterial(), ); - const shape = new CANNON.Sphere(new CANNON.Vec3(0.5)); + const shape = new CANNON.Sphere(new CANNON.Vec3(1.3)); const body = new CANNON.Body({ - mass: 1, + mass: 5, }); + body.linearDamping = 0; body.addShape(shape); body.position.set( raycaster.ray.origin.x, @@ -87,12 +89,14 @@ module.exports = function ( setTimeout(() => { bullet.alive = false; scene.remove(bullet); - world.remove(world.bodies[world.bodies.length - 1]); }, 3000); cannonBullets.push(world.bodies[world.bodies.length - 1]); bullets.push(bullet); scene.add(bullet); cannonBullets[cannonBullets.length - 1].velocity = bullet.velocity; + cannonBullets[cannonBullets.length - 1].velocity.x *= 150; + cannonBullets[cannonBullets.length - 1].velocity.y += 10; + cannonBullets[cannonBullets.length - 1].velocity.z *= 150; } if (movements.forward) { velocity.z -= 2000.0 * delta; }