face fixes, parentWorld fixes, linking system updates, physics ok
parent
ce4b1cd260
commit
01b36238f9
|
@ -80,6 +80,7 @@ GameLib.Event.FETCH_COMPONENT_TYPES = 0x3e;
|
||||||
GameLib.Event.FETCH_COMPONENTS = 0x3f;
|
GameLib.Event.FETCH_COMPONENTS = 0x3f;
|
||||||
GameLib.Event.GET_API_URL = 0x40;
|
GameLib.Event.GET_API_URL = 0x40;
|
||||||
GameLib.Event.GET_PHYSICS_ENGINE = 0x41;
|
GameLib.Event.GET_PHYSICS_ENGINE = 0x41;
|
||||||
|
GameLib.Event.PARENT_WORLD_CHANGE = 0x42;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns string name of event ID
|
* Returns string name of event ID
|
||||||
|
@ -155,6 +156,7 @@ GameLib.Event.GetEventName = function(number) {
|
||||||
case 0x3f : return 'fetch_components';
|
case 0x3f : return 'fetch_components';
|
||||||
case 0x40 : return 'get_api_url';
|
case 0x40 : return 'get_api_url';
|
||||||
case 0x41 : return 'get_physics_engine';
|
case 0x41 : return 'get_physics_engine';
|
||||||
|
case 0x42 : return 'parent_world_change';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@ GameLib.Component.prototype.getDependencies = function() {
|
||||||
this.linkedObjects.hasOwnProperty(property) &&
|
this.linkedObjects.hasOwnProperty(property) &&
|
||||||
property !== 'parentMesh' &&
|
property !== 'parentMesh' &&
|
||||||
property !== 'parentScene' &&
|
property !== 'parentScene' &&
|
||||||
|
property !== 'parentWorld' &&
|
||||||
property !== 'parentEntity' &&
|
property !== 'parentEntity' &&
|
||||||
property !== 'parentEntityManager' &&
|
property !== 'parentEntityManager' &&
|
||||||
this.hasOwnProperty(property)
|
this.hasOwnProperty(property)
|
||||||
|
@ -248,6 +249,7 @@ GameLib.Component.prototype.buildIdToObject = function() {
|
||||||
property !== 'parentEntity' &&
|
property !== 'parentEntity' &&
|
||||||
property !== 'parentScene' &&
|
property !== 'parentScene' &&
|
||||||
property !== 'parentMesh' &&
|
property !== 'parentMesh' &&
|
||||||
|
property !== 'parentWorld' &&
|
||||||
property !== 'parentEntityManager'
|
property !== 'parentEntityManager'
|
||||||
) {
|
) {
|
||||||
if (this.linkedObjects[property] instanceof Array) {
|
if (this.linkedObjects[property] instanceof Array) {
|
||||||
|
|
|
@ -58,7 +58,7 @@ GameLib.D3.API.Face = function(
|
||||||
this.materialIndex = materialIndex;
|
this.materialIndex = materialIndex;
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(uvs)) {
|
if (GameLib.Utils.UndefinedOrNull(uvs)) {
|
||||||
uvs = [[new GameLib.API.Vector2(0,0), new GameLib.API.Vector2(0,0), new GameLib.API.Vector2(0,0)]];
|
uvs = [[]];
|
||||||
}
|
}
|
||||||
this.uvs = uvs;
|
this.uvs = uvs;
|
||||||
|
|
||||||
|
@ -96,6 +96,46 @@ GameLib.D3.API.Face = function(
|
||||||
* @param objectFace
|
* @param objectFace
|
||||||
*/
|
*/
|
||||||
GameLib.D3.API.Face.FromObject = function(objectFace) {
|
GameLib.D3.API.Face.FromObject = function(objectFace) {
|
||||||
|
|
||||||
|
var apiUvs = objectFace.uvs.reduce(
|
||||||
|
|
||||||
|
function(result, uvArray, index) {
|
||||||
|
|
||||||
|
result[index] = uvArray.reduce(
|
||||||
|
function(uvResult, uv) {
|
||||||
|
uvResult.push(GameLib.API.Vector2.FromObject(uv));
|
||||||
|
return uvResult;
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
var apiVertexColors = objectFace.vertexColors.map(
|
||||||
|
function(vertexColor) {
|
||||||
|
return GameLib.API.Color.FromObject(vertexColor);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var apiColor = null;
|
||||||
|
if (objectFace.color) {
|
||||||
|
apiColor = GameLib.API.Color.FromObject(objectFace.color);
|
||||||
|
}
|
||||||
|
|
||||||
|
var apiVertexNormals = objectFace.vertexNormals.map(
|
||||||
|
function(vertexNormal) {
|
||||||
|
return GameLib.API.Vector3.FromObject(vertexNormal);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var apiNormal = null;
|
||||||
|
if (objectFace.normal) {
|
||||||
|
apiNormal = GameLib.API.Vector3.FromObject(objectFace.normal);
|
||||||
|
}
|
||||||
|
|
||||||
return new GameLib.D3.API.Face(
|
return new GameLib.D3.API.Face(
|
||||||
objectFace.id,
|
objectFace.id,
|
||||||
objectFace.name,
|
objectFace.name,
|
||||||
|
@ -103,11 +143,11 @@ GameLib.D3.API.Face.FromObject = function(objectFace) {
|
||||||
objectFace.v1index,
|
objectFace.v1index,
|
||||||
objectFace.v2index,
|
objectFace.v2index,
|
||||||
objectFace.materialIndex,
|
objectFace.materialIndex,
|
||||||
objectFace.uvs,
|
apiUvs,
|
||||||
objectFace.color,
|
apiColor,
|
||||||
objectFace.vertexColors,
|
apiVertexColors,
|
||||||
objectFace.vertexNormals,
|
apiVertexNormals,
|
||||||
objectFace.normal
|
apiNormal
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
* @param shapes
|
* @param shapes
|
||||||
* @param kinematic
|
* @param kinematic
|
||||||
* @param parentMesh
|
* @param parentMesh
|
||||||
|
* @param parentWorld
|
||||||
* @param parentEntity
|
* @param parentEntity
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
|
@ -42,6 +43,7 @@ GameLib.D3.API.RigidBody = function(
|
||||||
shapes,
|
shapes,
|
||||||
kinematic,
|
kinematic,
|
||||||
parentMesh,
|
parentMesh,
|
||||||
|
parentWorld,
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
@ -140,6 +142,11 @@ GameLib.D3.API.RigidBody = function(
|
||||||
}
|
}
|
||||||
this.parentMesh = parentMesh;
|
this.parentMesh = parentMesh;
|
||||||
|
|
||||||
|
if (GameLib.Utils.UndefinedOrNull(parentWorld)) {
|
||||||
|
parentWorld = null;
|
||||||
|
}
|
||||||
|
this.parentWorld = parentWorld;
|
||||||
|
|
||||||
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
|
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
|
||||||
parentEntity = null;
|
parentEntity = null;
|
||||||
}
|
}
|
||||||
|
@ -175,6 +182,7 @@ GameLib.D3.API.RigidBody.FromObject = function(objectRigidBody) {
|
||||||
objectRigidBody.shapes,
|
objectRigidBody.shapes,
|
||||||
objectRigidBody.kinematic,
|
objectRigidBody.kinematic,
|
||||||
objectRigidBody.parentMesh,
|
objectRigidBody.parentMesh,
|
||||||
|
objectRigidBody.parentWorld,
|
||||||
objectRigidBody.parentEntity
|
objectRigidBody.parentEntity
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -69,21 +69,52 @@ GameLib.D3.Face = function Face(
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// this.vertexNormals = this.vertexNormals.map(function(vertexNormal){
|
this.vertexNormals = this.vertexNormals.map(function(vertexNormal){
|
||||||
// if (vertexNormal instanceof GameLib.Vector3) {
|
if (vertexNormal instanceof GameLib.Vector3) {
|
||||||
// return vertexNormal;
|
return vertexNormal;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// if (vertexNormal instanceof GameLib.API.Vector3) {
|
if (vertexNormal instanceof GameLib.API.Vector3) {
|
||||||
// return new GameLib.Vector3(
|
return new GameLib.Vector3(
|
||||||
// this.graphics,
|
this.implementation,
|
||||||
// vertexNormal,
|
vertexNormal,
|
||||||
// this
|
this
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// console.warn('unknown vertex normal type', vertexNormal);
|
console.warn('unknown vertex normal type', vertexNormal);
|
||||||
// }.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
this.uvs = this.uvs.reduce(
|
||||||
|
|
||||||
|
function(result, uvArray, index) {
|
||||||
|
|
||||||
|
result[index] = uvArray.reduce(
|
||||||
|
function(uvResult, uv) {
|
||||||
|
|
||||||
|
if (uv instanceof GameLib.API.Vector2) {
|
||||||
|
uvResult.push(
|
||||||
|
new GameLib.Vector2(
|
||||||
|
this.implementation,
|
||||||
|
uv,
|
||||||
|
this
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.warn('unknown uv type');
|
||||||
|
}
|
||||||
|
|
||||||
|
return uvResult;
|
||||||
|
}.bind(this),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
|
||||||
|
}.bind(this),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
this.normal = new GameLib.Vector3(
|
this.normal = new GameLib.Vector3(
|
||||||
this.implementation,
|
this.implementation,
|
||||||
|
@ -105,14 +136,36 @@ GameLib.D3.Face.prototype.updateInstance = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.Face.prototype.toApiObject = function() {
|
GameLib.D3.Face.prototype.toApiObject = function() {
|
||||||
return new GameLib.D3.API.Face(
|
|
||||||
|
return new GameLib.D3.API.Face(
|
||||||
this.id,
|
this.id,
|
||||||
this.name,
|
this.name,
|
||||||
this.v0index,
|
this.v0index,
|
||||||
this.v1index,
|
this.v1index,
|
||||||
this.v2index,
|
this.v2index,
|
||||||
this.materialIndex,
|
this.materialIndex,
|
||||||
this.uvs,
|
this.uvs.reduce(
|
||||||
|
function(result, uvArray, index) {
|
||||||
|
|
||||||
|
result[index] = uvArray.reduce(
|
||||||
|
function(uvResult, uv) {
|
||||||
|
|
||||||
|
if (uv instanceof GameLib.Vector2) {
|
||||||
|
uvResult.push(uv.toApiObject());
|
||||||
|
} else {
|
||||||
|
console.warn('unknown uv type - cannot commit to API');
|
||||||
|
}
|
||||||
|
|
||||||
|
return uvResult;
|
||||||
|
}.bind(this),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}.bind(this),
|
||||||
|
[]
|
||||||
|
),
|
||||||
this.color.toApiObject(),
|
this.color.toApiObject(),
|
||||||
this.vertexColors.map(function(vertexColor){
|
this.vertexColors.map(function(vertexColor){
|
||||||
return vertexColor.toApiObject();
|
return vertexColor.toApiObject();
|
||||||
|
|
|
@ -366,14 +366,18 @@ GameLib.D3.Mesh.prototype.createInstanceGeometry = function(instanceGeometry) {
|
||||||
geometry.faces = this.faces.map(
|
geometry.faces = this.faces.map(
|
||||||
function (face) {
|
function (face) {
|
||||||
|
|
||||||
standardUvs.push(face.uvs[0].map(
|
if (face.uvs[0].length > 0) {
|
||||||
function(uv) {
|
standardUvs.push(
|
||||||
return new THREE.Vector2(
|
face.uvs[0].map(
|
||||||
uv.x,
|
function(uv) {
|
||||||
uv.y
|
return new THREE.Vector2(
|
||||||
)
|
uv.x,
|
||||||
}
|
uv.y
|
||||||
));
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
var faceInstance = new THREE.Face3(
|
var faceInstance = new THREE.Face3(
|
||||||
face.v0index,
|
face.v0index,
|
||||||
|
@ -722,18 +726,18 @@ GameLib.D3.Mesh.prototype.updateVerticesFromGeometryInstance = function(geometry
|
||||||
face.c,
|
face.c,
|
||||||
face.materialIndex,
|
face.materialIndex,
|
||||||
[[
|
[[
|
||||||
{
|
new GameLib.API.Vector2(
|
||||||
x: geometryInstance.faceVertexUvs[0][faceIndex][0].x,
|
geometryInstance.faceVertexUvs[0][faceIndex][0].x,
|
||||||
y: geometryInstance.faceVertexUvs[0][faceIndex][0].y
|
geometryInstance.faceVertexUvs[0][faceIndex][0].y
|
||||||
},
|
),
|
||||||
{
|
new GameLib.API.Vector2(
|
||||||
x: geometryInstance.faceVertexUvs[0][faceIndex][1].x,
|
geometryInstance.faceVertexUvs[0][faceIndex][1].x,
|
||||||
y: geometryInstance.faceVertexUvs[0][faceIndex][1].y
|
geometryInstance.faceVertexUvs[0][faceIndex][1].y
|
||||||
},
|
),
|
||||||
{
|
new GameLib.API.Vector2(
|
||||||
x: geometryInstance.faceVertexUvs[0][faceIndex][2].x,
|
geometryInstance.faceVertexUvs[0][faceIndex][2].x,
|
||||||
y: geometryInstance.faceVertexUvs[0][faceIndex][2].y
|
geometryInstance.faceVertexUvs[0][faceIndex][2].y
|
||||||
}
|
)
|
||||||
]],
|
]],
|
||||||
new GameLib.Color(
|
new GameLib.Color(
|
||||||
this.graphics,
|
this.graphics,
|
||||||
|
@ -1150,6 +1154,34 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() {
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push RigidBodies
|
||||||
|
*/
|
||||||
|
var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody);
|
||||||
|
rigidBodies.map(
|
||||||
|
function(rigidBody) {
|
||||||
|
|
||||||
|
if (rigidBody.parentMesh === this) {
|
||||||
|
GameLib.Utils.PushUnique(components, rigidBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
}.bind(this)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push Shapes
|
||||||
|
*/
|
||||||
|
var shapes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Shape);
|
||||||
|
shapes.map(
|
||||||
|
function(shape) {
|
||||||
|
|
||||||
|
if (shape.parentMesh === this) {
|
||||||
|
GameLib.Utils.PushUnique(components, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
}.bind(this)
|
||||||
|
);
|
||||||
|
|
||||||
return components;
|
return components;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -282,32 +282,64 @@ GameLib.D3.Mesh.Plane.prototype.createPhysicsObjects = function() {
|
||||||
GameLib.Event.GET_PHYSICS_ENGINE,
|
GameLib.Event.GET_PHYSICS_ENGINE,
|
||||||
null,
|
null,
|
||||||
function(data){
|
function(data){
|
||||||
var apiShapeSphere = new GameLib.D3.API.Shape(
|
|
||||||
|
/**
|
||||||
|
* Create the plane shape
|
||||||
|
* @type {GameLib.D3.API.Shape}
|
||||||
|
*/
|
||||||
|
var apiShapePlane = new GameLib.D3.API.Shape(
|
||||||
null,
|
null,
|
||||||
'Shape Plane (' + this.name + ')'
|
'Shape Plane (' + this.name + ')'
|
||||||
);
|
);
|
||||||
|
|
||||||
apiShapeSphere.parentMesh = this;
|
apiShapePlane.parentMesh = this;
|
||||||
|
|
||||||
var shapeSphere = new GameLib.D3.Shape.Plane(
|
var shapePlane = new GameLib.D3.Shape.Plane(
|
||||||
data.physics,
|
data.physics,
|
||||||
apiShapeSphere,
|
apiShapePlane
|
||||||
this.radius
|
|
||||||
);
|
);
|
||||||
|
|
||||||
var apiRigidBody = new GameLib.D3.API.RigidBody(
|
var apiRigidBody = new GameLib.D3.API.RigidBody(
|
||||||
null,
|
null,
|
||||||
'Rigid Body (' + this.name + ')'
|
'Rigid Body (' + this.name + ')',
|
||||||
|
0,
|
||||||
|
null,
|
||||||
|
new GameLib.API.Vector3(
|
||||||
|
this.position.x,
|
||||||
|
this.position.y,
|
||||||
|
this.position.z
|
||||||
|
),
|
||||||
|
new GameLib.API.Quaternion(
|
||||||
|
this.quaternion.x,
|
||||||
|
this.quaternion.y,
|
||||||
|
this.quaternion.z,
|
||||||
|
this.quaternion.w,
|
||||||
|
new GameLib.API.Vector3(
|
||||||
|
this.quaternion.axis.x,
|
||||||
|
this.quaternion.axis.y,
|
||||||
|
this.quaternion.axis.z
|
||||||
|
),
|
||||||
|
this.quaternion.angle
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
apiRigidBody.shapes.push(shapeSphere);
|
apiRigidBody.shapes.push(shapePlane);
|
||||||
|
|
||||||
|
apiRigidBody.parentMesh = this;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct the rigid body
|
||||||
|
* @type {GameLib.D3.RigidBody}
|
||||||
|
*/
|
||||||
var rigidBody = new GameLib.D3.RigidBody(
|
var rigidBody = new GameLib.D3.RigidBody(
|
||||||
data.physics,
|
data.physics,
|
||||||
apiRigidBody
|
apiRigidBody
|
||||||
);
|
);
|
||||||
|
|
||||||
rigidBody.parentMesh = this;
|
if (this.parentEntity instanceof GameLib.Entity) {
|
||||||
|
this.parentEntity.addComponent(shapePlane);
|
||||||
|
this.parentEntity.addComponent(rigidBody);
|
||||||
|
}
|
||||||
|
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
function(error){
|
function(error){
|
||||||
|
|
|
@ -151,6 +151,8 @@ GameLib.D3.Mesh.Sphere.prototype.createPhysicsObjects = function() {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
apiRigidBody.parentMesh = this;
|
||||||
|
|
||||||
apiRigidBody.shapes.push(shapeSphere);
|
apiRigidBody.shapes.push(shapeSphere);
|
||||||
|
|
||||||
var rigidBody = new GameLib.D3.RigidBody(
|
var rigidBody = new GameLib.D3.RigidBody(
|
||||||
|
@ -158,7 +160,10 @@ GameLib.D3.Mesh.Sphere.prototype.createPhysicsObjects = function() {
|
||||||
apiRigidBody
|
apiRigidBody
|
||||||
);
|
);
|
||||||
|
|
||||||
rigidBody.parentMesh = this;
|
if (this.parentEntity instanceof GameLib.Entity) {
|
||||||
|
this.parentEntity.addComponent(shapeSphere);
|
||||||
|
this.parentEntity.addComponent(rigidBody);
|
||||||
|
}
|
||||||
|
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
function(error){
|
function(error){
|
||||||
|
|
|
@ -111,18 +111,107 @@ GameLib.D3.PhysicsWorld.prototype.createInstance = function() {
|
||||||
function(contactMaterial) {
|
function(contactMaterial) {
|
||||||
if (contactMaterial && contactMaterial.instance) {
|
if (contactMaterial && contactMaterial.instance) {
|
||||||
instance.addContactMaterial(contactMaterial.instance);
|
instance.addContactMaterial(contactMaterial.instance);
|
||||||
|
} else {
|
||||||
|
console.warn('contact materials not loaded at time of PhysicsWorld.createInstance()');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.rigidBodies.map(
|
||||||
|
function(rigidBody) {
|
||||||
|
if (rigidBody && rigidBody.instance) {
|
||||||
|
/**
|
||||||
|
* Ensure the rigidBody knows its parentWorld
|
||||||
|
*/
|
||||||
|
rigidBody.parentWorld = this;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the rigid body to the instance world
|
||||||
|
*/
|
||||||
|
instance.add(rigidBody.instance);
|
||||||
|
} else {
|
||||||
|
console.warn('rigidbodies not loaded at time of PhysicsWorld.createInstance()');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.warn('broadphase or solver not loaded during PhysicsWorld.createInstance()');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.D3.PhysicsWorld.prototype.addRigidBody = function(rigidBody) {
|
||||||
|
|
||||||
|
if (rigidBody && rigidBody.instance) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the rigid body to the instance world
|
||||||
|
*/
|
||||||
|
this.instance.add(rigidBody.instance);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remember to set the parentWorld for this rigidBody
|
||||||
|
* @type {GameLib.D3.PhysicsWorld}
|
||||||
|
*/
|
||||||
|
rigidBody.parentWorld = this;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure this rigidBody is in our rigidBodies array, just not too many times..
|
||||||
|
*/
|
||||||
|
GameLib.Utils.PushUnique(this.rigidBodies, rigidBody);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.warn('Attempt to add rigidBody ' + rigidBody.name + ' without an instance');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param rigidBody
|
||||||
|
*/
|
||||||
|
GameLib.D3.PhysicsWorld.prototype.removeRigidBody = function(rigidBody) {
|
||||||
|
|
||||||
|
if (!rigidBody instanceof GameLib.D3.RigidBody) {
|
||||||
|
console.warn('not a rigid body');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the instance
|
||||||
|
*/
|
||||||
|
if (rigidBody.instance) {
|
||||||
|
|
||||||
|
this.instance.remove(rigidBody.instance);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.warn('Attempt to remove rigidBody ' + rigidBody.name + ' without an instance');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remember to set the parentWorld for this rigidBody
|
||||||
|
* @type {GameLib.D3.PhysicsWorld}
|
||||||
|
*/
|
||||||
|
rigidBody.parentWorld = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove from this rigidBodies array
|
||||||
|
*/
|
||||||
|
var index = this.rigidBodies.indexOf(rigidBody);
|
||||||
|
|
||||||
|
if (index !== -1) {
|
||||||
|
this.rigidBodies.splice(index, 1);
|
||||||
|
} else {
|
||||||
|
console.warn('could not remove a rigidbody from an array where it should have existed');
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -137,7 +226,7 @@ GameLib.D3.PhysicsWorld.prototype.updateInstance = function() {
|
||||||
this.instance.solver = this.solver.instance;
|
this.instance.solver = this.solver.instance;
|
||||||
this.instance.gravity = this.gravity.instance;
|
this.instance.gravity = this.gravity.instance;
|
||||||
|
|
||||||
//TODO add contact materials
|
//TODO add contact materials and rigidbodies ? - this functionality is actually part of the physics system..
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -179,18 +268,6 @@ GameLib.D3.PhysicsWorld.FromObject = function(graphics, objectComponent) {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.PhysicsWorld.prototype.step = function(deltaTime) {
|
|
||||||
|
|
||||||
if (this.isCannon) {
|
|
||||||
this.instance.step(deltaTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (this.isOimo) {
|
|
||||||
// //TODO: oimo
|
|
||||||
// }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GameLib.D3.PhysicsWorld.prototype.step = function(
|
// GameLib.D3.PhysicsWorld.prototype.step = function(
|
||||||
// fixedStep,
|
// fixedStep,
|
||||||
|
|
|
@ -41,6 +41,7 @@ GameLib.D3.RigidBody = function (
|
||||||
apiRigidBody.shapes,
|
apiRigidBody.shapes,
|
||||||
apiRigidBody.kinematic,
|
apiRigidBody.kinematic,
|
||||||
apiRigidBody.parentMesh,
|
apiRigidBody.parentMesh,
|
||||||
|
apiRigidBody.parentWorld,
|
||||||
apiRigidBody.parentEntity
|
apiRigidBody.parentEntity
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -73,7 +74,8 @@ GameLib.D3.RigidBody = function (
|
||||||
GameLib.Component.COMPONENT_RIGID_BODY,
|
GameLib.Component.COMPONENT_RIGID_BODY,
|
||||||
{
|
{
|
||||||
'shapes' : [GameLib.D3.Shape],
|
'shapes' : [GameLib.D3.Shape],
|
||||||
'parentMesh' : GameLib.D3.Mesh
|
'parentMesh' : GameLib.D3.Mesh,
|
||||||
|
'parentWorld' : GameLib.D3.PhysicsWorld
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -177,17 +179,12 @@ GameLib.D3.RigidBody.prototype.setFromParentMesh = function() {
|
||||||
this.instance.position.y = this.parentMesh.position.y;
|
this.instance.position.y = this.parentMesh.position.y;
|
||||||
this.instance.position.z = this.parentMesh.position.z;
|
this.instance.position.z = this.parentMesh.position.z;
|
||||||
|
|
||||||
// this.parentMesh.quaternion.x = this.parentMesh.instance.quaternion.x;
|
this.instance.quaternion.x = this.parentMesh.quaternion.x;
|
||||||
// this.parentMesh.quaternion.y = this.parentMesh.instance.quaternion.y;
|
this.instance.quaternion.y = this.parentMesh.quaternion.y;
|
||||||
// this.parentMesh.quaternion.z = this.parentMesh.instance.quaternion.z;
|
this.instance.quaternion.z = this.parentMesh.quaternion.z;
|
||||||
// this.parentMesh.quaternion.w = this.parentMesh.instance.quaternion.w;
|
this.instance.quaternion.w = this.parentMesh.quaternion.w;
|
||||||
//
|
|
||||||
// this.quaternion.x = this.parentMesh.quaternion.x;
|
|
||||||
// this.quaternion.y = this.parentMesh.quaternion.y;
|
|
||||||
// this.quaternion.z = this.parentMesh.quaternion.z;
|
|
||||||
// this.quaternion.w = this.parentMesh.quaternion.w;
|
|
||||||
|
|
||||||
this.updateInstance();
|
// this.updateInstance();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -217,6 +214,7 @@ GameLib.D3.RigidBody.prototype.toApiObject = function() {
|
||||||
this.shapes.map(function(shape){return GameLib.Utils.IdOrNull(shape)}),
|
this.shapes.map(function(shape){return GameLib.Utils.IdOrNull(shape)}),
|
||||||
this.kinematic,
|
this.kinematic,
|
||||||
GameLib.Utils.IdOrNull(this.parentMesh),
|
GameLib.Utils.IdOrNull(this.parentMesh),
|
||||||
|
GameLib.Utils.IdOrNull(this.parentWorld),
|
||||||
GameLib.Utils.IdOrNull(this.parentEntity)
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -31,4 +31,14 @@ GameLib.D3.Shape.Plane.prototype.createInstance = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.Shape.Plane.prototype.updateInstance = function() {
|
GameLib.D3.Shape.Plane.prototype.updateInstance = function() {
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Shape.Plane.FromObject = function(physics, objectShape) {
|
||||||
|
|
||||||
|
var apiShape = GameLib.D3.API.Shape.FromObject(objectShape);
|
||||||
|
|
||||||
|
return new GameLib.D3.Shape.Plane(
|
||||||
|
physics,
|
||||||
|
apiShape
|
||||||
|
);
|
||||||
};
|
};
|
|
@ -43,9 +43,23 @@ GameLib.D3.Shape.Sphere.prototype.createInstance = function() {
|
||||||
|
|
||||||
GameLib.D3.Shape.Sphere.prototype.updateInstance = function() {
|
GameLib.D3.Shape.Sphere.prototype.updateInstance = function() {
|
||||||
this.instance.radius = this.radius;
|
this.instance.radius = this.radius;
|
||||||
this.instance.updateAABB();
|
|
||||||
this.instance.updateBoundingSphereRadius();
|
this.instance.updateBoundingSphereRadius();
|
||||||
this.instance.updateEdges();
|
};
|
||||||
this.instance.updateNormals();
|
|
||||||
this.instance.updateTree();
|
|
||||||
|
GameLib.D3.Shape.Sphere.prototype.toApiObject = function() {
|
||||||
|
var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this);
|
||||||
|
apiShape.radius = this.radius;
|
||||||
|
return apiShape;
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Shape.Sphere.FromObject = function(physics, objectShape) {
|
||||||
|
|
||||||
|
var apiShape = GameLib.D3.API.Shape.FromObject(objectShape);
|
||||||
|
|
||||||
|
return new GameLib.D3.Shape.Sphere(
|
||||||
|
physics,
|
||||||
|
apiShape,
|
||||||
|
objectShape.radius
|
||||||
|
);
|
||||||
};
|
};
|
|
@ -56,24 +56,38 @@ GameLib.Entity.prototype.addComponent = function(component) {
|
||||||
|
|
||||||
GameLib.Utils.PushUnique(this.components, component);
|
GameLib.Utils.PushUnique(this.components, component);
|
||||||
|
|
||||||
/**
|
if (component instanceof GameLib.D3.Mesh) {
|
||||||
* Here we will dig into this component - find all its 'parentEntity' members - and update them accordingly
|
|
||||||
*/
|
|
||||||
component.buildIdToObject();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Also add the child components of this component as components of this entity
|
* For meshes, simply get the children components
|
||||||
*/
|
* @type {Array}
|
||||||
for (var property in component.idToObject) {
|
*/
|
||||||
if (component.idToObject.hasOwnProperty(property) &&
|
component.getChildrenComponents().map(function(childComponent){
|
||||||
component.idToObject[property] !== component &&
|
GameLib.Utils.PushUnique(this.components, childComponent);
|
||||||
component.idToObject[property] instanceof GameLib.Component
|
childComponent.parentEntity = this;
|
||||||
) {
|
}.bind(this))
|
||||||
GameLib.Utils.PushUnique(this.components, component.idToObject[property]);
|
|
||||||
component.idToObject[property].parentEntity = this;
|
} else {
|
||||||
|
/**
|
||||||
|
* Here we will dig into this component - find all its 'parentEntity' members - and update them accordingly
|
||||||
|
*/
|
||||||
|
component.buildIdToObject();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Also add the child components of this component as components of this entity
|
||||||
|
*/
|
||||||
|
for (var property in component.idToObject) {
|
||||||
|
if (component.idToObject.hasOwnProperty(property) &&
|
||||||
|
component.idToObject[property] !== component &&
|
||||||
|
component.idToObject[property] instanceof GameLib.Component
|
||||||
|
) {
|
||||||
|
GameLib.Utils.PushUnique(this.components, component.idToObject[property]);
|
||||||
|
component.idToObject[property].parentEntity = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finally, we are the boss component - update my parent entity
|
* Finally, we are the boss component - update my parent entity
|
||||||
* @type {GameLib.Entity}
|
* @type {GameLib.Entity}
|
||||||
|
|
|
@ -96,7 +96,12 @@ GameLib.GUI.prototype.addFolder = function(folderName) {
|
||||||
try {
|
try {
|
||||||
return this.instance.addFolder(folderName);
|
return this.instance.addFolder(folderName);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e.message);
|
try {
|
||||||
return null;
|
folderName += ' duplicate (' + GameLib.Utils.RandomId() + ')';
|
||||||
|
return this.instance.addFolder(folderName);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -376,10 +376,14 @@ GameLib.System.GUI.prototype.buildParentSelectionControl = function(folder, comp
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property === 'parentMesh') {
|
if (property === 'parentMesh') {
|
||||||
constructor = GameLib.D3.Mesh
|
constructor = GameLib.D3.Mesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
var options = GameLib.EntityManager.Instance.queryComponents(constructor).reduce(
|
if (property === 'parentWorld') {
|
||||||
|
constructor = GameLib.D3.PhysicsWorld;
|
||||||
|
}
|
||||||
|
|
||||||
|
var options = GameLib.EntityManager.Instance.queryComponents(constructor).reduce(
|
||||||
function(result, object) {
|
function(result, object) {
|
||||||
result[object.name] = object;
|
result[object.name] = object;
|
||||||
return result;
|
return result;
|
||||||
|
@ -419,6 +423,17 @@ GameLib.System.GUI.prototype.buildParentSelectionControl = function(folder, comp
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (property === 'parentWorld') {
|
||||||
|
GameLib.Event.Emit(
|
||||||
|
GameLib.Event.PARENT_WORLD_CHANGE,
|
||||||
|
{
|
||||||
|
originalWorld : this.initialValue,
|
||||||
|
newWorld : newComponent,
|
||||||
|
object : component
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
);
|
);
|
||||||
|
@ -623,6 +638,17 @@ GameLib.System.GUI.prototype.buildSelectControl = function(folder, componentTemp
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (property === 'parentWorld') {
|
||||||
|
GameLib.Event.Emit(
|
||||||
|
GameLib.Event.PARENT_WORLD_CHANGE,
|
||||||
|
{
|
||||||
|
originalWorld : originalComponent,
|
||||||
|
newWorld : newComponent,
|
||||||
|
object : component
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1069,9 +1095,13 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate,
|
||||||
) {
|
) {
|
||||||
controllers.push(folder.add(object, property, -100, 100, 0.001));
|
controllers.push(folder.add(object, property, -100, 100, 0.001));
|
||||||
} else if (
|
} else if (
|
||||||
property === 'rrradius'
|
property === 'friction'
|
||||||
) {
|
) {
|
||||||
controllers.push(folder.add(object, property, 0, 1000, 0.01));
|
controllers.push(folder.add(object, property, 0, 1000, 0.01));
|
||||||
|
} else if (
|
||||||
|
property === 'mass'
|
||||||
|
) {
|
||||||
|
controllers.push(folder.add(object, property, 0, 1000, 0.1));
|
||||||
} else if (
|
} else if (
|
||||||
property === 'thetaLength' ||
|
property === 'thetaLength' ||
|
||||||
property === 'angle'
|
property === 'angle'
|
||||||
|
@ -1525,6 +1555,7 @@ GameLib.System.GUI.prototype.buildGUI = function(data) {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
templateProperty === 'parentEntity' ||
|
templateProperty === 'parentEntity' ||
|
||||||
|
templateProperty === 'parentWorld' ||
|
||||||
templateProperty === 'parentMesh'
|
templateProperty === 'parentMesh'
|
||||||
) {
|
) {
|
||||||
this.buildParentSelectionControl(folder, componentTemplate, templateProperty);
|
this.buildParentSelectionControl(folder, componentTemplate, templateProperty);
|
||||||
|
|
|
@ -27,6 +27,7 @@ GameLib.System.Linking = function(
|
||||||
this.imageNotFoundSubscription = null;
|
this.imageNotFoundSubscription = null;
|
||||||
this.componentCreatedSubscription = null;
|
this.componentCreatedSubscription = null;
|
||||||
this.parentSceneChangeSubscription = null;
|
this.parentSceneChangeSubscription = null;
|
||||||
|
this.parentWorldChangeSubscription = null;
|
||||||
this.parentEntityChangeSubscription = null;
|
this.parentEntityChangeSubscription = null;
|
||||||
this.meshInstanceCreatedSubscription = null;
|
this.meshInstanceCreatedSubscription = null;
|
||||||
this.lightInstanceCreatedSubscription = null;
|
this.lightInstanceCreatedSubscription = null;
|
||||||
|
@ -39,6 +40,9 @@ GameLib.System.Linking = function(
|
||||||
this.materialTypeChangedSubscription = null;
|
this.materialTypeChangedSubscription = null;
|
||||||
this.arrayItemAddedSubscription = null;
|
this.arrayItemAddedSubscription = null;
|
||||||
this.instanceCreatedSubscription = null;
|
this.instanceCreatedSubscription = null;
|
||||||
|
this.shapeInstanceCreatedSubscription = null;
|
||||||
|
this.solverInstanceCreatedSubscription = null;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.System.Linking.prototype = Object.create(GameLib.System.prototype);
|
GameLib.System.Linking.prototype = Object.create(GameLib.System.prototype);
|
||||||
|
@ -56,6 +60,11 @@ GameLib.System.Linking.prototype.start = function() {
|
||||||
this.onParentSceneChange
|
this.onParentSceneChange
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.parentWorldChangeSubscription = this.subscribe(
|
||||||
|
GameLib.Event.PARENT_WORLD_CHANGE,
|
||||||
|
this.onParentWorldChange
|
||||||
|
);
|
||||||
|
|
||||||
this.parentEntityChangeSubscription = this.subscribe(
|
this.parentEntityChangeSubscription = this.subscribe(
|
||||||
GameLib.Event.PARENT_ENTITY_CHANGE,
|
GameLib.Event.PARENT_ENTITY_CHANGE,
|
||||||
this.onParentEntityChange
|
this.onParentEntityChange
|
||||||
|
@ -91,6 +100,11 @@ GameLib.System.Linking.prototype.start = function() {
|
||||||
this.materialInstanceCreated
|
this.materialInstanceCreated
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.physicsWorldInstanceCreatedSubscription = this.subscribe(
|
||||||
|
GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED,
|
||||||
|
this.physicsWorldInstanceCreated
|
||||||
|
);
|
||||||
|
|
||||||
this.imageNotFoundSubscription = this.subscribe(
|
this.imageNotFoundSubscription = this.subscribe(
|
||||||
GameLib.Event.IMAGE_NOT_FOUND,
|
GameLib.Event.IMAGE_NOT_FOUND,
|
||||||
this.imageNotFound
|
this.imageNotFound
|
||||||
|
@ -119,7 +133,23 @@ GameLib.System.Linking.prototype.start = function() {
|
||||||
this.arrayItemAddedSubscription = this.subscribe(
|
this.arrayItemAddedSubscription = this.subscribe(
|
||||||
GameLib.Event.ARRAY_ITEM_ADDED,
|
GameLib.Event.ARRAY_ITEM_ADDED,
|
||||||
this.arrayItemAdded
|
this.arrayItemAdded
|
||||||
)
|
);
|
||||||
|
|
||||||
|
this.shapeInstanceCreatedSubscription = this.subscribe(
|
||||||
|
GameLib.Event.SHAPE_INSTANCE_CREATED,
|
||||||
|
this.shapeInstanceCreated
|
||||||
|
);
|
||||||
|
|
||||||
|
this.solverInstanceCreatedSubscription = this.subscribe(
|
||||||
|
GameLib.Event.SOLVER_INSTANCE_CREATED,
|
||||||
|
this.solverInstanceCreated
|
||||||
|
);
|
||||||
|
|
||||||
|
this.instanceCreatedSubscription = this.subscribe(
|
||||||
|
GameLib.Event.INSTANCE_CREATED,
|
||||||
|
this.instanceCreated
|
||||||
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.System.Linking.prototype.link = function(component, data) {
|
GameLib.System.Linking.prototype.link = function(component, data) {
|
||||||
|
@ -659,6 +689,78 @@ GameLib.System.Linking.prototype.imageInstanceCreated = function(data) {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.System.Linking.prototype.solverInstanceCreated = function(data) {
|
||||||
|
console.log(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.System.Linking.prototype.arrayItemAdded = function(data) {
|
||||||
|
if (
|
||||||
|
data.component instanceof GameLib.D3.PhysicsWorld &&
|
||||||
|
data.item instanceof GameLib.D3.RigidBody
|
||||||
|
) {
|
||||||
|
data.component.addRigidBody(data.item);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.System.Linking.prototype.instanceCreated = function(data) {
|
||||||
|
|
||||||
|
if (data.component instanceof GameLib.D3.RaycastVehicle) {
|
||||||
|
|
||||||
|
var worlds = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First ensure our parent world is linked
|
||||||
|
*/
|
||||||
|
worlds.map(
|
||||||
|
function(world) {
|
||||||
|
if (data.component.parentWorld === world.id) {
|
||||||
|
data.component.parentWorld = world;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!data.component.parentWorld) {
|
||||||
|
console.log('no parent world');
|
||||||
|
} else {
|
||||||
|
data.component.instance.addToWorld(data.component.parentWorld.instance);
|
||||||
|
console.log('raycast vehicle instance added to its parent world');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.component instanceof GameLib.D3.RigidBody) {
|
||||||
|
|
||||||
|
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
||||||
|
|
||||||
|
meshes.map(
|
||||||
|
function(mesh) {
|
||||||
|
if (data.component.parentMesh === mesh.id) {
|
||||||
|
data.component.parentMesh = mesh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.restart();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.System.Linking.prototype.shapeInstanceCreated = function(data) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a shape instance is created, just check if the parentMesh is loaded somewhere and set it.
|
||||||
|
*
|
||||||
|
* We also do the reverse, when a mesh instance is created, we check if its the parent of any shape.
|
||||||
|
*/
|
||||||
|
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
||||||
|
meshes.map(
|
||||||
|
function(mesh) {
|
||||||
|
if (data.shape.parentMesh === mesh.id) {
|
||||||
|
data.shape.parentMesh = mesh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
GameLib.System.Linking.prototype.textureInstanceCreated = function(data) {
|
GameLib.System.Linking.prototype.textureInstanceCreated = function(data) {
|
||||||
this.resolveDependencies(data.texture);
|
this.resolveDependencies(data.texture);
|
||||||
};
|
};
|
||||||
|
@ -667,6 +769,37 @@ GameLib.System.Linking.prototype.materialInstanceCreated = function(data) {
|
||||||
this.resolveDependencies(data.material);
|
this.resolveDependencies(data.material);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.System.Linking.prototype.physicsWorldInstanceCreated = function(data) {
|
||||||
|
|
||||||
|
var raycastVehicles = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastVehicle);
|
||||||
|
raycastVehicles.map(
|
||||||
|
function(vehicle) {
|
||||||
|
if (vehicle.parentWorld === data.world.id) {
|
||||||
|
vehicle.parentWorld = data.world;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody);
|
||||||
|
rigidBodies.map(
|
||||||
|
function(rigidBody){
|
||||||
|
if (rigidBody.parentWorld === data.world.id) {
|
||||||
|
rigidBody.parentWorld = data.world;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var contactMaterials = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.FrictionContactMaterial);
|
||||||
|
contactMaterials.map(
|
||||||
|
function(contactMaterial) {
|
||||||
|
if (contactMaterial.parentWorld === data.world.id) {
|
||||||
|
contactMaterial.parentWorld = data.world;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
GameLib.System.Linking.prototype.materialTypeChanged = function(data) {
|
GameLib.System.Linking.prototype.materialTypeChanged = function(data) {
|
||||||
|
|
||||||
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
||||||
|
@ -704,6 +837,27 @@ GameLib.System.Linking.prototype.materialTypeChanged = function(data) {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param data
|
||||||
|
*/
|
||||||
|
GameLib.System.Linking.prototype.onParentWorldChange = function(data) {
|
||||||
|
|
||||||
|
if (
|
||||||
|
data.object instanceof GameLib.D3.RigidBody
|
||||||
|
) {
|
||||||
|
|
||||||
|
if (data.originalWorld instanceof GameLib.D3.PhysicsWorld) {
|
||||||
|
data.originalWorld.removeRigidBody(data.object);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.newWorld instanceof GameLib.D3.PhysicsWorld) {
|
||||||
|
data.newWorld.addRigidBody(data.object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines what should happen when a parent scene changes
|
* Defines what should happen when a parent scene changes
|
||||||
* @param data
|
* @param data
|
||||||
|
@ -848,6 +1002,7 @@ GameLib.System.Linking.prototype.stop = function() {
|
||||||
this.imageNotFoundSubscription.remove();
|
this.imageNotFoundSubscription.remove();
|
||||||
this.componentCreatedSubscription.remove();
|
this.componentCreatedSubscription.remove();
|
||||||
this.parentSceneChangeSubscription.remove();
|
this.parentSceneChangeSubscription.remove();
|
||||||
|
this.parentWorldChangeSubscription.remove();
|
||||||
this.parentEntityChangeSubscription.remove();
|
this.parentEntityChangeSubscription.remove();
|
||||||
this.meshInstanceCreatedSubscription.remove();
|
this.meshInstanceCreatedSubscription.remove();
|
||||||
this.lightInstanceCreatedSubscription.remove();
|
this.lightInstanceCreatedSubscription.remove();
|
||||||
|
@ -860,5 +1015,8 @@ GameLib.System.Linking.prototype.stop = function() {
|
||||||
this.materialTypeChangedSubscription.remove();
|
this.materialTypeChangedSubscription.remove();
|
||||||
this.instanceCreatedSubscription.remove();
|
this.instanceCreatedSubscription.remove();
|
||||||
this.arrayItemAddedSubscription.remove();
|
this.arrayItemAddedSubscription.remove();
|
||||||
|
this.physicsWorldInstanceCreatedSubscription.remove();
|
||||||
|
this.shapeInstanceCreatedSubscription.remove();
|
||||||
|
this.solverInstanceCreatedSubscription.remove();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,12 @@ GameLib.System.Physics = function(
|
||||||
);
|
);
|
||||||
|
|
||||||
this.worlds = [];
|
this.worlds = [];
|
||||||
this.rigidBodies = [];
|
// this.rigidBodies = [];
|
||||||
this.wheels = [];
|
// this.wheels = [];
|
||||||
this.vehicles = [];
|
// this.vehicles = [];
|
||||||
|
|
||||||
this.worldSubscription = null;
|
// this.worldSubscription = null;
|
||||||
this.rigidBodySubscription = null;
|
// this.rigidBodySubscription = null;
|
||||||
this.beforeRenderSubscription = null;
|
this.beforeRenderSubscription = null;
|
||||||
this.afterRenderSubscription = null;
|
this.afterRenderSubscription = null;
|
||||||
|
|
||||||
|
@ -33,65 +33,42 @@ GameLib.System.Physics.prototype.constructor = GameLib.System.Physics;
|
||||||
GameLib.System.Physics.prototype.start = function() {
|
GameLib.System.Physics.prototype.start = function() {
|
||||||
|
|
||||||
this.worlds = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld);
|
this.worlds = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld);
|
||||||
this.rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody);
|
// this.rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody);
|
||||||
this.wheels = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastWheel);
|
// this.wheels = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastWheel);
|
||||||
this.vehicles = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastVehicle);
|
// this.vehicles = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastVehicle);
|
||||||
|
|
||||||
this.shapeInstanceCreatedSubscription = this.subscribe(
|
|
||||||
GameLib.Event.SHAPE_INSTANCE_CREATED,
|
|
||||||
this.shapeInstanceCreated
|
|
||||||
);
|
|
||||||
|
|
||||||
this.solverInstanceCreatedSubscription = this.subscribe(
|
|
||||||
GameLib.Event.SOLVER_INSTANCE_CREATED,
|
|
||||||
this.solverInstanceCreated
|
|
||||||
);
|
|
||||||
|
|
||||||
this.physicsWorldInstanceCreatedSubscription = this.subscribe(
|
|
||||||
GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED,
|
|
||||||
this.physicsWorldInstanceCreated
|
|
||||||
);
|
|
||||||
|
|
||||||
this.instanceCreatedSubscription = this.subscribe(
|
|
||||||
GameLib.Event.INSTANCE_CREATED,
|
|
||||||
this.instanceCreated
|
|
||||||
);
|
|
||||||
|
|
||||||
this.arrayItemAddedSubscription = this.subscribe(
|
|
||||||
GameLib.Event.ARRAY_ITEM_ADDED,
|
|
||||||
this.arrayItemAdded
|
|
||||||
);
|
|
||||||
|
|
||||||
this.worlds.map(
|
|
||||||
function(world) {
|
|
||||||
world.instance.addEventListener(
|
|
||||||
'postStep',
|
|
||||||
function() {
|
|
||||||
|
|
||||||
this.vehicles.map(
|
|
||||||
function(vehicle) {
|
|
||||||
vehicle.instance.wheelInfos.map(
|
|
||||||
function(wheelInfo, index) {
|
|
||||||
vehicle.instance.updateWheelTransform(index);
|
|
||||||
var t = wheelInfo.worldTransform;
|
|
||||||
// vehicle.wheels[index].instance.position.copy(t.position);
|
|
||||||
// vehicle.wheels[index].instance.quaternion.copy(t.quaternion);
|
|
||||||
|
|
||||||
// vehicle.raycastWheels[index].parentMesh.localPosition.x = t.position.x;
|
|
||||||
// vehicle.raycastWheels[index].parentMesh.localPosition.y = t.position.y;
|
|
||||||
// vehicle.raycastWheels[index].parentMesh.localPosition.z = t.position.z;
|
|
||||||
|
|
||||||
// vehicle.raycastWheels[index].parentMesh.updateInstance();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
}.bind(this)
|
|
||||||
)
|
// this.worlds.map(
|
||||||
}.bind(this)
|
// function(world) {
|
||||||
);
|
// world.instance.addEventListener(
|
||||||
|
// 'postStep',
|
||||||
|
// function() {
|
||||||
|
//
|
||||||
|
// this.vehicles.map(
|
||||||
|
// function(vehicle) {
|
||||||
|
// vehicle.instance.wheelInfos.map(
|
||||||
|
// function(wheelInfo, index) {
|
||||||
|
// vehicle.instance.updateWheelTransform(index);
|
||||||
|
// var t = wheelInfo.worldTransform;
|
||||||
|
// // vehicle.wheels[index].instance.position.copy(t.position);
|
||||||
|
// // vehicle.wheels[index].instance.quaternion.copy(t.quaternion);
|
||||||
|
//
|
||||||
|
// // vehicle.raycastWheels[index].parentMesh.localPosition.x = t.position.x;
|
||||||
|
// // vehicle.raycastWheels[index].parentMesh.localPosition.y = t.position.y;
|
||||||
|
// // vehicle.raycastWheels[index].parentMesh.localPosition.z = t.position.z;
|
||||||
|
//
|
||||||
|
// // vehicle.raycastWheels[index].parentMesh.updateInstance();
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// }.bind(this)
|
||||||
|
// )
|
||||||
|
// }.bind(this)
|
||||||
|
// );
|
||||||
|
|
||||||
this.beforeRenderSubscription = this.subscribe(
|
this.beforeRenderSubscription = this.subscribe(
|
||||||
GameLib.Event.BEFORE_RENDER,
|
GameLib.Event.BEFORE_RENDER,
|
||||||
|
@ -106,173 +83,50 @@ GameLib.System.Physics.prototype.beforeRender = function(data) {
|
||||||
|
|
||||||
this.worlds.map(
|
this.worlds.map(
|
||||||
function(world) {
|
function(world) {
|
||||||
world.instance.step(data.delta);
|
|
||||||
|
|
||||||
this.rigidBodies.map(
|
if (world.loaded && world.instance) {
|
||||||
function(rigidBody){
|
|
||||||
rigidBody.position.x = rigidBody.instance.position.x;
|
|
||||||
rigidBody.position.y = rigidBody.instance.position.y;
|
|
||||||
rigidBody.position.z = rigidBody.instance.position.z;
|
|
||||||
|
|
||||||
rigidBody.quaternion.x = rigidBody.instance.quaternion.x;
|
world.instance.step(data.delta);
|
||||||
rigidBody.quaternion.y = rigidBody.instance.quaternion.y;
|
|
||||||
rigidBody.quaternion.z = rigidBody.instance.quaternion.z;
|
|
||||||
rigidBody.quaternion.w = rigidBody.instance.quaternion.w;
|
|
||||||
|
|
||||||
rigidBody.parentMesh.position.x = rigidBody.instance.position.x;
|
world.rigidBodies.map(
|
||||||
rigidBody.parentMesh.position.y = rigidBody.instance.position.y;
|
function(rigidBody){
|
||||||
rigidBody.parentMesh.position.z = rigidBody.instance.position.z;
|
rigidBody.position.x = rigidBody.instance.position.x;
|
||||||
|
rigidBody.position.y = rigidBody.instance.position.y;
|
||||||
|
rigidBody.position.z = rigidBody.instance.position.z;
|
||||||
|
|
||||||
rigidBody.parentMesh.quaternion.x = rigidBody.instance.quaternion.x;
|
rigidBody.quaternion.x = rigidBody.instance.quaternion.x;
|
||||||
rigidBody.parentMesh.quaternion.y = rigidBody.instance.quaternion.y;
|
rigidBody.quaternion.y = rigidBody.instance.quaternion.y;
|
||||||
rigidBody.parentMesh.quaternion.z = rigidBody.instance.quaternion.z;
|
rigidBody.quaternion.z = rigidBody.instance.quaternion.z;
|
||||||
rigidBody.parentMesh.quaternion.w = rigidBody.instance.quaternion.w;
|
rigidBody.quaternion.w = rigidBody.instance.quaternion.w;
|
||||||
|
|
||||||
rigidBody.instance.getVelocityAtWorldPoint(new CANNON.Vec3(0,0,0), rigidBody.velocity.instance);
|
rigidBody.parentMesh.position.x = rigidBody.instance.position.x;
|
||||||
|
rigidBody.parentMesh.position.y = rigidBody.instance.position.y;
|
||||||
|
rigidBody.parentMesh.position.z = rigidBody.instance.position.z;
|
||||||
|
|
||||||
rigidBody.velocity.x = rigidBody.velocity.instance.x;
|
rigidBody.parentMesh.quaternion.x = rigidBody.instance.quaternion.x;
|
||||||
rigidBody.velocity.y = rigidBody.velocity.instance.y;
|
rigidBody.parentMesh.quaternion.y = rigidBody.instance.quaternion.y;
|
||||||
rigidBody.velocity.z = rigidBody.velocity.instance.z;
|
rigidBody.parentMesh.quaternion.z = rigidBody.instance.quaternion.z;
|
||||||
|
rigidBody.parentMesh.quaternion.w = rigidBody.instance.quaternion.w;
|
||||||
|
|
||||||
rigidBody.parentMesh.updateRotationFromAxisAngle = false;
|
rigidBody.instance.getVelocityAtWorldPoint(new CANNON.Vec3(0,0,0), rigidBody.velocity.instance);
|
||||||
|
|
||||||
rigidBody.parentMesh.updateInstance();
|
rigidBody.velocity.x = rigidBody.velocity.instance.x;
|
||||||
|
rigidBody.velocity.y = rigidBody.velocity.instance.y;
|
||||||
|
rigidBody.velocity.z = rigidBody.velocity.instance.z;
|
||||||
|
|
||||||
|
rigidBody.parentMesh.updateRotationFromAxisAngle = false;
|
||||||
|
|
||||||
|
rigidBody.parentMesh.updateInstance();
|
||||||
|
|
||||||
|
rigidBody.parentMesh.updateRotationFromAxisAngle = true;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
rigidBody.parentMesh.updateRotationFromAxisAngle = true;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.solverInstanceCreated = function(data) {
|
|
||||||
|
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.arrayItemAdded = function(data) {
|
|
||||||
|
|
||||||
if (
|
|
||||||
data.component instanceof GameLib.D3.PhysicsWorld &&
|
|
||||||
data.item instanceof GameLib.D3.RigidBody
|
|
||||||
) {
|
|
||||||
data.component.instance.add(data.item.instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.instanceCreated = function(data) {
|
|
||||||
|
|
||||||
if (data.component instanceof GameLib.D3.RaycastVehicle) {
|
|
||||||
|
|
||||||
var worlds = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* First ensure our parent world is linked
|
|
||||||
*/
|
|
||||||
worlds.map(
|
|
||||||
function(world) {
|
|
||||||
if (data.component.parentWorld === world.id) {
|
|
||||||
data.component.parentWorld = world;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!data.component.parentWorld) {
|
|
||||||
console.log('no parent world');
|
|
||||||
} else {
|
|
||||||
data.component.instance.addToWorld(data.component.parentWorld.instance);
|
|
||||||
console.log('raycast vehicle instance added to its parent world');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.component instanceof GameLib.D3.RigidBody) {
|
|
||||||
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
|
||||||
|
|
||||||
meshes.map(
|
|
||||||
function(mesh) {
|
|
||||||
if (data.component.parentMesh === mesh.id) {
|
|
||||||
data.component.parentMesh = mesh;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* By default - add this rigidbody to all physics worlds
|
|
||||||
*/
|
|
||||||
this.worlds.map(function(world){
|
|
||||||
world.rigidBodies.push(data.component);
|
|
||||||
world.instance.add(data.component.instance);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.restart();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.physicsWorldInstanceCreated = function(data) {
|
|
||||||
|
|
||||||
var raycastVehicles = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastVehicle);
|
|
||||||
|
|
||||||
raycastVehicles.map(
|
|
||||||
function(vehicle) {
|
|
||||||
if (vehicle.parentWorld === data.world.id) {
|
|
||||||
vehicle.parentWorld = data.world;
|
|
||||||
// vehicle.instance.addToWorld(data.world.instance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// var contactMaterials = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.FrictionContactMaterial);
|
|
||||||
//
|
|
||||||
// data.world.contactMaterials.map(
|
|
||||||
// function() {
|
|
||||||
// if ()
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.shapeInstanceCreated = function(data) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* When a shape instance is created, just check if the parentMesh is loaded somewhere and set it.
|
|
||||||
*
|
|
||||||
* We also do the reverse, when a mesh instance is created, we check if its the parent of any shape.
|
|
||||||
*/
|
|
||||||
var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh);
|
|
||||||
meshes.map(
|
|
||||||
function(mesh) {
|
|
||||||
if (data.shape.parentMesh === mesh.id) {
|
|
||||||
data.shape.parentMesh = mesh;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* We need to now, find the rigid bodies to which the shape belongs, and add it - also the reverse ?
|
|
||||||
*
|
|
||||||
* this is not ok - because we are not dealing with links to parent objects - these are dependencies which need
|
|
||||||
* to be resolved - an array of dependencies... eeek...
|
|
||||||
*/
|
|
||||||
// var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody);
|
|
||||||
// rigidBodies.map(function(rigidBody){
|
|
||||||
//
|
|
||||||
// rigidBody.shapes = rigidBody.shapes.reduce(
|
|
||||||
// function(result, shape) {
|
|
||||||
// if (shape === data.shape.id) {
|
|
||||||
// result.push(data.shape);
|
|
||||||
// }
|
|
||||||
// return result;
|
|
||||||
// },
|
|
||||||
// []
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// });
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
GameLib.System.Physics.prototype.stop = function() {
|
GameLib.System.Physics.prototype.stop = function() {
|
||||||
|
|
||||||
|
@ -281,14 +135,6 @@ GameLib.System.Physics.prototype.stop = function() {
|
||||||
this.wheels = [];
|
this.wheels = [];
|
||||||
this.vehicles = [];
|
this.vehicles = [];
|
||||||
|
|
||||||
if (this.worldSubscription) {
|
|
||||||
this.worldSubscription.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.rigidBodySubscription) {
|
|
||||||
this.rigidBodySubscription.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.beforeRenderSubscription) {
|
if (this.beforeRenderSubscription) {
|
||||||
this.beforeRenderSubscription.remove();
|
this.beforeRenderSubscription.remove();
|
||||||
}
|
}
|
||||||
|
@ -297,11 +143,5 @@ GameLib.System.Physics.prototype.stop = function() {
|
||||||
this.afterRenderSubscription.remove();
|
this.afterRenderSubscription.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.shapeInstanceCreatedSubscription.remove();
|
|
||||||
this.solverInstanceCreatedSubscription.remove();
|
|
||||||
this.physicsWorldInstanceCreatedSubscription.remove();
|
|
||||||
this.instanceCreatedSubscription.remove();
|
|
||||||
this.arrayItemAddedSubscription.remove();
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue