|
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 point sprite 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 Point Sprite Particle System Template included in our project. To do this, copy the DefaultPointSpriteParticleSystemTemplate.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 DefaultPointSpriteParticleSystemTemplate.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 DefaultPointSpriteParticleSystemTemplate (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:
DefaultPointSpriteParticleSystemTemplate 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 DefaultPointSpriteParticleSystemTemplate(this); mcParticleSystem.AutoInitialize(this.GraphicsDevice, this.Content);
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
To update the particle system, in the Game classes Update() function add:
mcParticleSystem.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
Drawing 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. So to draw the particle system, in the Game classes Draw() function add:
mcParticleSystem.SetWorldViewProjectionMatrices(Matrix.Identity, sViewMatrix, sProjectionMatrix); mcParticleSystem.Draw();
where Matrix.Identity, sViewMatrix, and sProjectionMatrix are the World, View, and Projection matrices respectfully.
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.
|