Game Development Reference
In-Depth Information
Explosion.p[i].life = tb_Round(life * f);
Explosion.p[i].r = 255;//tb_Rnd(225, 255);
Explosion.p[i].g = 255;//tb_Rnd(85, 115);
Explosion.p[i].b = 255;//tb_Rnd(15, 45);
Explosion.p[i].time = 0;
Explosion.p[i].Active = TRUE;
Explosion.p[i].gravity = gravity;
}
}
As you can see, the initial velocity V0 controls the strength of the explosion. In Chap‐
ter 2 , we chose this value randomly. Now that we have a bullet flying through the air,
we can make a better estimate of how strong of an explosion to create. As you recall
from earlier in the chapter, a bullet has an energy associated with it at any time, t , in its
flight. This energy is its kinetic energy and is equal to half the bullet mass times its
velocity squared.
In our projectile simulation, it is simple to calculate this energy as the bullet flies through
the air. It should be noted that a big bullet moving slowly is just as powerful as a smaller
bullet moving quickly. Our upcoming code is going to assume that 100% of the kinetic
energy is delivered to the target. This would not be true if a bullet shot straight through
something. A way to visualize this is to imagine two targets, both hanging from the
ceiling. One is made from paper and one is made from steel. When shot at, the steel
target swings from its support, while the paper target stays still. This is because the bullet
is passing straight though the paper and not transferring its kinetic energy to the target.
To make things simple, we'll transfer all of the bullet's kinetic energy to the gravel. In
equation form, this would look like:
1/2 m b v 2 bullet = Σ1/2 m g v 2 gravel
Note that this is the sum of the individual bits of gravel's velocities. In, Chapter 2 , each
particle was just given a random velocity anywhere from Vinit/2 to Vinit . This could
lead to creating a set of particles whose energies exceed the input energy. To prevent
this, we'll add a variable to our explosion class like so:
typedef struct _TParticle
{
float x; // x-coordinate of the particle
float y; // y-coordinate of the particle
float vi; // initial velocity
float angle; // initial trajectory (direction)
int life; // duration in milliseconds
int r; // red component of particle's color
int g; // green component of particle's color
int b; // blue component of particle's color
int time; // keeps track of the effect's time
float gravity; // gravity factor