function removeObjects(tombstoned) { var tombstonedLength = tombstoned.length; if (tombstonedLength) { for (var i = 0; i < tombstonedLength; i++) { world.remove(tombstoned[i].body); } } }
constructor() { this.g = config.world.g.base + Math.random() * config.world.g.variation / 2.0 - config.world.g.variation / 2.0; this._holded = false; this._started = false; this._timeStep = config.world.defaultTimeStep; this._world = new CANNON.World(); this._world.gravity.set(0, 0, -this.g); // m/s² this._world.quatNormalizeSkip = 0; this._world.quatNormalizeFast = false; this._solver = new CANNON.GSSolver(); this._world.defaultContactMaterial.contactEquationStiffness = 1e9; this._world.defaultContactMaterial.contactEquationRelaxation = 4; this._solver.iterations = 7; this._solver.tolerance = 0.1; this._world.solver = new CANNON.SplitSolver(this._solver); this._world.broadphase = new CANNON.NaiveBroadphase(); this.groundMaterial = new CANNON.Material('groundMaterial'); this.groundGroundCm = new CANNON.ContactMaterial(this.groundMaterial, this.groundMaterial, { friction: config.world.friction, restitution: config.world.restitution, frictionEquationRegularizationTime: 3, }); this._world.addContactMaterial(this.groundGroundCm); this._groundBody = new CANNON.Body({ mass: 0, position: new CANNON.Vec3(0, 0, 0), material: this.groundMaterial, }); this._groundShape = new CANNON.Plane(); this._groundBody.addShape(this._groundShape); this._world.addBody(this._groundBody); this._listeners = []; this._objects = []; }
module.exports = function(gameState) { bootstrappingObjects(gameState.bootstrapping); //checkStopWobblingObjects(gameState.objects); if (gameState.player.body.wakeUp) { gameState.player.body.wakeUp(); } movePlayer(gameState.player, gameState.timing.delta); world.step(1.0/60.0, gameState.timing.delta / 1000); removeObjects(gameState.tombstoned); };
addCube(position) { let size = config.map.cubeSize / 2.0; var box = new CANNON.Body({ mass: 0, position: position, shape: new CANNON.Box(new CANNON.Vec3(size, size, config.map.cubeHeight)), }); this._world.addBody(box); this._objects.push(box); return box; }
_render() { var now = Date.now(), dt = (now - this._lastTime) / 1e3; this.sphere.rotation.setFromQuaternion(this._body.quaternion); this._world.step(TIME_STEP, dt, MAX_SUB_STEPS); this.renderer.render(this.scene, this.camera); if (!this.paused) requestAnimationFrame(this.render); this._lastTime = now; }
function bootstrappingObjects(bootstrapping) { var bootstrapLength = bootstrapping.length; if (bootstrapLength) { var body = null, obj = null; for (var i = 0; i < bootstrapLength; i++) { obj = bootstrapping[i]; switch (obj.type) { case 'cube': body = createCube(obj.initialPosition.x, obj.initialPosition.y, obj.initialPosition.z, obj.initialPosition.zRotation); break; default: console.warn('Type:', obj.type, 'unknown'); continue; } world.add(body); obj.body = body; } } }
addBody(body) { this._world.addBody(body); this._objects.push(body); }
_step(dt) { this._world.step(dt); }
var CANNON = require('cannon'); var baseImpulse = 0.1; const ITERATIONS_BETWEEN_SLEEP_CHECK = 20; const SLEEP_VELOCITY = 0.5; var currentIteration = 0; var world = new CANNON.World(); world.gravity.set(0,0,-9); world.broadphase = new CANNON.NaiveBroadphase(); // world.solver.tolerance = 0.01; world.solver.iterations = 200; world.solver.tolerance = 0.0; world.defaultContactMaterial.contactEquationStiffness = 1e8; world.defaultContactMaterial.contactEquationRegularizationTime = 10; // Static ground plane // Static bodies only interacts with dynamic bodies. Velocity is always zero. var groundShape = new CANNON.Plane(); var groundMaterial = new CANNON.Material('groundMaterial'); var groundBody = new CANNON.Body({ mass: 0, material: groundMaterial // mass=0 will produce a static body automatically }); var slipperyMaterial = new CANNON.Material('slipperyMaterial'); var groundGroundContactMaterial = new CANNON.ContactMaterial(groundMaterial, groundMaterial, { friction: 1, restitution: 0,
constructor(canvas) { this._lastTime = Date.now(); // Camera & Scene this.scene = new THREE.Scene(); this.scene.fog = new THREE.Fog(0x000000, 4.5, 6.5); this.camera = new THREE.PerspectiveCamera(33, window.innerWidth / window.innerHeight, 0.1, 100); this.camera.position.set(0, 0, 4.5); this.camera.lookAt(new THREE.Vector3(0, 0, 0)); // Lighting this.scene.add(new THREE.AmbientLight(0x4f5359)); this.scene.add(new THREE.HemisphereLight(0xC6C2B6, 0x3A403B, .85)); // Renderer this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, canvas: canvas }); var renderSize = throttle(() => { this.renderer.setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio); this.camera.aspect = (window.innerWidth / window.innerHeight); this.camera.updateProjectionMatrix(); }, 200, true); renderSize(); // Physics this._world = new CANNON.World(); // The volume and mass of a human head this._body = new CANNON.Body({ mass: 5, position: new CANNON.Vec3(0, 0, 0), shape: new CANNON.Sphere(0.0875) }); this._body.angularDamping = .88; this._world.addBody(this._body); this._world.addEventListener('preStep', ()=> { var t = this._lastTime; this._body.angularVelocity.y = Math.sin(τ * ((t + OSC_DUR / 2.5) % (OSC_DUR*2)) / (OSC_DUR*2)) * 2 * DEFAULT_ω; this._body.angularVelocity.x = Math.sin(τ * (t % OSC_DUR) / OSC_DUR) * DEFAULT_ω; this._body.angularVelocity.z = Math.sin(τ * ((t + OSC_DUR / 2) % OSC_DUR) / OSC_DUR) * DEFAULT_ω; }); // DOM bindings and render loop window.addEventListener('resize', renderSize); this.paused = true; this.render = this._render.bind(this); }