Creating a New Particle System Class

Top  Previous  Next

In addition to inheriting from the DPSF class to obtain its functionality, particle systems must also specify the Particle class and Particle Vertex structure they will use through the use of generics.  These will be the particle class and particle vertex structure you defined earlier in this section.

 

Initializing the particle system

 

Step 0 - You should have already renamed / refactored the particle system classes name when you imported the template into your project.  If you have not done this yet, do it now.

 

Step 1 - First locate the AutoInitialize() function.  The first thing this function does is call the particle initialization function with some default values.  You may change these parameter values if you like.

 

Step 2 - Locate the virtual UpdateVertexProperties() function.  Modify the function's code to copy all of the drawable particle properties from a particle class object into a  particle vertex object which is stored in the vertex buffer.  The template shows how to do this using the default properties it was created with.  This is a bit more complex when using Quad particles, since a Quad has four vertices and the particle's properties need to be copied correctly into all four vertices in the vertex buffer.  Also, Quad particles make use of the Index Buffer in order to save memory.  Again, the templates show how this should be done.

 

Step 3 - Locate the overridden SetEffectParameters() function.  This function is used to set any of the global effect (i.e. shader) parameters.  The templates show how to set the effect parameters used with the DPSF Default Effect, but you may modify these if you wish or if you plan on using your own custom effect file.  This function will be called before the particle system is drawn to ensure that the parameter values are correct before rendering.

 

Step 4 - Locate the overridden InitializeRenderProperties() function.  This function can be used to change any of the RenderState properties before drawing the particle system, such as using alpha blending or enabling depth buffer writes.

 

 

Defining the particle system behavior

 

Step 1 - Locate the InitializeParticleProperties() function.  This will be the particle initialization function that is used to initialize a new particle's properties when it is added to the particle particle system.  Use this function to set all of the particle properties to their starting values.  You may create several particle initialization functions if you wish, but they all must follow the InitializeParticleDelegate prototype, which is:

 

       public void FunctionName(Particle cParticle); where Particle is the particle class that the particle system is using for its particles.

 

The naming convention DPSF uses for naming particle initialization functions to make them easy to recognize is that the function name starts with InitializeParticle.

 

Step 2 - Locate the UpdateParticlePositionUsingVelocity() function.  This function is an example of a Particle Event function, and it shows you how to update a particle's properties.  You will likely want to create several particle event functions, and they all must follow the UpdateParticleDelegate prototype, which is:

 

       public FunctionName(Particle cParticle, float fElapsedTimeInSeconds); where Particle is the particle class that the particle system is using for its particles.

 

The naming convention DPSF uses for naming particle event functions to make them easy to recognize is that the function name starts with UpdateParticle.  If you like you may create several smaller particle event functions and put them together later in the LoadParticleSystem() function, or create fewer larger particle event functions; it is personal preference.  For example, if you wanted to update a particle's position based on its acceleration and velocity, you could create two separate functions:

 

      public void UpdateParticlePositionUsingVelocity(Particle cParticle, float fElapsedTimeInSeconds)

       {

          // Update the Particle's Position according to its Velocity

           cParticle.Position += cParticle.Velocity * fElapsedTimeInSeconds;

       }

 

      public void UpdateParticleVelocityUsingAcceleration(Particle cParticle, float fElapsedTimeInSeconds)

       {

          // Update the Particle's Position according to its Velocity

           cParticle.Velocity += cParticle.Acceleration * fElapsedTimeInSeconds;

       }

 

Or you could create one larger function:

 

      public void UpdateParticlePositionAndVelocityUsingAcceleration(Particle cParticle, float fElapsedTimeInSeconds)

       {

          // Update the Particle Velocity and Position according to Acceleration

           cParticle.Velocity += cParticle.Acceleration * fElapsedTimeInSeconds;

           cParticle.Position += cParticle.Velocity * fElapsedTimeInSeconds;

       }

 

DPSF uses the first method of creating several smaller functions for the Default particle systems.  This method is used because tests show that there is virtually no performance difference between the two methods, even though intuition may suggest that since there are more functions calls the performance of the first method would be worse.  Also, by using the first method you can include only the operations you want performed when defining the events in the LoadParticleSystem() function.  This could actually increase performance if the method of creating fewer large particle event functions was performing unnecessary operations.  In our example given, this could be the case where we weren't actually using acceleration, only velocity, in which case the acceleration operations would still be performed by using the larger function, but could simply not be included in the particle event functions when using the smaller functions (see Step 6).

 

Step 3 - Locate the UpdateParticleSystemTurnEmitterOn() function.  This function is an example of a Particle System Event function, and it shows you how to update  the particle system's properties.  All particle system event functions you create must follow the UpdateParticleSystemDelegate prototype, which is:

 

       public FunctionName(float fElapsedTimeInSeconds);

 

The naming convention DPSF uses for naming particle system event functions to make them easy to recognize is that the function name starts with UpdateParticleSystem.

 

Step 4 - Locate the LoadParticleSystem() function; This is where you will define how the particle system should behave.  If you like you may rename / refactor this function, or create several functions to load the particle system with different behaviors.  Steps 5 - 7 take place within the LoadParticleSystem() function.

 

Step 5 - Specify the ParticleInitializationFunction.  The Particle Initialization Function is the function that will be used to initialize new particles when they are added to the particle system.  This should be one of the particle initialization functions you created / modified in step 1.  If you do not specify a Particle Initialization Function, when new particles are created their properties will have their default values, which should have been specified in the Reset() function of the particle class.

 

Step 6 - First remove all ParticleEvents and ParticleSystemEvents so that if the function is called again later to reload the particle system, the events will not be added twice.  This is done by calling the ParticleEvents.RemoveAllEvents() and ParticleSystemEvents.RemoveAllEvents() functions.  Next, add the the Particle and Particle System Events that you want the particle system to use.  The particle events created in step 2 should be added to the ParticleEvents object, and the particle system events created in step 3 should be added to the ParticleSystemEvents object.  The templates show examples of how to do this to achieve specific effects.

 

Step 7 - Setup the emitter if it you want to use it, such as specifying its position, orientation, and how many particles it should emit per second.

 

 

To see some particle system examples, check out the DPSF Demo source code to see how the particle systems used in the demo were created.