Tutorial 1 - Defining, Updating, and Drawing a Particle System

Top  Previous  Next

In this tutorial we will create, update, draw, and destroy a particle system created from the default textured quad particle system template provided by DPSF.

 

Load the Tutorial 1 project and open up Game1.cs.

 

 

Setup

 

Once you have referenced DPSF.dll in your XNA project (Tutorial 0) you are ready to start using DPSF.

 

DPSF provides particle system templates to help make creating new particle systems easier, so the first thing we want to do is get the Default Textured Quad Particle System Template included in our project.  To do this, copy the DefaultTexturedQuadParticleSystemTemplate.cs file from the Templates directory and paste it into your project's directory, and then include it in your project.  If you don't know how to do this yet, check out the detailed instructions.  The template class by default is going to look for "Textures/Star9" as the image to use for the particles, so you will also need to create a Textures folder in the project's Content folder and import Star9.png into it.  If you want to use a different image simply replace "Textures/Star9" in DefaultTexturedQuadParticleSystemTemplate.cs's AutoInitialize() function with the asset name of the image that you want to use.

 

Now that we have a particle system class defined (in the template we just included in our project) we can create an instance of it and start drawing it to the screen.  Before we do that though there is one more thing we want to do, which is include the DPSF namespace so that we don't have to put DPSF. in front of all of the DPSF classes and functions.  So near the top of your Game1.cs file (or whatever your main file is called) we want to add the following, below the rest of the using statements:

 

       using DPSF;

       using DPSF.ParticleSystems;

 

We include DPSF.ParticleSystems as well because the template files define the particle systems to be in the DPSF.ParticleSystems namespace.

 

 

Defining a new particle system

 

The name of the particle system class in the template file is DefaultTexturedQuadParticleSystemTemplate (unless you have changed it as mentioned in the template instructions, in which case use that class name), so to define a variable to hold the particle system we give the Game1 class the following variable:

 

       DefaultTexturedQuadParticleSystemTemplate mcParticleSystem = null;

 

 

Initializing the particle system

 

To create an instance of the the particle system add the following code to the Game classes LoadContent() function:

 

       mcParticleSystem = new DefaultTexturedQuadParticleSystemTemplate(this);

       mcParticleSystem.AutoInitialize(this.GraphicsDevice, this.Content, null);

 

Here we create an instance of the particle system, and then call AutoInitialize() to set it up so that it is ready to use.

 

 

Destroying the particle system

 

When we exit the game we want the particle system to be destroyed, so add the following code to the Game classes UnloadContent() function:

 

       mcParticleSystem.Destroy();

 

 

Updating the particle system

 

Before drawing the particle system we must specify the World, View, and Projection matrices so that the particle system knows where on-screen to draw the particles.  Because these typically are not going to change between updates, we can set them here in the Game class's Update() function rather than in the Draw() function, as the Draw() function typically gets called much more often than the Update() function.

 

Some particle systems need to know where the camera is before updating their particles, as they may rotate the particles to face the camera, so we also need to call the particle system's SetCameraPosition() function before updating the particle system.

 

Then to update the particles in the particle system we need to call the particle system's Update() function.  So in the Game class's Update() function add:

 

       mcParticleSystem.SetWorldViewProjectionMatrices(Matrix.Identity, sViewMatrix, sProjectionMatrix);

       mcParticleSystem.SetCameraPosition(cameraPosition);

       mcParticleSystem.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

 

where Matrix.Identity, sViewMatrix, and sProjectionMatrix are the World, View, and Projection matrices respectfully, and cameraPosition is the current position of the camera.

 

 

Drawing the particle system

 

To actually draw the particle system, in the Game classes Draw() function add:

 

       mcParticleSystem.Draw();

 

 

Remarks

 

When you run the Tutorial 1 source code you should see the particle system emitting stars upward.

 

You can also check out more detailed information on Defining, Initializing, Destroying, Updating, and Drawing particle systems, as well as how to use a Particle System Manager to manage multiple particle systems.  The Particle System Manager provides equivalents of the particle system functions shown in this tutorial that affect all of the particle systems in the particle system manager.  This allows you to simply call these functions on the particle system manager, rather than having to loop through and call them on each individual particle system.