starting to work with physics

beta.r3js.org
Theunis J. Botha 2016-10-07 16:06:50 +02:00
parent 74d04acc02
commit be9718aaee
2 changed files with 188 additions and 50 deletions

View File

@ -73,16 +73,7 @@ Controls.FlyControls.prototype.onMouseUp = function(event) {
} }
}; };
Controls.FlyControls.prototype.applyRotation = function() {
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) {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ"); 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) { Controls.FlyControls.prototype.update = function(deltaTime) {
this.applyRotation();
this.applyTranslation(deltaTime);
}; };
Controls.FlyControls.prototype.onMouseMove = function ( event ) { Controls.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) { if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0; var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 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.yaw -= movementX * 0.002;
this.pitch -= movementY * 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 ) { Controls.FlyControls.prototype.onKeyDown = function ( event ) {

View File

@ -1121,73 +1121,166 @@ GameLib.D3.Bone = function(
this.rawData = null;//rawData; this.rawData = null;//rawData;
}; };
GameLib.D3.Physics = function() {};
/** /**
* Physics Engine Superset * Physics Superset
* @param id * @param id
* @param name * @param name
* @param engineType * @param engineType
* @param engine * @param CANNON
* @param Ammo
* @param Goblin
* @param worlds GameLib.D3.Physics.World[]
* @constructor * @constructor
*/ */
GameLib.D3.Physics.Engine = function( GameLib.D3.Physics = function(
id, id,
name, name,
engineType, engineType,
engine CANNON,
Ammo,
Goblin
) { ) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.engineType = engineType; this.engineType = engineType;
this.engine = engine;
this.CANNON = CANNON;
this.Ammo = Ammo;
this.Goblin = Goblin;
this.worlds = [];
this.customWorlds = [];
}; };
/** /**
* Physics Engine Types * Physics Engine Types
* @type {number} * @type {number}
*/ */
GameLib.D3.Physics.Engine.TYPE_CANNON = 0x1; GameLib.D3.Physics.TYPE_CANNON = 0x1;
GameLib.D3.Physics.Engine.TYPE_AMMO = 0x2; GameLib.D3.Physics.TYPE_AMMO = 0x2;
GameLib.D3.Physics.Engine.TYPE_GOBLIN = 0x3; 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 * Physics World Superset
* @param id * @param id
* @param name * @param name
* @param physics
* @param gravity * @param gravity
* @param rigidBodies GameLib.D3.Physics.RigidBody[] * @param broadphase
* @param engine GameLib.D3.Physics.Engine * @param solver
* @param rigidBodies
* @constructor * @constructor
*/ */
GameLib.D3.Physics.World = function( GameLib.D3.Physics.World = function(
id, id,
name, name,
engineType, physics,
gravity gravity,
broadphase,
solver,
rigidBodies
) { ) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.engineType = engineType; this.physics = physics;
//this.worldType = GameLib.D3.Physics.World.TYPE_CANNON_WORLD;
if (typeof gravity == 'undefined') { 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.gravity = gravity;
this.worldObject = null; if (typeof broadphase == 'undefined') {
broadphase = new GameLib.D3.Physics.Broadphase(
if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) { null,
this.worldObject = new CANNON.World(); 'broadPhaseNaive',
this.worldObject.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z); GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE
this.worldObject.broadphase = new CANNON.NaiveBroadphase(); );
//this.worldObject.broadphase = new CANNON.SAPBroadphase();
this.worldObject.solver.iterations = 10;
} }
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 * Physics Rigid Body Superset
* @param id * @param id
@ -1471,7 +1564,8 @@ GameLib.D3.Scene = function(
rotation, rotation,
scale, scale,
parentSceneId, parentSceneId,
lights lights,
physics
) { ) {
this.id = id; this.id = id;
this.path = path; this.path = path;
@ -1510,6 +1604,11 @@ GameLib.D3.Scene = function(
lights = []; lights = [];
} }
this.lights = 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) { if (imagePath) {
this.textureLoader.crossOrigin = ''; this.textureLoader.crossOrigin = '';
this.textureLoader.load( this.textureLoader.load(
imagePath, 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( GameLib.D3.Physics.Shape.prototype.Update = function(
physicsWorld 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; return this.vehicleObject.wheelBodies;
}; };