UNPKG

incheon

Version:

A Node.js based real-time game server

60 lines (49 loc) 1.72 kB
'use strict'; const PhysicsEngine = require('./PhysicsEngine'); const CANNON = require('cannon'); /** * CannonPhysicsEngine is a three-dimensional lightweight physics engine */ class CannonPhysicsEngine extends PhysicsEngine { init(options) { super.init(options); this.options.dt = this.options.dt || (1 / 60); let world = this.world = new CANNON.World(); world.quatNormalizeSkip = 0; world.quatNormalizeFast = false; world.gravity.set(0, -10, 0); world.broadphase = new CANNON.NaiveBroadphase(); this.CANNON = CANNON; } // entry point for a single step of the Simple Physics step(objectFilter) { this.world.step(this.options.dt); } addSphere(radius, mass) { let shape = new CANNON.Sphere(radius); let body = new CANNON.Body({ mass, shape }); body.position.set(0, 0, 0); this.world.addBody(body); return body; } addBox(x, y, z, mass, friction) { let shape = new CANNON.Box(new CANNON.Vec3(x, y, z)); let options = { mass, shape }; if (friction !== undefined) options.material = new CANNON.Material({ friction }); let body = new CANNON.Body(options); body.position.set(0, 0, 0); this.world.addBody(body); return body; } addCylinder(radiusTop, radiusBottom, height, numSegments, mass) { let shape = new CANNON.Cylinder(radiusTop, radiusBottom, height, numSegments); let body = new CANNON.Body({ mass, shape }); this.world.addBody(body); return body; } removeObject(obj) { this.world.removeBody(obj); } } module.exports = CannonPhysicsEngine;