Andorra 2D tutorials
Particle systems
What are particle systems?
You want to use graphical effects such as explosions or magic spells many times in your game.
To do this, so-called "particle systems" are often used. As the name suggests, particle systems consist of single "particles", which in turn are mostly rather unspectacularly looking images that move in a certain direction and disappear again after some time:
http://andorra.sourceforge.net/tutots/particle.png
If you have several of these particles, you get this effect:
http://andorra.sourceforge.net/tutots/part1.png
The particle system manages the particles and their movements.
Particle systems Andorra 2D
Loading particles
In Andorra 2D, there is the class "TAdParticleSystem" in the
unit "AdParticles".
The class expects a parameter of the type "TAdDraw" in its constructor specifying the surface which the output should be displayed on.
To create particles, you have to assign the particle's image first.
This is done by the following code:
Delphi-Quellcode:
//Creating the particle system
partsys := TAdParticleSystem.Create(AdDraw);
//Assigning the image
partsys.Texture := AdImageList.Items[0].Texture;
To achieve the best effect, make sure the particle has an alpha channel. Use
this image, for example. (Note that in order to load PNG images you have to include the AdPNG
unit and the library
PNGDelphi has to be in the search path, if needed).
Now we're already done. Add the following line to your rendering routine:
partsys.Emit(1, 0, 0);
This line adds a new particle to the particle system at the position 0,0. The particle system has its own coordinate system in which the particles move. We create our new particle at the origin of ordinates.
Now we have to tell the particle systems to move its particles and draw them. This is done by the following lines:
Delphi-Quellcode:
PartSys.Move(AdPerCounter.TimeGap / 1000);
partsys.Draw(bmAdd, AdDraw.DisplayRect.Right / 2, AdDraw.DisplayRect.Bottom / 2);
The coordinate system's point of origin is temporarily moved to the centre point of the screen, so that new particles also appear there.
Do not forget to also free the particle system in the end and execute the programme:
http://andorra.sourceforge.net/tutots/part3.png
If you want to have the source code, take a look at the demo "demos\Particles\Simple\simplepart". Or take a look at this:
http://andorra.cvs.sourceforge.net/a...iew=markup#l_1
The behaviour of particles
As you have seen, the particles have a default behaviour. But where does it come from?
To find that out, we take a look at how the "emit" function of the particle system works in detail:
http://andorra.sourceforge.net/tutots/creation.png
In the beginning, the specified amount of particles is created, which inherit their properties from the particle system's "default particle". Then the fuction "SetupMovement" of each particle is called, in which a movement vector is generated.
To change the overall appearance of the particle system, you only have to adjust the properties of the default particle. To adjust the life time of the particles, for example, write the following:
PartSys.DefaultParticle.LifeTime := 2; //Specified in seconds
You can adjust the particles' properties with the comfortable particle editor (parted.exe). Just play around with it a bit. You can also write the particles into a file with it and load them again with:
PartSys.DefaultParticle.LoadFromFile('particle.apf');
Note that the image you have loaded into the particle editor is not saved into the file.
That was virtually everything there is to say about particles.
Custom particle classes
If you want to create particles with a chaotic movement pattern, you can derive your own particle class, which you can pass on to the particle system by assigning the default particle. Take a look at the source code of TAdStdParticle or TAdBillboardParticle respectively. It's the easiest way to derive your own particle class from those.
Particles in the sprite engine
The class "TParticleSprite" exists to use particle systems in the sprite engine. It has its own particle system. You can also tell it how many particles to create per second. This, however, is a chapter on its own and will be covered separately in the sprite engine tutorials.
Copyright and licence
(c) by Andreas Stöckel February 2007
Translation by 3_of_8 (Manuel Eberl)
The content of this tutorial is subject to the
GNU Licence for Free Documentation.