Game Development Reference
In-Depth Information
this.rigidBody.applyWorldImpulse(jigLib.Vector3DUtil.
create(impulse[0],impulse[1],impulse[2]),jigLib.Vector3DUtil.
create(pos.x,pos.y,pos.z));
this.system.addBody(this.rigidBody);
this.rigidBody.addEventListener(jigLib.JCollisionEvent.COLLISION,
function(event){
console.log(event);
//event.collisionBody
//event.collisionInfo
});
}
In the preceding code, we showed both the impulse and the force application code
examples. However, we have commented out the force code because for the grenade,
we can use either force or impulse, but actually we need to give it just some initial
velocity in the desired direction. Also, if you notice, we have added code to handle
the collision event but just logged the event ( this.rigidBody.addEventListener
(jigLib.JCollisionEvent.COLLISION,function(event) ). For this code, we chose
to use the collisions array, which is explained later. The extractOrientation
function is shown in the following code:
Grenade.prototype.extractOrientation=function(matrix){
matrix[12]=0;
matrix[13]=0;
matrix[14]=0;
}
Our update function invoked from the drawScene function is straightforward. It
simply reads the position of the rigid body and translates the geometric shape of the
grenade's modelMatrix with it.
We also check for the collisions array in our update function. If the length of the
collisions array is greater than 0 , this denotes that a collision has occurred. We
toggle the visibility of the grenade and remove it from the physics world ( this.
system.removeBody(this.rigidBody); ). Remember, we need to remove objects
that are not visible to the user from the physics world; otherwise, you will slow
down the system. We then invoke the event handler ( this.callBack (this.
rigidBody, this.rigidBody.collisions); ). The variable, this.callback ,
is declared in the parent class StageObject . We also clear our collisions array
( this.rigidBody.collisions=[]; ). Our update function is as follows:
Grenade.prototype.update=function() {
if(this.rigidBody){
var pos = this.rigidBody.get_currentState().position;
mat4.identity(this.modelMatrix);
this.location=vec3.fromValues(pos[0], pos[1], pos[2]);
 
Search WWH ::




Custom Search