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

#### The Particle Engine Software

We need functions to initialize all the particles, start a particle, process all the particles, and then clean up all the particles when we’re done. Let’s start with the initialization functions:

    void Init_Reset_Particles(void)

    {

    // this function serves as both an init and reset for the particles

    // loop thru and reset all the particles to dead

    for (int index=0; index<MAX_PARTICLES; index++)

        {

        particles[index].state = PARTICLE_STATE_DEAD;

        particles[index].type  = PARTICLE_TYPE_FADE;

        particles[index].x     = 0;

        particles[index].y     = 0;

        particles[index].xv    = 0;

        particles[index].yv    = 0;

        particles[index].start_color = 0;

        particles[index].end_color   = 0;

        particles[index].curr_color  = 0;

        particles[index].counter     = 0;

        particles[index].max_count   = 0;

        } // end if

    } // end Init_Reset_Particles

`Init_Reset_Particles()` just makes all particles zeros and gets them ready for use. If you wanted to do anything special, this would be the place to do it. The next function we need is something to start a particle with a given set of initial conditions. We will worry how to arrive at the initial conditions in a moment, but for now I want to hunt for an available particle, and if found, start it up with the sent data. Here’s the function to do that:

    void Start_Particle(int type, int color, int count,

                       float x, float y, float xv, float yv)

    {

    // this function starts a single particle

    int pindex = -1; // index of particle

    // first find open particle

    for (int index=0; index < MAX_PARTICLES; index++)

        if (particles[index].state == PARTICLE_STATE_DEAD)

           {

           // set index

           pindex = index;

           break;

           } // end if

    // did we find one

    if (pindex==-1)

       return;

    // set general state info

    particles[pindex].state = PARTICLE_STATE_ALIVE;

    particles[pindex].type  = type;

    particles[pindex].x     = x;

    particles[pindex].y     = y;

    particles[pindex].xv    = xv;

    particles[pindex].yv    = yv;

    particles[pindex].counter     = 0;

    particles[pindex].max_count   = count;

    // set color ranges, always the same

       switch(color)

             {

             case PARTICLE_COLOR_RED:

                  {

                  particles[pindex].start_color = COLOR_RED_START;

                  particles[pindex].end_color   = COLOR_RED_END;

                  } break;

             case PARTICLE_COLOR_GREEN:

                  {

                  particles[pindex].start_color = COLOR_GREEN_START;

                  particles[pindex].end_color   = COLOR_GREEN_END;

                  } break;

             case PARTICLE_COLOR_BLUE:

                  {

                  particles[pindex].start_color = COLOR_BLUE_START;

                  particles[pindex].end_color   = COLOR_BLUE_END;

                  } break;

             case PARTICLE_COLOR_WHITE:

                  {

                  particles[pindex].start_color = COLOR_WHITE_START;

                  particles[pindex].end_color   = COLOR_WHITE_END;

                  } break;

             break;

             } // end switch

    // what type of particle is being requested

    if (type == PARTICLE_TYPE_FLICKER)

       {

        // set current color

        particles[index].curr_color

        = RAND_RANGE(particles[index].start_color,

                     particles[index].end_color);

       } // end if

    else

       {

       // particle is fade type

       // set current color

       particles[index].curr_color  = particles[index].start_color;

       } // end if

    } // end Start_Particle

> Note: There is no error detection or even a success/failure sent back. The point is that I don’t care; if we can’t create one teenie-weenie particle, I think I’ll live. However, you might want to add more robust error handling.

To start a particle at (10,20) with an initial velocity of (0, -5) (straight up), a life span of 90 frames, colored a fading green, this is what you would do:

    Start_Particle(PARTICLE_TYPE_FADE,   // type

                   PARTICLE_COLOR_GREEN, // color

                   90,                   // count, lifespan

                   10,20,                // initial position

                   0,-5);                // initial velocity

Of course, the particle system has both gravity and wind that are always acting, so you can set them anytime you want and they will globally affect all particles already online as well as new ones. Thus if you want no wind force but a little gravity, you would do this:

    particle_gravity = 0.1; // positive is downward

    particle_wind    = 0.0; // could be +/-

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.