Click or drag to resize
DigitalRuneParticle Systems, Parameters and Effectors

This section discusses particle systems, particle parameters and particle effectors.

Note: Have a look at the Class Diagrams for DigitalRune.Particles to get an overview of the provided types. Check out the section How To: Create a Simple Particle Effect and the included Samples to see how particle systems are used in code.

This topic contains the following sections:

The particle system service

The ParticleSystemManager implements the interface IParticleSystemService. A particle system service manages a collection of particle systems. Usually ParticleSystemManagerUpdate(TimeSpan) is called in the game loop once per frame. In this method all particle systems are updated.

The use of the ParticleSystemManager is optional. Individual particle systems can also be updated manually.

Particle systems

The class ParticleSystem provides the base class for a particle system. It acts like a configurable container for particle parameters, particle effectors and other particle systems. To create a new particle effect a new type can be derived from ParticleSystem, but usually the class is used directly.

Each particle system can have child particle systems (see property Children) to create complex effects. For example, an explosion particle system can consist of a glow particle system, a smoke particle system and a debris particle system.

The particle system updates its simulation state when the method Update is called. This method is called automatically if the particle system is added to a particle system service or a another particle system. Reset can be called to remove all particles and set a particle system back to its initial state.

A particle system starts without any particles. To add ("emit") new particles the method AddParticles needs to be called. There are special particle effectors (called "emitters" such as the StreamEmitter) that automatically call this method.

Geometric object

The particle system implements IGeometricObject, which means that it has a Pose (= position and orientation) and a Shape. The shape can be used as a bounding volume for view frustum culling. A suitable shape must be set manually (the default shape is an infinitely large shape).

Reference frame

Normally, all 3D positions and directions of particle parameters are given in world space. The particle system has a property ReferenceFrame where the coordinate space for particle parameters can be changed. If the reference frame is changed to Local, all 3D positions and directions are relative to the local space of the particle system. That means, when the particle system moves (by changing ParticleSystemPose), all particles move and rotate with the particle system as if they were attached to it.

Timing

The simulation time (see ParticleSystemTime) of the particle system can be scaled using ParticleSystemTimeScaling to achieve slow motion or high speed effects.

Additionally, a delay can be defined (see ParticleSystemInitialDelay and ParticleSystemCurrentDelay). This can be used to time effects (e.g. for staggered explosions).

Some particle systems should not start without any particles. Instead, they should start "in the middle" of the effect. For example: When a player encounters a waterfall, the water should already be flowing. The properties ParticleSystemPreloadDuration and ParticleSystemPreloadDeltaTime can be used to define a pre-load phase, which is executed when the particle system is updated for the first time. In this phase particles can be created and simulated to ensure that the particle system is not empty when it is shown.

Particle parameters

Each particle system owns a ParticleParameterCollection which manages particle parameters. Particle parameters have a name and define properties of the particle system or of individual particles. Typical particle parameters are Position, Speed, Direction, Size, Color, Alpha, etc.

A particle parameter is called "uniform" if it provides a single value that is shared by the particle system and all particles. For example, most particle systems will have a uniform parameter "Texture" that defines which texture a particle renderer should use for the particles. All particles use the same texture.

A particle parameter is called "varying" if each particle has its own value. For example, most particle systems will have a varying parameter "Position" that stores the positions of the individual particles. (Each particle can have a unique position.)

New particle parameters are added to a particle system using the methods ParticleParameterCollectionAddUniformT(String) and ParticleParameterCollectionAddVaryingT(String). Existing parameters can be retrieved using the Get methods (for example ParticleParameterCollectionGetT(String)).

Particle parameters implement the interface IParticleParameterT, which has a property DefaultValue and a property Values. Uniform particle parameters store their value in DefaultValue and Values is null. Varying particle parameters store the values in the Values array and DefaultValue is not used. The Values array is preallocated when the particle parameter is added to the particle system. The length of the array is defined by ParticleSystemMaxNumberOfParticles. It is used as a circular buffer for the current particle values. ParticleSystemParticleStartIndex determines the index of the oldest active particle in the array. ParticleSystemNumberOfActiveParticles defines how many array elements are currently in used. When old particles die, ParticleStartIndex is increased by the particle system. NumberOfActiveParticles is increased when new particles are added.

