Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

A Simple Particle Effects Object
Date 3/12/2009    Tags XNA    (0)

Update: The article A Better Particle Effects Object is better... use that one instead of this one!

A particle is simply a small object with a few properties like speed, size, and direction. What makes them impressive is that when they are drawn in large quantities, they can be used to create realistic fire, smoke, and explosions (among other things). Adding particle effects is easily one of the coolest graphical additions that you can make to your game and is easier than you may think.

In order to do this, first I have created a Particle class. This will be a single instance of the many particles we will draw. Each particle has a size (width/height), a texture (I'm just using a plain black one for now but using a more detailed texture can make for very impressive effects), the particle's current position, the direction the particle is traveling in, and it's speed.

Some of this information (such as speed and size) is hardcoded in this example. If you want to make this more flexible, then I suggestion you either allow these to be set in the constructor or create a public accessor to them.

class Particle
{
    private Vector2 size;
    private Texture2D particleTexture;

    public Vector2 Position
    {
        get;
        set;
    }

    public Vector2 Direction
    {
        get;
        set;
    }

    public Vector2 Speed
    {
        get;
        set;
    }

    public Particle(ContentManager contentManager)
    {
        Speed = new Vector2(130, 130);
        Direction = new Vector2(0, 0);
        size = new Vector2(1, 1);

        particleTexture = contentManager.Load<Texture2D>("level_graphics\\Black");
    }

    public void Update(float elapsed)
    {
        Position += Speed * Direction * elapsed;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(particleTexture, new Rectangle((int)Position.X, (int)Position.Y, (int)size.X, (int)size.Y), Color.White);
    }
}
That's nice and all, but it's only one particle and we're going to need a lot more than that. That's why I created a collection class, aptly named Particles.


class Particles : System.Collections.ObjectModel.Collection<Particle>
{
    const float DEFAULT_START_VALUE = .35f;
    private float particleTimer;

    public Particles()
    {
        particleTimer = 0;
    }

    /// <summary>
    /// Initiallize your set of particles.
    /// </summary>
    public void CreateParticles(int numberToCreate, ContentManager contentManager)
    {
        Random newRandom = new Random();

        for (int i = 0; i < numberToCreate; i++)
        {
            <
                    
                    


This article has been view 1548 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)