Game Development Reference
In-Depth Information
In our grenade initialize function, we initialize our rigid body physics. We invoke
the this.initializePhysics() function after we calculate our camera matrix. We
still need our camera matrix for two things: the initial location of the grenade and
the final orientation of the force/impulse applied on the grenade. Remember, we
are using the first-person camera, and all initial values (location, force) will be with
respect to our model (camera) space. Then, we will transform it to world space using
the model matrix. Our grenade initialize function is defined as follows:
Grenade.prototype.initialize=function(positions) {
var mMatrix=mat4.clone(this.camera.viewMatrix);
mat4.invert(this.initialModelMatrix,mMatrix);
this.initializePhysics();
this.counter=0;
this.visible=true;
}
In the upcoming initializePhysics function's code, we first use a simple
geometry, jigLib.JSphere(null, 10) , to present our grenade in the physics
world with a radius of 10 . Then, we set its mass to 10 . The important thing is that
the moment we add the sphere to the physics world, a force of 98 will be applied
on it from the -Y direction, because of gravity (9.8), with instantaneous velocity
of 0. We position our rigidBody variable in the world using the coordinate,
[-1.5,-2,-10] , with respect to the camera and convert it to world coordinates
by multiplying it by the initialModelMatrix ( jigLib.GLMatrix.multiplyVec3
(this.initialModelMatrix,[-1.5,-2,-10]); ). We then move our rigid body
to the calculated location ( this.rigidBody.moveTo(newPos[0], newPos[1],
newPos[2]); ).
Now, we want to apply a force or impulse. Before we do that, let's evaluate
our situation:
• We want the grenade to move along the +Y and -Z directions, so the force
has to be greater than the -Y force of gravity.
We want the grenade to move in the direction of the camera. The
initialModelMatrix has two components: orientation and position. We
want the magnitude and direction of the force to be affected only by the
orientation and not the position of the camera.
We want to apply the force on the center of the rigid body.
 
Search WWH ::




Custom Search