The class ParticleParameterNames provides names for commonly used particle parameters. These names are only suggestions. (The only parameter name which is fixed is "NormalizedAge" - see below.)

Default parameters

The particle system defines and manages only one particle parameter: "NormalizedAge" is a varying particle parameter (type: single-precision floating point). The parameter is 0 for new particles and increases linearly until the particle dies. Particles with a normalized age of 1 are removed.

By default, the lifetime of particles (= the time it takes for a particle to reach the normalized age of 1) is 3 seconds. A different lifetime can be set by adding a uniform or varying particle parameter called "Lifetime" to the particle system. The particle parameter needs to specify the particle lifetime in seconds (type: single-precision floating point). If "Lifetime" is a uniform parameter then all particles are simulated for the same time span. If "Lifetime" is a varying parameter then the lifetimes can be set per particle.

The particle system itself does not read or manipulate any other parameters. All particle parameters (except "NormalizedAge") must be added by the user. The designer of the particle system can decide which particle parameters are needed, which parameters should be uniform or varying, and which types the parameters should use!

Inheriting uniform parameters

Uniform particle parameters can be inherited from parent particle systems (but not varying parameters). That means, a parent particle system can define a uniform parameter "Alpha" and if a child particle system does not define an "Alpha" parameter itself, it will use the value of the parent. (The Get methods of the ParticleParameterCollection first check whether a uniform parameter is defined locally. If it is missing they check the parent particle system.)

Inherited parameters are useful to control multiple particle systems at once. For example, the "teleport effect" (included in the samples) owns several child particle systems (one particle systems for glowing points and other particle systems for energy beams). The parent particle system defines a parameter "GlobalAlpha". The child particle systems multiply the "GlobalAlpha" with their local "Alpha" value. Thus the whole particle effect can be faded in/out by changing a single parameter.

Particle effectors

Each particle system owns a ParticleEffectorCollection that manages objects derived from the base class ParticleEffector. In each update the particle system calls the particle effectors to do the actual work. Particle effectors can be used to create, initialize, and manipulate particles. Effectors define the behavior of a particle system.

DigitalRune Particles provides several predefined effectors: See namespace DigitalRune.Particles.Effectors and included samples.

The base class ParticleEffector provides several virtual methods, which can be overridden in derived class. See the class description of ParticleEffector for more information.

Common particle effector types

Most particle effectors modify particle parameters. Thus, they define the behavior of the particles. Effectors should be designed to work with uniform and varying particle parameters. Usually, particle effectors override OnBeginUpdate or OnEndUpdate to modify uniform particle parameters, and OnUpdateParticles to handle varying particle parameters. The section How To: Create a Particle Effector shows how to implement a particle effector.

A particle effector that creates particles (by calling ParticleSystemAddParticles(Int32)) is called an emitter. For example, see class StreamEmitter. Usually, emitters override the method OnBeginUpdate to add new particles. The section How To: Create a Particle Emitter shows how to implement an emitter.

Particle effectors that set the initial values of varying parameters are called start value effectors. For example, see classes StartValueEffectorT, StartPositionEffector and StartDirectionEffector. Start value effectors usually override the methods OnInitialize to initialize uniform parameters and OnInitializeParticles to initialize varying parameters. The section How To: Create a Start Value Effector shows how to implement a generic start value effector.

Distributions

Many start value effectors use DistributionT objects to define the start values of particle parameters. A DistributionT usually returns random values that follow a certain probability distribution. For example, the StartPositionEffector can use a SphereDistribution to return random start positions uniformly distributed within a sphere volume. See Statistics and Distributions in DigitalRune Mathematics for more information.

Miscellaneous tips

Here are some tips for designing particles effects: