From be9718aaeecd8278940b036733f14a6dee41e3f0 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 7 Oct 2016 16:06:50 +0200 Subject: [PATCH] starting to work with physics --- game-lib-controls.js | 21 +---- game-lib.js | 217 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 188 insertions(+), 50 deletions(-) diff --git a/game-lib-controls.js b/game-lib-controls.js index c6a477c..2245a7a 100644 --- a/game-lib-controls.js +++ b/game-lib-controls.js @@ -73,16 +73,7 @@ Controls.FlyControls.prototype.onMouseUp = function(event) { } }; - -Controls.FlyControls.prototype.getForward = function() { - var direction = new this.THREE.Vector3(0, 0, 1); - var rotation = new this.THREE.Euler(0, 0, 0, "YXZ"); - rotation.set(this.pitch, this.yaw, 0, "YXZ"); - var forward = direction.applyEuler(rotation).normalize(); - return forward; -}; - -Controls.FlyControls.prototype.applyRotation = function(deltaTime) { +Controls.FlyControls.prototype.applyRotation = function() { this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ"); }; @@ -140,11 +131,11 @@ Controls.FlyControls.prototype.applyTranslation = function(deltaTime) { }; Controls.FlyControls.prototype.update = function(deltaTime) { - + this.applyRotation(); + this.applyTranslation(deltaTime); }; Controls.FlyControls.prototype.onMouseMove = function ( event ) { - if (this.canRotate) { var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0; var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0; @@ -152,12 +143,6 @@ Controls.FlyControls.prototype.onMouseMove = function ( event ) { this.yaw -= movementX * 0.002; this.pitch -= movementY * 0.002; } - - // Save mouseCoords - - // sys.mouseCoords.set( ( event.clientX / window.innerWidth ) * 2 - 1, - // -( event.clientY / window.innerHeight ) * 2 + 1 ); - }; Controls.FlyControls.prototype.onKeyDown = function ( event ) { diff --git a/game-lib.js b/game-lib.js index 85aa924..3bac60a 100644 --- a/game-lib.js +++ b/game-lib.js @@ -1121,73 +1121,166 @@ GameLib.D3.Bone = function( this.rawData = null;//rawData; }; -GameLib.D3.Physics = function() {}; - /** - * Physics Engine Superset + * Physics Superset * @param id * @param name * @param engineType - * @param engine + * @param CANNON + * @param Ammo + * @param Goblin + * @param worlds GameLib.D3.Physics.World[] * @constructor */ -GameLib.D3.Physics.Engine = function( +GameLib.D3.Physics = function( id, name, engineType, - engine + CANNON, + Ammo, + Goblin ) { + this.id = id; this.name = name; this.engineType = engineType; - this.engine = engine; + + this.CANNON = CANNON; + + this.Ammo = Ammo; + + this.Goblin = Goblin; + + this.worlds = []; + + this.customWorlds = []; }; /** * Physics Engine Types * @type {number} */ -GameLib.D3.Physics.Engine.TYPE_CANNON = 0x1; -GameLib.D3.Physics.Engine.TYPE_AMMO = 0x2; -GameLib.D3.Physics.Engine.TYPE_GOBLIN = 0x3; +GameLib.D3.Physics.TYPE_CANNON = 0x1; +GameLib.D3.Physics.TYPE_AMMO = 0x2; +GameLib.D3.Physics.TYPE_GOBLIN = 0x3; + +/** + * Broadphase Types + * @type {number} + */ +GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE = 0x1; +GameLib.D3.Physics.BROADPHASE_TYPE_GRID = 0x2; +GameLib.D3.Physics.BROADPHASE_TYPE_SAP = 0x3; + +/** + * Solver Types + * @type {number} + */ +GameLib.D3.Physics.SPLIT_SOLVER = 0x1; +GameLib.D3.Physics.GS_SOLVER = 0x2; + +/** + * Physics Solver Superset + * @param id + * @param name + * @param solverType + * @param iterations + * @param tolerance + * @constructor + */ +GameLib.D3.Physics.Solver = function( + id, + name, + solverType, + iterations, + tolerance +) { + this.id = id; + this.name = name; + this.solverType = solverType; + this.iterations = iterations; + this.tolerance = tolerance; +}; + +/** + * Physics Broadphase Superset + * @param id + * @param name + * @param broadphaseType + * @constructor + */ +GameLib.D3.Physics.Broadphase = function( + id, + name, + broadphaseType +) { + this.id = id; + this.name = name; + this.broadphaseType = broadphaseType; +}; /** * Physics World Superset * @param id * @param name + * @param physics * @param gravity - * @param rigidBodies GameLib.D3.Physics.RigidBody[] - * @param engine GameLib.D3.Physics.Engine + * @param broadphase + * @param solver + * @param rigidBodies * @constructor */ GameLib.D3.Physics.World = function( id, name, - engineType, - gravity + physics, + gravity, + broadphase, + solver, + rigidBodies ) { this.id = id; this.name = name; - this.engineType = engineType; + this.physics = physics; + + //this.worldType = GameLib.D3.Physics.World.TYPE_CANNON_WORLD; if (typeof gravity == 'undefined') { - gravity = new THREE.Vector3(0, -9.81, 0); + gravity = new GameLib.D3.Vector3(0, -9.81, 0); } this.gravity = gravity; - this.worldObject = null; - - if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) { - this.worldObject = new CANNON.World(); - this.worldObject.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z); - this.worldObject.broadphase = new CANNON.NaiveBroadphase(); - //this.worldObject.broadphase = new CANNON.SAPBroadphase(); - this.worldObject.solver.iterations = 10; + if (typeof broadphase == 'undefined') { + broadphase = new GameLib.D3.Physics.Broadphase( + null, + 'broadPhaseNaive', + GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE + ); } + this.broadphase = broadphase; + + if (typeof solver == 'undefined') { + solver = new GameLib.D3.Physics.Solver( + null, + 'GCSolver', + GameLib.D3.Physics.GS_SOLVER + ); + } + this.solver = solver; + + if (typeof rigidBodies == 'undefined') { + rigidBodies = []; + } + this.rigidBodies = rigidBodies; + + physics.worlds.push(this); + physics.customWorlds.push(this.getCustomWorld(this)); }; +//GameLib.D3.Physics.World.TYPE_CANNON_WORLD = 0x1; + /** * Physics Rigid Body Superset * @param id @@ -1471,7 +1564,8 @@ GameLib.D3.Scene = function( rotation, scale, parentSceneId, - lights + lights, + physics ) { this.id = id; this.path = path; @@ -1510,6 +1604,11 @@ GameLib.D3.Scene = function( lights = []; } this.lights = lights; + + if (typeof physics == 'undefined') { + physics = []; + } + this.physics = physics; }; /** @@ -1936,7 +2035,7 @@ GameLib.D3.prototype.loadMap = function(gameLibTexture, threeMaterial, threeMate if (imagePath) { - this.textureLoader.crossOrigin = ''; + this.textureLoader.crossOrigin = ''; this.textureLoader.load( imagePath, @@ -2661,9 +2760,65 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals }); }; -// --------------- -// Physics -// --------------- +/** --------------- + * Physics + * --------------- */ +/** + * Creates a Custom World object from a GameLib.D3.Physics.World + */ +GameLib.D3.Physics.World.prototype.getCustomWorld = function() { + + var engineType = this.physics.engineType; + + if (engineType != GameLib.D3.Physics.TYPE_CANNON) { + console.warn('Unsupported engine type: ' + engineType); + throw new Error('Unsupported engine type: ' + engineType); + } + + var customWorld = new this.physics.CANNON.World(); + + var cannonBroadphase = null; + + if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE) { + cannonBroadphase = new this.physics.CANNON.NaiveBroadphase(); + } else if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_GRID) { + cannonBroadphase = new this.physics.CANNON.GridBroadphase(); + } else if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_SAP) { + cannonBroadphase = new this.physics.CANNON.SAPBroardphase(); + } else { + console.warn('Unsupported broadphase type: ' + this.broadphase.broadphaseType); + throw new Error('Unsupported broadphase type: ' + this.broadphase.broadphaseType); + } + + customWorld.broadphase = cannonBroadphase; + + var cannonSolver = null; + + if (this.solver.solverType == GameLib.D3.Physics.SPLIT_SOLVER) { + cannonSolver = new this.physics.CANNON.SplitSolver(); + } else if (this.solver.solverType == GameLib.D3.Physics.GS_SOLVER) { + cannonSolver = new this.physics.CANNON.GSSolver(); + cannonSolver.iterations = this.solver.iterations; + } + + customWorld.solver = cannonSolver; + + customWorld.gravity.x = this.gravity.x; + customWorld.gravity.y = this.gravity.y; + customWorld.gravity.z = this.gravity.z; + + for (var b = 0; b < this.rigidBodies.length; b++) { + + var customBody = this.createCustomBody(this.rigidBodies[b]); + + //customWorld.AddRigidBody(); + } + + customWorld.name = this.name; + + return customWorld; +}; + GameLib.D3.Physics.Shape.prototype.Update = function( physicsWorld @@ -2725,9 +2880,7 @@ GameLib.D3.Physics.World.prototype.AddWheel = function( } }; -GameLib.D3.Physics.Vehicle.prototype.GetWheelInfo = function( - -) { +GameLib.D3.Physics.Vehicle.prototype.GetWheelInfo = function() { return this.vehicleObject.wheelBodies; };