Defining, Updating, and Drawing a Particle System

Defining, Updating, and Drawing a Particle System

Top  Previous  Next

For this example of defining, updating, and drawing a particle system, I will assume that you are working within a class that inherits from Microsoft.XNA.Framework.Game.   This is not a requirement however, it is just for demonstrative purposes.  Also, this example assumes that at the top of your file you have included:

 

       using DPSF;

       using DPSF.ParticleSystems;

 

Otherwise each of the code segments provided below will need to be preceded by DPSF. or DPSF.ParticleSystems.

 

 

Defining a new particle system variable

 

Once a particle system class has been created, you can define a variable to hold it.  You will likely want to create this as a class variable so that it can be accessed across several of the class functions.  Assuming the particle system class that you created is called FireballParticleSystem, you can create a new variable to hold the particle system using:

 

       FireballParticleSystem mcFireballParticleSystem = null;

 

If you want to use the Particle System Manager to update and draw your particle systems, declare and initialize a Particle System Manager variable using:

 

       ParticleSystemManager mcParticleSystemManager = new ParticleSystemManager();        .

 

 

Defining a new particle system instance

 

Once you have the variable declared you will want to create a new instance of the particle system.  You may decide to do this within the Game classes overridden LoadContent() function, or somewhere else of your choosing.  A new instance of the particle system could be created using:

 

       mcFireballParticleSystem = new FireballParticleSystem(this);

 

If you are not inheriting from the Microsoft.XNA.Framework.Game class, you may pass null into the constructor instead of this.

 

At this point you may also want to specify the UpdateOrder and DrawOrder of the particle system from its default value of zero, but this is not a requirement.  The UpdateOrder and DrawOrder only have an effect when using the Particle System Manager to update and draw the particle systems respectively, or when DPSF is set to inherit from DrawableGameComponent.  Otherwise the particle system will be updated and drawn where ever it is placed within the application's Update() and Draw() functions.  The UpdateOrder and DrawOrder of the particle system are one of the few variables that do not get reset when the particle system is initialized or destroyed.  You may set the UpdateOrder and DrawOrder by using:

 

       mcFireballParticleSystem.UpdateOrder = 100;

       mcFireballParticleSystem.DrawOrder = 100;

 

If you want to use the Particle System Manager to manage the particle system, after creating a new particle system instance, add the particle system to the particle system manager using:

 

       mcParticleSystemManager.AddParticleSystem(mcFireballParticleSystem);

 

 

Initializing the particle system

 

Once a new instance of the particle system has been created you may initialize the particle system using:

 

       mcFireballParticleSystem.AutoInitialize(this.GraphicsDevice, this.Content);

 

Where this.GraphicsDevice is the Graphics Device that the particle system should draw to, and this.Content is the Content Manager that the particle system should use to load texture and effect (i.e. shader) files.

 

If you are using the Particle System Manager and want to initialize all of the particle systems that have been added to it, instead of using the above code to initialize just the one particle system, you can initialize all particle systems that are in the particle system manager using:

 

       mcParticleSystemManager.AutoInitializeAllParticleSystems(this.GraphicsDevice, this.Content);

 

However, if you only want the particle systems to be initialized once they are needed, it is better to just initialize the ones that you need individually since when the particle system manager's update and draw functions are called it will update and draw all particle system's that are initialized.

 

 

Updating the particle system

 

Once the particle system is initialized you may update the particle system.  If you try to update or draw the particle system before it is initialized the functions will simply exit without doing anything; they will not throw any sort of exception or error.  Assuming you want to update the particle system at the same pace as the rest of the game, you will likely want to add the following code to the Game classes overridden Update() function:

 

       mcFireballParticleSystem.Update((float)cGameTime.ElapsedGameTime.TotalSeconds);        

 

If you are using the Particle System Manager you can update all of the particle systems that are initialized using:

 

       mcParticleSystemManager.UpdateAllParticleSystems((float)cGameTime.ElapsedGameTime.TotalSeconds);

 

If DPSF is set to inherit from DrawableGameComponent, these function calls will not actually update the particle system.  Instead, the particle system will be updated when the base.Update() function is called, as this is when all of the Game Components get updated.  However, having these function calls remain here will not cause any problems and will allow the particle systems to still be updated if you set DPSF to not inherit from DrawableGameComponent, but you may remove them if you like.

 

 

Drawing the particle system

 

To draw the particle system, place the following code in the Game classes overridden Draw() function:

 

       mcFireballParticleSystem.SetWorldViewProjectionMatrices(mcWorldMatrix, mcViewMatrix, mcProjectionMatrix);

       mcFireballParticleSystem.Draw();

 

Where mcWorldMatrix, mcViewMatrix, and mcProjectionMatrix are the World, View, and Projection matrices respectively.  You must set the World, View, and Projection matrices before drawing to ensure that the particle systems are drawn properly according to the camera's properties.  Since Sprite Particle Systems are drawn in 2D screen coordinates, they will not be affected by the World, View, and Projection matrices, but it does not hurt to set them anyways if they will be mixed in with 3D particle systems.  To set the transformation matrix for a Sprite particle system, you can set the mcFireballParticleSystem.SpriteBatchSettings.TransformationMatrix property.

 

If you are using the Particle System Manager and want to draw all of the initialized particle systems, use the following code:

 

       mcParticleSystemManager.SetWorldViewProjectionMatricesForAllParticleSystems(mcWorldMatrix, mcViewMatrix, mcProjectionMatrix);

       mcParticleSystemManager.DrawAllParticleSystems();

 

If DPSF is set to inherit from DrawableGameComponent, these function calls will not actually draw the particle system.  The particle system will be updated when the base.Draw() function is called, as this is when all of the Game Components get drawn.  However, having these function calls remain here will not cause any problems and will allow the particle systems to still be drawn if you set DPSF to not inherit from DrawableGameComponent, but you may remove them if you like.

 

 

Destroying the particle system

 

When you are done with the particle system it may be destroyed using:

 

       mcFireballParticleSystem.Destroy();

 

It is a good idea to destroy particle systems when they are not in use and you know they won't be used again for a while, as this will free all resources used by the particle system.  This can free up a lot of memory, especially if the particle system has memory allocated for a large number of particles.  When you are ready to use the particle system again, just re-initialize it.  If the particle system is used frequently however you may want to just leave it initialized, as initializing and destroying particle systems allocates and de-allocates memory, which is expensive to do at run-time, especially if many (thousands or tens of thousands) of particles are allocated by the particle system.

 

If you are using the Particle System Manager and want to destroy all of its particle systems, this can be done using:

 

       mcParticleSystemManager.DestroyAllParticleSystems();

 

If you are completely done with the particle system all together and want to totally remove it you must set the variable to null, like so:

 

       mcFireballParticleSystem = null;

 

However, if you had also added it to the particle system manager, before setting the variable to null you must also remove it from the particle system manager.  You can do this using:

 

       mcParticleSystemManager.RemoveParticleSystem(mcFireballParticleSystem);

 

 

For more examples of defining, updating, and drawing particle systems take a look at the GameMain.cs file included in the DPSF Demo source code.