Game Development Reference
In-Depth Information
this.rotations.push((j*22/7)/180);
}
this.visible=false;
this.counter=0;
this.grenadeCallback=null;
});
In the
update
function, we obtain the camera view matrix. Calculate the camera
matrix, then translate the arm with respect to the camera using the matrix. Then,
apply a rotation transformation along the
x
axis with an angle from the
rotations
array. Note that in the case of the left arm, we use the latest value of the camera view
matrix throughout the animation. This is done so that during the animation if you
rotate or translate your camera, the arm moves with it. Also, when we have iterated
over the complete set of rotations, it resets the
counter
, toggles visibility, and invokes
the callback function.
LeftHand.prototype.update=function() {
if(this.counter<this.rotations.length) {
var mMatrix=mat4.clone(this.camera.viewMatrix);
mat4.invert(mMatrix,mMatrix);
mat4.translate(this.modelMatrix,mMatrix,vec3.fromValues(-1.5,-3,-6));
mat4.rotateX(this.modelMatrix,this.modelMatrix,this.rotations[this.
counter]);
this.counter=this.counter+1;
}else{
this.counter=0;
this.visible=false;
this.grenadeCallback();
}
}
Open the
grenade.js
file from the
primitive/game
folder in your editor.
The
Grenade
class is very similar to the
Bullet
class except for one difference: we
do not calculate the interpolates in the initialize function. We calculate it over the
control points once the application is loaded in the main control code. Each time the
animation is required to run, we pass the
positions
array from the main code as a
parameter to the
initialize
function.
Grenade= inherit(StageObject, function (){
superc(this);
this.positions=[];
this.counter=0;
this.visible=false;
this.initialModelMatrix=mat4.create();
});