trigger sphere

beta.r3js.org
polygonboutique 2016-11-09 14:17:13 +01:00
parent ac820f70ba
commit a3f552b941
1 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,71 @@
//
// uses the entity position + mesh bounding sphere to check if intersections
//
GameLib.D3.ComponentTriggerSphereSphere = function(
componentId,
sphereRadius,
entitiesToCheck,
callback
) {
this.componentId = componentId || GameLib.D3.Tools.RandomId();
this.parentEntity = null;
// 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.ComponentTriggerSphereSphere, GameLib.D3.ComponentInterface);
this.entitiesToCheck = entitiesToCheck || [];
this.sphereRadius = sphereRadius || 1.0;
this.callback = callback || null;
};
if(typeof THREE != "undefined") {
ComponentTriggerSphereSphere_spherePosition_Vector3 = new THREE.Vector3();
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3 = new THREE.Vector3();
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 0.0;
ComponentTriggerSphereSphere_sphereToSphere_Vector3 = new THREE.Vector3();
}
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentTriggerSphereSphere.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
ComponentTriggerSphereSphere_spherePosition_Vector3.set(
parentEntity.position.x,
parentEntity.position.y,
parentEntity.position.z
);
for(var e in this.entitiesToCheck) {
var entityToCheck = this.entitiesToCheck[e];
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.set(
entityToCheck.position.x,
entityToCheck.position.y,
entityToCheck.position.z
);
if(entityToCheck.mesh && entityToCheck.mesh.geometry.boundingSphere) {
ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius;
} else {
ComponentTriggerSphereSphere_targetBoundingSphereRadius = 0.0;
}
// do sphere sphere collision
ComponentTriggerSphereSphere_sphereToSphere_Vector3.set(
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.x - ComponentTriggerSphereSphere_spherePosition_Vector3.x,
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.y - ComponentTriggerSphereSphere_spherePosition_Vector3.y,
ComponentTriggerSphereSphere_targetBoundingSpherePosition_Vector3.z - ComponentTriggerSphereSphere_spherePosition_Vector3.z
);
var length = ComponentTriggerSphereSphere_sphereToSphere_Vector3.length();
if((length - (ComponentTriggerSphereSphere_targetBoundingSphereRadius + this.sphereRadius)) < 0) {
if(this.callback) {
this.callback(parentEntity, entityToCheck);
}
}
}
};