Game Development Reference
We've commented the code so that you can readily see what's going on. This function
essentially does four things: 1) increments the time variable by the specified time in‐
crement, 2) calculates the initial muzzle velocity components in the x-, y-, and z-
directions, 3) calculates the shell's new position, and 4) checks for a collision with the
target using a bounding box scheme or the ground.
Here's the code that computes the shell's position:
// Now we can calculate the position vector at this time
s.i = Vm * cosX * time + xe;
s.j = (Yb + L * cos(Alpha*3.14/180)) + (Vm * cosY * time) −
(0.5 * g * time * time);
s.k = Vm * cosZ * time + ze;
This code calculates the three components of the displacement vector, s , using the for‐
mulas that we gave you earlier. If you wanted to compute the velocity and acceleration
vectors as well, just to see their values, you should do so in this section of the program.
You can set up a couple of new global variables to represent the velocity and acceleration
vectors, just as we did with the displacement vector, and apply the velocity and accel‐
eration formulas that we gave you.
That's all there is to it. It's obvious by playing with this sample program that the shell's
trajectory is parabolic in shape, which is typical projectile motion . We'll take a more
detailed look at this sort of motion in Chapter 6 .
Even though we put a comment in the source code, we must reiterate a warning here
regarding the collision detection scheme that we used in this example. Because we're
checking only the current position coordinate to see if it falls within the bounding
dimensions of the target cube, we run the risk of skipping over the target if the change
in position is too large for a given time step. A better approach would be to keep track
of the shell's previous position and check to see if the line connecting the previous
position to the new one intersects the target cube.
Kinematic Particle Explosion
At this point you might be wondering how particle kinematics can help you create
realistic game content unless you're writing a game that involves shooting a gun or a
cannon. If so, let us offer you a few ideas and then show you an example. Say you're
writing a football simulation game. You can use particle kinematics to model the tra‐
jectory of the football after it's thrown or kicked. You can also treat the wide receivers
as particles when calculating whether or not they'll be able to catch the thrown ball. In
this scenario you'll have two particles—the receiver and the ball—traveling independ‐
ently, and you'll have to calculate when a collision occurs between these two particles,