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

Now we have to decide how to move and process the particle. Do we want to wrap them around the screen? Or, when they hit the edges, should we kill them? This depends on the type of game; 2D, 3D, scrolling, and so on. For now let’s keep it simple and agree that when a particle goes off a screen edge it’s terminated. In addition, the movement function should update the color animation, test if the life counter is expired, and kill particles that are off the screen. Here’s the movement function that takes into consideration all that, along with the gravity and wind forces:

    void Process_Particles(void)

    {

    // this function moves and animates all particles

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

        {

        // test if this particle is alive

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

           {

           // translate particle

           particles[index].x+=particles[index].xv;

           particles[index].y+=particles[index].yv;

           // update velocity based on gravity and wind

           particles[index].xv+=particle_wind;

           particles[index].yv+=particle_gravity;

           // now based on type of particle perform proper animation

           if (particles[index].type==PARTICLE_TYPE_FLICKER)

              {

              // simply choose a color in the color range and

              // assign it to the current color

              particles[index].curr_color =

                RAND_RANGE(particles[index].start_color,

                           particles[index].end_color);

              // now update counter

              if (++particles[index].counter >= particles[index].max_count)

                 {

                 // kill the particle

                 particles[index].state = PARTICLE_STATE_DEAD;

                 } // end if

              } // end if

          else

              {

              // must be a fade, be careful!

              // test if it’s time to update color

              if (++particles[index].counter >= particles[index].max_count)

                 {

                  // reset counter

                  particles[index].counter = 0;

                 // update color

                 if (++particles[index].curr_color >

                                  particles[index].end_color)

                    {

                    // transition is complete, terminate particle

                    particles[index].state = PARTICLE_STATE_DEAD;

                    } // end if

                 } // end if

              } // end else

           // test if the particle is off the screen?

           if (particles[index].x > screen_width ||

               particles[index].x < 0 ||

               particles[index].y > screen_height ||

               particles[index].y < 0)

              {

              // kill it!

              particles[index].state = PARTICLE_STATE_DEAD;

              } // end if

           } // end if

        } // end for index

    } // end Process_Particles

The function is self-explanatory—I hope. It translates the particle, applies the external forces, updates the counters and color, tests whether the particle has moved offscreen, and that’s it. Next we need to draw the particles. This can be accomplished in a number of ways, but I’m assuming simple pixels and a back buffered display, so here’s a function to do that:

    void Draw_Particles(void)

    {

    // this function draws all the particles

    // lock back surface

    DDraw_Lock_Back_Surface();

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

        {

        // test if particle is alive

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

           {

           // render the particle, perform world to screen transform

           int x = particles[index].x;

           int y = particles[index].y;

           // test for clip

           if (x >= screen_width || x < 0 || y >= screen_height || y < 0)

              continue;

           // draw the pixel

           Draw_Pixel(x,y,particles[index].curr_color,

                      back_buffer, back_lpitch);

          } // end if

        } // end for index

    // unlock the secondary surface

    DDraw_Unlock_Back_Surface();

    } // end Draw_Particles

Getting exited, huh? Want to try it out, don’t you? Well, we’re almost done. Now we need some functions to create particle effects like explosions and vapor trails.

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.