3 min read

Particle Systems - Part 4

Using our Particle System

We are now ready to use our particle system in our game. With the work we’ve done in the previous parts of this tutorial, this will be pretty simple to do. There are several things we will need to add to the main game class to use the particle system, which are discussed here in the final section of this tutorial.

The first thing we will need to do is create an instance of our particle system. So go to your main game class, and add the following line of code as an instance variable to your class:

ParticleSystem particleSystem;

Next we need to assign this a value. We can only do this after we have loaded the textures for it to use, so go down to your LoadContent() method and add the following code there:

List<Texture2D> textures = new List<Texture2D>();
textures.Add(Content.Load<Texture2D>("circle"));
textures.Add(Content.Load<Texture2D>("star"));
textures.Add(Content.Load<Texture2D>("diamond"));
particleSystem = new ParticleSystem(textures, new Vector2(400, 240));

This loads in the three textures that we added to our project in part 1. If you are using different textures, change them here. In the last line, we create our particle system with our textures and a default location in the middle of the screen.

Next, go to the Update() method and add the following code to allow the particle system to update itself:

particleSystem.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
particleSystem.Update();

This does two things. First, it changes the location of the particle emitter to the location of the mouse. If you haven’t done much with the mouse at this point, it might be worth checking it out. After that, we just tell the particle emitter to update itself, which will in turn update each of the particles.

Finally, go down to the Draw() method and add the following line of code to tell your particle system to update itself:

particleSystem.Draw(spriteBatch);

Additionally, in the Draw() method, I have changed by background/clear color to be Color.Black instead of Color.CornflowerBlue.

You should now be able to run your game and see your particle system in effect. You should hopefully see something like the image below:

Screenshot 1

Additionally, I have included the entire code for my Game1 class for you guys to take a look at:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ParticleEngine2D
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        ParticleSystem particleSystem;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            List<Texture2D> textures = new List<Texture2D>();
            textures.Add(Content.Load<Texture2D>("circle"));
            textures.Add(Content.Load<Texture2D>("star"));
            textures.Add(Content.Load<Texture2D>("diamond"));
            particleSystem = new ParticleSystem(textures, new Vector2(400, 240));
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            particleSystem.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            particleSystem.Update();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            particleSystem.Draw(spriteBatch);

            base.Draw(gameTime);
        }
    }
}

Expanding on the Basic Particle System

With this basic system in place, you can now try experimenting to get different visual appearances for your particle system. You can tweak the number of particles generated, the initial velocity, the colors, etc. You can accomplish quite a bit by changing the way these values are assigned. Also try changing the textures used in the particle system.

You can also combine this tutorial with the stuff in the additive sprites tutorial. Additive sprites do a really good job of giving you a glowing effect, which is what I did for the engine exhaust in the Asterois game that was shown in Part 1.

There’s a lot you can do with particle systems, to experiment and see what you can accomplish.