Tutorial 08 - Particle Fun

This tutorial is just about particles. Don't you know what they are? Particles are widely used in game because they can simulate complex effects with very simple effort. Have you seen fire or rain in a videogame? Explosions maybe? Most of them are made using particles.

Particles have two parts in this engine. The first one is the graphic part, which is inside the Graphic namespace. You can use ParticleSystemNode to generate particles, but need to assign a behaviour to each particle. We will do it later, first just create the particle system:

// Particle setup
SDE.Graphic.ParticleSystemNode ps gMan.CreateParticleSystem();
ps.Material gMan.Core.CreateMaterial("particle");
ps.Material.Textures[0gMan.Core.GetTexture("../../../Bin/Media/particle.tga");
ps.Material.Type SDE.Graphic.Material.Blending.TransparentAdd;
ps.SetSphereEmitter(0.2f);
cam.AddToScene(ps);

As you can see, I've removed the second camera, the rotation controller and the decal, to make things easier. Now let's have a little fun. I've also changed the default emitter (a point emitter), so now particles are created inside a sphere. You can comment this line and see the difference. We need a ParticleController to add behaviour to the particles. It's easy to create a ParticleController, similar to what we have seen in the previous tutorial:

SDE.Util.Controller.ParticleController pc;
pc SDE.Engine.Instance.CreateParticleController(ps);
SDE.Engine.Instance.AddController(pc);

But this Controller is a bit more complex than the others, because it can add different behaviours to the particle node. You can try this code:

pc.AddAffector(pc.CreateLinearMovementAffector(new Vector3(0,1,0),0.3f));
pc.AddAffector(pc.CreateTransparencyAffector(255,0));
pc.MaxParticlesPerSecond 12;
pc.MinParticlesPerSecond 8;
pc.MaxLife 1;

Looks much better in movement, I promise

You can comment some of those lines to isolate the behaviour you wan't to see. This 'behaviours' are called ParticleAffectors, and you can write your own in a similar way you created Controllers, just inherit IParticleAffector. There are some more affectors I didn't use, so feel free to experiment with them.