Game Development Reference
In-Depth Information
We define positions (interpolated points) with respect to the camera, not the scene.
However, if we move our arm after firing the bullet, then the bullet should not
change its initial trajectory, which means the bullet should use the initial camera
matrix to calculate its transformation.
Open the
Bullet.js
file from the
primitive/game
folder in your editor.
We first inherit our
StageObject
to create the
Bullet
class. We have added three
new variables in the class:
positions
to hold the calculated set of interpolated
points for the animation trajectory,
initialModelMatrix
to hold the initial camera
matrix so that the bullet does not change the trajectory after firing, even if the camera
changes its location/orientation, and lastly,
counter
to maintain the index of the
current position once the bullet is fired. Also note that we have set the
visible
property of the bullet to
false
so that the bullet is initially not visible, as shown in
the following code:
Bullet= inherit(StageObject, function (){
superc(this);
this.visible=false;
this.positions=[];
this.initialModelMatrix=mat4.create();
this.counter=0;
});
We have implemented the
initialize
function of the
StageObject
class in the
Bullet
class. This function first clones the camera view matrix, and then stores the
camera matrix in
initialModelMatrix
. It initializes the
position
array by invoking
calculatePositions
for a set of 200 points, and sets
counter
to
0
and
visible
to
true
, as shown in the following code:
Bullet.prototype.initialize=function() {
var mMatrix=mat4.clone(this.camera.viewMatrix);
mat4.invert(this.initialModelMatrix,mMatrix);
var count=200;
this.positions=[];
for(var i=0;i<count;++i){
this.positions.push(this.calculatePosition(i*this.steps));
}
this.counter=0;
this.visible=true;
}