Blitz2D Intermediates: Particle Systems
by Giako

Overview:

Thanks to solidkojima (Anthony Wells) for making this great particle library, that helped me to understand how a particle system works.

This article assumes that you know something about physics (for example you know what is velocity and acceleration).

In this article we will focus on particle systems. We will learn how to achieve these effects in Blitz Basic and we will examine a little demo that uses a particle system to simulate a fountain.

What are particle systems?

Particle systems are nice graphics effects that simulate particles moving in the air. A particle system, in fact, can show to the user various effects, like smoke, sparkles and sprinklings of water.

We certainly know that the smoke is composed by billions of particles. In a computer particle system is used the same concept, but the number of particles are dramatically reduced (otherwise the FPS will be very very very few).

The particle:

A particle in a particle system can be either an image or a simple dot on the screen.

The parameters we need in order to control and show each particle are:

  • Its X position;
  • Its Y position;
  • Its X acceleration;
  • Its Y acceleration;
  • The gravitational acceleration (the amount of acceleration to the velocity in order to simulate the gravity);
  • Its type (the particle is a simple dot or is an image?);
  • Its image (if the particle is an image).
We can encapsulate this parameters in a single Blitz type, that we are going to call "Part":


Creating the library:

Now we are going to create a particle library so we can easily include it in any program.

After creating the Part type, we need several Globals to keep track of:

  • For...Each loops, so we need a Part type called P;
  • Maximum acceleration, otherwise the particles can move very very fast;
  • Screen Width (we use 800 as default, but we can change this parameter from our program);
  • Screen Height, so we can delete particles if they go out of the screen (we use 600 as default).
We also need to seed the number generator.

Here's the code:


The second step consist in creating five functions: the first must create a particle, assigning certain values at each field of the type; the second must calculate the position of each particle, applying the gravity acceleration; the third must draw the particles in different ways (if the particle is a dot the function plots a pixel, if it's an image draws its image); the fourth must allow the progammer to set the variables SW and Sh (they keep track of the screen resolution); the fifth must allow the progammer to set the maximum acceleration.

Here's the code:


A few notes on the second function: simply adding the gravity to the y velocity you achieve the falling of the particles. This cool effect is achieved in only one line of code.

An example program:

I made a little program that displays a fountain, to show you the power of the particle systems.

In order to make the dots in tones of blue I have modified the line that assign the color to the dots:


to:


In the code, I placed the source in the bottom center of the screen and I have assigned to each particle a negative velocity (in this way they fly a bit, then they fall because of the gravity). The X velocity is little and his range goes from left to right.

If you notice that the demo is slow on your computer, decrease the number of particles generated each loop. This number is stored in the NumParticles variable.

Download the source to see this stuff in action and examine it, in order to learn how it works.

Applying particle effects in games:

Now you can put particle effects in your game. For example you can use it for displaying some splarkles when you hit a wall with your gun in your top-down shooter.

In games (not demos) you must use a few particles in particle systems, otherwise the user needs a supercomputer in order to play your game!

Feel free to e-mail me if you have any questions or if you spot grammar mistakes.

A "ciao" from Italy,

-Giako


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.