Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    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


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