Game Development Reference
In-Depth Information
mat4.translate(this.modelMatrix,this.modelMatrix,
this.location);
mat4.scale(this.modelMatrix,this.modelMatrix,
vec3.fromValues(5,5,5));
if(this.rigidBody.collisions.length>0){
this.system.removeBody(this.rigidBody);
console.log(this.rigidBody.collisions);
if(this.callBack){
this.callBack(this.rigidBody,this.rigidBody.collisions);
}
this.visible=false;
this.rigidBody.collisions=[];
}
}
Now, let's quickly look at the implementation of the preceding code. Open the
07-Grenade-Action-Blast-Physics.html
file in your editor. The following code
snippet is present in this file:
function loadStageObject(url,location,rotationX,rotationY,
rotationZ){
...
else if(nameOfObject=="grenade"){
stageObject=new Grenade();
grenade=stageObject;
grenade.system=system;
grenade.callBack=initializeExplosion;
}
...
}
In our preceding code, we initialize our
grenade
object and assign the physics object
and the collision
callback
function. The collision
callBack
function takes
rigidBody
as a parameter. We retrieve the position of
rigidBody
, create a
modelMatrix
parameter, and translate the model matrix to the
rigidBody
parameter's position. We
then initialize our explosion with
modelMatrix
. The
initializeExplosion
function
is given in the following code:
function initializeExplosion(rigidBody,collisions){
var pos = rigidBody.get_currentState().position;
var modelMatrix=mat4.create();
mat4.identity(modelMatrix);
var location=vec3.fromValues(pos[0], pos[1], pos[2]);
mat4.translate(modelMatrix,modelMatrix,location);
explosion.initialize(modelMatrix);
}