refactored game-class, entity, rigidbody extends component, refactored world, sphere physics working.

beta.r3js.org
polygonboutique 2016-11-01 12:42:06 +01:00
parent 2098af0c7e
commit 3b6a61ef69
6 changed files with 176 additions and 187 deletions

File diff suppressed because one or more lines are too long

View File

@ -402,7 +402,10 @@ GameLib.D3.Entity.prototype.addComponent = function(
this.parentScene.registerComponent(component); this.parentScene.registerComponent(component);
this.componentIds.push(component.componentId); this.componentIds.push(component.componentId);
component.setParentEntity(this.parentScene, this);
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
component.setParentEntity(this.parentScene, this);
}
}; };
@ -666,107 +669,53 @@ GameLib.D3.Game = function (
) { ) {
this.scenes = {}; this.scenes = {};
this.physicsWorlds = [];
this.sceneToPhysicsWorldsMap = {};
}; };
GameLib.D3.Game.prototype.AddScene = function( GameLib.D3.Game.prototype.addScene = function(
scene scene,
identifer
) { ) {
this.scenes[scene.name] = scene; this.scenes[identifer] = scene;
}; };
GameLib.D3.Game.prototype.AddPhysicsWorld = function( GameLib.D3.Game.prototype.processPhysics = function (
physicsWorld
) {
this.physicsWorlds.push(physicsWorld);
};
GameLib.D3.Game.prototype.LinkPhysicsWorldToScene = function(
physicsWorld,
scene
) {
this.sceneToPhysicsWorldsMap[scene.name] = this.sceneToPhysicsWorldsMap[scene.name] || [];
this.sceneToPhysicsWorldsMap[scene.name].push(physicsWorld);
};
GameLib.D3.Game.prototype.GetPhysicsWorldsForScene = function (
scene
) {
return this.sceneToPhysicsWorldsMap[scene.name];
};
GameLib.D3.Game.prototype.ProcessPhysics = function (
timestep timestep
) { ) {
for(var s in this.sceneToPhysicsWorldsMap) { for(var s in this.scenes) {
var physicsWorldArray = this.sceneToPhysicsWorldsMap[s];
var scene = this.scenes[s]; var scene = this.scenes[s];
if(scene && physicsWorldArray) { for(var w in scene.worlds) {
var world = scene.worlds[w];
for(var i = 0, l = physicsWorldArray.length; i < l; i++) { world.step(timestep);
var physicsWorld = physicsWorldArray[i];
physicsWorld.Step(timestep);
for(var p in physicsWorld.linkedPairs) {
var pair = physicsWorld.linkedPairs[p];
var mesh = pair.threeMesh;
var body = pair.physicsBody;
if(mesh) {
if(physicsWorld.engineType === GameLib.D3.Physics.TYPE_CANNON) {
var quaternion = new THREE.Quaternion();
quaternion.copy(body.rigidBodyInstance.quaternion);
var quaternionCopy = quaternion.clone();
var position = new THREE.Vector3();
position.copy(body.rigidBodyInstance.position);
if(mesh.permutate) {
if(mesh.permutate.offset) {
if(mesh.permutate.offset.quaternion) {
var offsetQuaternion = new THREE.Quaternion();
offsetQuaternion.copy(mesh.permutate.offset.quaternion);
quaternion = quaternion.multiply(offsetQuaternion).normalize();
}
if(mesh.permutate.offset.position) {
var offsetPosition = new THREE.Vector3();
offsetPosition.copy(mesh.permutate.offset.position);
//position = position.add(offsetPosition);
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
}
}
}
mesh.position.copy(position);
mesh.quaternion.copy(quaternion);
}
}
}
}
} }
} }
}; };
GameLib.D3.Game.prototype.LinkPair = function ( GameLib.D3.Game.prototype.render = function(
threeMesh, dt,
physicsBody, renderer,
physicsWorld camera
) { ) {
physicsWorld.linkedPairs = physicsWorld.linkedPairs || []; for(var s in this.scenes) {
var scene = this.scenes[s];
scene.render(dt, renderer, camera);
}
};
physicsWorld.linkedPairs.push({ GameLib.D3.Game.prototype.update = function(
threeMesh : threeMesh, dt
physicsBody : physicsBody ) {
}); for(var s in this.scenes) {
var scene = this.scenes[s];
for(var w in scene.worlds) {
var world = scene.worlds[w];
world.step(dt);
}
scene.update(dt);
scene.lateUpdate(dt);
}
}; };
/** /**
* Graphics Superset * Graphics Superset
@ -2999,10 +2948,11 @@ GameLib.D3.RigidBody = function(
this.shape = typeof shape == "undefined" ? null : shape; this.shape = typeof shape == "undefined" ? null : shape;
this.engine = engine; this.engine = engine;
this.engine.isNotCannonThrow(); this.engine.isNotCannonThrow();
this.instance = this.createInstance(); this.instance = this.createInstance();
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
GameLib.D3.Utils.Extend(GameLib.D3.RigidBody, GameLib.D3.ComponentInterface);
}; };
/** /**
@ -3012,7 +2962,7 @@ GameLib.D3.RigidBody = function(
GameLib.D3.RigidBody.prototype.createInstance = function() { GameLib.D3.RigidBody.prototype.createInstance = function() {
var instance = new this.engine.instance.Body({ var instance = new this.engine.instance.Body({
mass: mass, mass: this.mass,
friction: this.friction, friction: this.friction,
position: new this.engine.instance.Vec3( position: new this.engine.instance.Vec3(
this.position.x, this.position.x,
@ -3043,7 +2993,7 @@ GameLib.D3.RigidBody.prototype.createInstance = function() {
collisionFilterGroup: this.collisionFilterGroup, collisionFilterGroup: this.collisionFilterGroup,
collisionFilterMask: this.collisionFilterMask, collisionFilterMask: this.collisionFilterMask,
fixedRotation: this.fixedRotation, fixedRotation: this.fixedRotation,
shape: this.shape.instance shape: this.shape && this.shape.instance ? this.shape.instance : null
}); });
this.instance = instance; this.instance = instance;
@ -3087,8 +3037,52 @@ GameLib.D3.RigidBody.prototype.addShape = function(
); );
}; };
///////////////////////// Methods to override //////////////////////////
GameLib.D3.RigidBody.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity.mesh) {
var quaternion = new THREE.Quaternion();
quaternion.copy(this.instance.quaternion);
var quaternionCopy = quaternion.clone();
var position = new THREE.Vector3();
position.copy(this.instance.position);
// todo: create mesh superset with permutate field
// permutate : {
// offset : {
// quaternion : new Quaternion(),
// position : new Vec3()
// }
// }
/*if(mesh.permutate) {
if(mesh.permutate.offset) {
if(mesh.permutate.offset.quaternion) {
var offsetQuaternion = new THREE.Quaternion();
offsetQuaternion.copy(mesh.permutate.offset.quaternion);
quaternion = quaternion.multiply(offsetQuaternion).normalize();
}
if(mesh.permutate.offset.position) {
var offsetPosition = new THREE.Vector3();
offsetPosition.copy(mesh.permutate.offset.position);
//position = position.add(offsetPosition);
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
}
}
}*/
parentEntity.mesh.position.copy(position);
parentEntity.mesh.quaternion.copy(quaternion);
}
};
/** /**
* Rigid Wheel superset * Rigid Wheel superset
* @param body GameLib.D3.RigidBody * @param body GameLib.D3.RigidBody
@ -5341,7 +5335,7 @@ GameLib.D3.World = function(
if (typeof solver == 'undefined') { if (typeof solver == 'undefined') {
solver = new GameLib.D3.Solver( solver = new GameLib.D3.Solver(
null, null,
'GSSolver', engine,
GameLib.D3.Solver.GS_SOLVER GameLib.D3.Solver.GS_SOLVER
); );
} }

View File

@ -87,7 +87,10 @@ GameLib.D3.Entity.prototype.addComponent = function(
this.parentScene.registerComponent(component); this.parentScene.registerComponent(component);
this.componentIds.push(component.componentId); this.componentIds.push(component.componentId);
component.setParentEntity(this.parentScene, this);
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
component.setParentEntity(this.parentScene, this);
}
}; };

View File

@ -2,105 +2,51 @@ GameLib.D3.Game = function (
) { ) {
this.scenes = {}; this.scenes = {};
this.physicsWorlds = [];
this.sceneToPhysicsWorldsMap = {};
}; };
GameLib.D3.Game.prototype.AddScene = function( GameLib.D3.Game.prototype.addScene = function(
scene scene,
identifer
) { ) {
this.scenes[scene.name] = scene; this.scenes[identifer] = scene;
}; };
GameLib.D3.Game.prototype.AddPhysicsWorld = function( GameLib.D3.Game.prototype.processPhysics = function (
physicsWorld
) {
this.physicsWorlds.push(physicsWorld);
};
GameLib.D3.Game.prototype.LinkPhysicsWorldToScene = function(
physicsWorld,
scene
) {
this.sceneToPhysicsWorldsMap[scene.name] = this.sceneToPhysicsWorldsMap[scene.name] || [];
this.sceneToPhysicsWorldsMap[scene.name].push(physicsWorld);
};
GameLib.D3.Game.prototype.GetPhysicsWorldsForScene = function (
scene
) {
return this.sceneToPhysicsWorldsMap[scene.name];
};
GameLib.D3.Game.prototype.ProcessPhysics = function (
timestep timestep
) { ) {
for(var s in this.sceneToPhysicsWorldsMap) { for(var s in this.scenes) {
var physicsWorldArray = this.sceneToPhysicsWorldsMap[s];
var scene = this.scenes[s]; var scene = this.scenes[s];
if(scene && physicsWorldArray) { for(var w in scene.worlds) {
var world = scene.worlds[w];
for(var i = 0, l = physicsWorldArray.length; i < l; i++) { world.step(timestep);
var physicsWorld = physicsWorldArray[i];
physicsWorld.Step(timestep);
for(var p in physicsWorld.linkedPairs) {
var pair = physicsWorld.linkedPairs[p];
var mesh = pair.threeMesh;
var body = pair.physicsBody;
if(mesh) {
if(physicsWorld.engineType === GameLib.D3.Physics.TYPE_CANNON) {
var quaternion = new THREE.Quaternion();
quaternion.copy(body.rigidBodyInstance.quaternion);
var quaternionCopy = quaternion.clone();
var position = new THREE.Vector3();
position.copy(body.rigidBodyInstance.position);
if(mesh.permutate) {
if(mesh.permutate.offset) {
if(mesh.permutate.offset.quaternion) {
var offsetQuaternion = new THREE.Quaternion();
offsetQuaternion.copy(mesh.permutate.offset.quaternion);
quaternion = quaternion.multiply(offsetQuaternion).normalize();
}
if(mesh.permutate.offset.position) {
var offsetPosition = new THREE.Vector3();
offsetPosition.copy(mesh.permutate.offset.position);
//position = position.add(offsetPosition);
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
}
}
}
mesh.position.copy(position);
mesh.quaternion.copy(quaternion);
}
}
}
}
} }
} }
}; };
GameLib.D3.Game.prototype.LinkPair = function ( GameLib.D3.Game.prototype.render = function(
threeMesh, dt,
physicsBody, renderer,
physicsWorld camera
) { ) {
physicsWorld.linkedPairs = physicsWorld.linkedPairs || []; for(var s in this.scenes) {
var scene = this.scenes[s];
physicsWorld.linkedPairs.push({ scene.render(dt, renderer, camera);
threeMesh : threeMesh, }
physicsBody : physicsBody };
});
GameLib.D3.Game.prototype.update = function(
dt
) {
for(var s in this.scenes) {
var scene = this.scenes[s];
for(var w in scene.worlds) {
var world = scene.worlds[w];
world.step(dt);
}
scene.update(dt);
scene.lateUpdate(dt);
}
}; };

View File

@ -55,10 +55,11 @@ GameLib.D3.RigidBody = function(
this.shape = typeof shape == "undefined" ? null : shape; this.shape = typeof shape == "undefined" ? null : shape;
this.engine = engine; this.engine = engine;
this.engine.isNotCannonThrow(); this.engine.isNotCannonThrow();
this.instance = this.createInstance(); this.instance = this.createInstance();
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
GameLib.D3.Utils.Extend(GameLib.D3.RigidBody, GameLib.D3.ComponentInterface);
}; };
/** /**
@ -68,7 +69,7 @@ GameLib.D3.RigidBody = function(
GameLib.D3.RigidBody.prototype.createInstance = function() { GameLib.D3.RigidBody.prototype.createInstance = function() {
var instance = new this.engine.instance.Body({ var instance = new this.engine.instance.Body({
mass: mass, mass: this.mass,
friction: this.friction, friction: this.friction,
position: new this.engine.instance.Vec3( position: new this.engine.instance.Vec3(
this.position.x, this.position.x,
@ -99,7 +100,7 @@ GameLib.D3.RigidBody.prototype.createInstance = function() {
collisionFilterGroup: this.collisionFilterGroup, collisionFilterGroup: this.collisionFilterGroup,
collisionFilterMask: this.collisionFilterMask, collisionFilterMask: this.collisionFilterMask,
fixedRotation: this.fixedRotation, fixedRotation: this.fixedRotation,
shape: this.shape.instance shape: this.shape && this.shape.instance ? this.shape.instance : null
}); });
this.instance = instance; this.instance = instance;
@ -143,4 +144,49 @@ GameLib.D3.RigidBody.prototype.addShape = function(
); );
}; };
///////////////////////// Methods to override //////////////////////////
GameLib.D3.RigidBody.prototype.onLateUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity.mesh) {
var quaternion = new THREE.Quaternion();
quaternion.copy(this.instance.quaternion);
var quaternionCopy = quaternion.clone();
var position = new THREE.Vector3();
position.copy(this.instance.position);
// todo: create mesh superset with permutate field
// permutate : {
// offset : {
// quaternion : new Quaternion(),
// position : new Vec3()
// }
// }
/*if(mesh.permutate) {
if(mesh.permutate.offset) {
if(mesh.permutate.offset.quaternion) {
var offsetQuaternion = new THREE.Quaternion();
offsetQuaternion.copy(mesh.permutate.offset.quaternion);
quaternion = quaternion.multiply(offsetQuaternion).normalize();
}
if(mesh.permutate.offset.position) {
var offsetPosition = new THREE.Vector3();
offsetPosition.copy(mesh.permutate.offset.position);
//position = position.add(offsetPosition);
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
}
}
}*/
parentEntity.mesh.position.copy(position);
parentEntity.mesh.quaternion.copy(quaternion);
}
};

View File

@ -41,7 +41,7 @@ GameLib.D3.World = function(
if (typeof solver == 'undefined') { if (typeof solver == 'undefined') {
solver = new GameLib.D3.Solver( solver = new GameLib.D3.Solver(
null, null,
'GSSolver', engine,
GameLib.D3.Solver.GS_SOLVER GameLib.D3.Solver.GS_SOLVER
); );
} }