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

### Particle Systems

This is the hot topic. Everyone is always saying, “So, does it have particle systems?” Well, particle systems can be very complex or very simple. Basically, particle systems are physics models that model small particles. They are great for explosions, vapor trails, and general light shows in your game. You have already learned a lot about physics modeling and I’m sure can create your own particle system. However, just to get you started, I’m going to show you how to create a very quick and simple system based on pixel-sized particles.

Let’s say that we want to use particles for explosions, and maybe vapor trails. Since a particle system is nothing more than n particles, let’s just focus on the model of a single particle.

#### What Every Particle Needs

If you wanted, you could model collision response, momentum transfer, and all that stuff, but most particle systems have extremely simple models. The following are the general features of a garden variety particle:

  • Position
  • Velocity
  • Color/animation
  • Life span
  • Gravity
  • Wind force

When you start a particle, you will want to give it a position, initial velocity, color, and a life span at the very least. Also, the particle might be a glowing cinder, so there might be color animation involved. Additionally, you may want to have some global forces that act on all particles, like gravity and wind. You may want to have functions that create collections of particles with the desired initial conditions that you’re looking for, like explosions or vapor trails. And of course, you may want to give particles the ability to bounce off objects with some physical realism. However, most of the time particles just tunnel through everything and no one cares!

#### Designing a Particle Engine

To design a particle system you need three separate elements:

  • The particle data structure.
  • The particle engine that processes each particle.
  • Functions to generate particular particle initial conditions.

Let’s begin with the data structure. I’ll assume an 8-bit display since, in animation, it’s easier to work with bytes than RGB colors. Anyway, here’s a first attempt at a single particle:

    // a single particle

    typedef struct PARTICLE_TYP

            {

            int state;           // state of the particle

            int type;            // type of particle effect

            float x,y;           // world position of particle

            float xv,yv;         // velocity of particle

            int curr_color;      // the current rendering color of particle

            int start_color;     // the start color or range effect

            int end_color;       // the ending color of range effect

            int counter;         // general state transition timer

            int max_count;       // max value for counter

            } PARTICLE, *PARTICLE_PTR;

Let’s add in some globals to handle external effects such as gravity in the Y direction and wind force in the X direction.

    float particle_wind = 0;   // assume it operates in the X direction

    float particle_gravity = 0; // assume it operates in the Y direction

Let’s define some useful constants that we might need to accomplish some of the effects:

    // defines for particle system

    #define PARTICLE_STATE_DEAD               0

    #define PARTICLE_STATE_ALIVE              1

    // types of particles

    #define PARTICLE_TYPE_FLICKER             0

    #define PARTICLE_TYPE_FADE                1

    // color of particle

    #define PARTICLE_COLOR_RED                0

    #define PARTICLE_COLOR_GREEN              1

    #define PARTICLE_COLOR_BLUE               2

    #define PARTICLE_COLOR_WHITE              3

    #define MAX_PARTICLES                     128

    // color ranges (based on my palette)

    #define COLOR_RED_START                   32

    #define COLOR_RED_END                     47

    #define COLOR_GREEN_START                 96

    #define COLOR_GREEN_END                   111

    #define COLOR_BLUE_START                  144

    #define COLOR_BLUE_END                    159

    #define COLOR_WHITE_START                 16

    #define COLOR_WHITE_END                   31

Hopefully, you can see my thinking here. I want to have particles that are either red, green, blue, or white, so I took a palette and figured out the color indices for the ranges. If you wanted to use 16-bit color then you would have to manually interpolate RGB from the starting value to some ending value—I’ll keep it simple. Also, you see that I’m counting on making two types of particles: fading and flickering. The fading particles will just fade away, but the flickering ones will flicker away, like sparks.

Finally, I’m happy with our little particles, so let’s create storage for them:

    PARTICLE particles[MAX_PARTICLES]; // the particles for the particle

    engine

So let’s start writing the functions to process each particle.

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.