Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Generating the Initial Conditions

Here’s the fun part. You can go wild with your imagination. Let’s start off with a vapor trail algorithm. Basically, a vapor trail is nothing more than particles that are emitted from a source positioned at (`emit_x`, `emit_y`) with slightly different life spans and starting positions. Here’s a possible algorithm:

    // emit a particle every with a change of 1 in 10

    if ((rand()%10) == 1)

    {

    Start_Particle(PARTICLE_TYPE_FADE,   // type

                   PARTICLE_COLOR_GREEN, // color

                   RAND_RANGE(90,150),   // count, lifespan

                   emit_x+RAND_RANGE(-4,4),  // initial x

                   emit_y+RAND_RANGE(-4,4),  // initial y

                   RAND_RANGE(-2,2),   // initial x velocity

                   RAND_RANGE(-2,2));  // initial y velocity

    } // end if

As the emitter moves, so does the emitter source (`emit_x`, `emit_y`) and therefore a vapor trail is left. If you want to get really real and give the vapor particles an even more realistic physics model you should take into consideration that the emitter could be in motion and thus any particle emitted would have final velocity = emitted velocity + emitter velocity. You would need to know the velocity of the emitter source, (call it (`emit_xv`, `emit_yv`)) and simply add it to the final particle velocity like this:

    // emit a particle every with a change of 1 in 10

    if ((rand()%10) == 1)

    {

    Start_Particle(PARTICLE_TYPE_FADE,   // type

                   PARTICLE_COLOR_GREEN, // color

                   RAND_RANGE(90,150),   // count, lifespan

                   emit_x+RAND_RANGE(-4,4),  // initial x

                   emit_y+RAND_RANGE(-4,4),  // initial y

                   emit_xv+RAND_RANGE(-2,2),   // initial x velocity

                   emit_yv+RAND_RANGE(-2,2));  // initial y velocity

    } // end if

For something a little more exciting, let’s model an explosion. An explosion looks something like Figure 13.43. Particles are emitted in a spherical shape in all directions.

Figure 13.43 The particles of an explosion.

That’s easy enough to model. All we need do is start up a random number of particles from a common point with random velocities that are equally distributed in a circular radius. Then if gravity is on, the particle will fall toward Earth and either go off the screen or die out due to its individual life span. Here’s the code to create a particle explosion:

    void Start_Particle_Explosion(int type, int color, int count,

                                  int x, int y, int xv, int yv,

                                  int num_particles)

    {

    // this function starts a particle explosion

    // at the given position and velocity

    // note the use of look up tables for sin,cos

    while(--num_particles >=0)

        {

        // compute random trajectory angle

        int ang = rand()%360;

        // compute random trajectory velocity

        float vel = 2+rand()%4;

        Start_Particle(type,color,count,

                       x+RAND_RANGE(-4,4),y+RAND_RANGE(-4,4),

                       xv+cos_look[ang]*vel, yv+sin_look[ang]*vel);

        } // end while

    } // end Start_Particle_Explosion

`Start_Particle_Explosion()` takes the type of particle you want (`PARTICLE_TYPE_FADE`, `PARTICLE_TYPE_FLICKER`), the color of the particles, the desired number of particles, along with the position and velocity of the source. The function then generates all the desired particles.

To create other special effects, just write a function. For example, one of the coolest effects that I like in movies is the ring-shaped shock wave when a spaceship is blown away. Creating this is simple. All you need to do is modify the explosion function to start all the particles out with exactly the same velocity, but at different angles. Here’s that code:

    void Start_Particle_Ring(int type, int color, int count,

                                  int x, int y, int xv, int yv,

                                  int num_particles)

    {

    // this function starts a particle explosion at the

    // given position and velocity

    // note the use of look up tables for sin,cos

    // compute random velocity on outside of loop

    float vel = 2+rand()%4;

    while(--num_particles >=0)

        {

        // compute random trajectory angle

        int ang = rand()%360;

        //start the particle

        Start_Particle(type,color,count,

                       x,y,

                       xv+cos_look[ang]*vel,

                       yv+sin_look[ang]*vel);

        } // end while

    } // end Start_Particle_Ring

#### Putting the Particle System Together

You now have everything you need to put together some cool particle effects. Just make a call in the initialize phase of your game to `Init_Reset_Particles()`, then in the main loop make a call to `Process_Particles()`. Each cycle and the engine will do the rest. Of course, you have to call one of the generator functions to create some particles! Lastly, if you want to improve the system you might want to add better memory management so you can have infinite particles, and you might want to add particle-to-particle collision detection and particle-to-environment collision detection—that would be really cool.

As a demo of using the particle system, take a look at `DEMO13_10.CPP|EXE` on the CD. It is a fireworks display based on the tank projectile demo. Basically, the tank from the previous demo fires projectiles now. Also, note in the demo I jacked the number of particles up to 256.

### Playing God: Constructing Physics Models for Games

This chapter has given you a lot of information and concepts to sift through. The key is to use the concepts and some of the hard math to make working models that look good. No one will ever know if they accurately simulate reality 100 percent, nor will they care. If you can make an approximation then do it—as long as it’s worth it. For example, if you’re trying to make a racing game and you want to race on road, ice, and dirt, then you better have some frictional effects, otherwise, your cars will drive like they’re on rails!

On the other hand, if you have an asteroid field that the player blows up and each asteroid splits into two or more smaller asteroids then I don’t think the player is going to care or know for that matter the exact trajectory that the smaller asteroids would take—just pick them in a deterministic way so they look good.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.