Game Development Reference
In-Depth Information
The
update
function increments the counter value. The
rigidBody
parameter
updates its position by invoking the
initializePosition
function using the
incremented counter value. After we are through with iterating over the
positions
array, we do not set the position of
rigidBody
and let the physics engine take over.
We translate
modelMatrix
by the new position of
rigidBody
. Note that for all cases,
we read the
rigidBody
coordinate so that when the physics engine takes over, we
move the geometry with
rigidBody
.
Earlier, we were toggling the visibility of the bullet when we had iterated over all
values of the positions. However, in this case, we wait for the collision to occur, for
example, when the bullet hits the ground.
We check the length of the
collisions
array to test the collision. Then, we remove
the rigid body from the physics system and also toggle its visibility. The
update
function of the
Bullet
class is as follows:
Bullet.prototype.update=function() {
if(this.rigidBody){
if(this.counter<this.positions.length-1){
this.counter=this.counter+1;
this.initializePosition();
}
var pos=this.rigidBody.get_currentState().position;
mat4.identity(this.modelMatrix);
mat4.translate(this.modelMatrix,this.modelMatrix,vec3.
fromValues(pos[0],pos[1],pos[2]));
if(this.rigidBody.collisions.length>0){
this.visible=false;
this.system.removeBody(this.rigidBody);
this.rigidBody.collisions=[];
}
mat4.scale(this.modelMatrix,this.modelMatrix,vec3.
fromValues(5,5,5));
}else{
...
}
}
Let's also walk through the bullet's implementation in the main code. Open the
07-Grenade-Action-Blast-Physics.html
file in your editor.