2D Particle Engines - Part 4

Sections > 1 - 2 - 3 - 4

Using our Particle Engine

We are now ready to use our particle engine 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 engine, 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 engine. So go to your main game class, and add the following line of code as an instance variable to your class:

ParticleEngine particleEngine;

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"));
particleEngine = new ParticleEngine(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 engine 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 engine to update itself:

particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
particleEngine.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 engine to update itself:

particleEngine.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 engine in effect. You should hopefully see something like the image below:

screenshot-1.png

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;
        ParticleEngine particleEngine;
 
        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"));
            particleEngine = new ParticleEngine(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();
 
            particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            particleEngine.Update();
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
 
            particleEngine.Draw(spriteBatch);
 
            base.Draw(gameTime);
        }
    }
}

What's Next?

This tutorial shows how to make a 2D particle engine. The first thing that you should do is play around with the various parameters in the particle engine, like the total number of particles generated, the initial velocity of the particles, the colors of the particles, and so on. You can accomplish quite a bit by changing the way these values are assigned. Also, try changing the textures that you use for the particle engine.

Another interesting thing to try out is to combine the stuff in this tutorial with the stuff in the Additive Sprites tutorial. Additive sprites do a really good job of giving you a sort of glowing effect, which is what I did for the engine exhaust in the Asteroids game that was shown at the beginning of this tutorial. There are a lot of things that you can do with particle engines, so experiment and see what you can accomplish.

Sections > 1 - 2 - 3 - 4


Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!