Wednesday, June 16, 2010

2D Particles: Part 8 Even More Options Part 2

Welcome to the eighth part of the 2d particles tutorial. It has been a while since I last posted and hopefully I post more frequently now that school is over and its summer. So lets get to it. In this final part of the tutorial we will add particle fading in and out as well as color interpolation. This is not very difficult all we are going to do is add in an enum representing the different styles of fading which we will use to update our particles. Also we will add in three color variables in the particle class, a starting color, ending color, and one to use to draw. And that will be all for this tutorial. I might do a slightly more advanced particles tutorial later that will use emitters and potentially be in 3d not 2d. So without any more delay lets begin by creating our fade style enum in our particle class above the Particle class itself, see source if you do not know where.
These are just some simple ones but you can add more if you want. Now lets declare our fade style and three color variables in the particle class. By default we will have no fade and no change in color so we can declare them like so.
Now we are going to create a function that will be called in our update method that will update just the color. The alpha of the particle is also here since that is the color.
All of this is relatively simple just know that when your setting the alpha value for the colors your setting them in bytes from 0 to 255 not a float from 0 to 1. That is why we need to multiply by 255 or it will not work correctly. So next we will add in the function to the middle of the particle's update method just above where you check whether or not its alive.
Now that we have fading and our coloring we need to implement how to use them, first step is to make sure were drawing them correctly by changing the draw method of the particles to use the color variable.
Next, we need to add in a fade style, start color and end color properties into the particle engine class to enable us to use them.
Finally, we just need to set the start color, end color and fade styles of the particles as we load them.
And we are done, the following is a simple sample by me using the new features.
Well that is all of this tutorial on creating a 2d particle engine. You are able to do a lot of stuff using this engine but the more typical approach is to create a particle emitter class, basically the particle engine class. And have specific emitters inherit from that class that will update the particles in different ways such as instead of using a point as the center you might use a square. Or instead of the particles moving outwards you might want them to move inwards. This system allows you to have multiple types of emitters that are easily accessible. It is not too much more work to get this system to work and if I have time I might do a simple tutorial between 3-5 parts as it is not that long but for now I am done with particles. As always questions can be asked in the comments below.

1 comment:

  1. Can you make an additional option which makes the particle move like from position 1 to position 2. Ignoring -> Update(GameTime gameTime, Vector2 position)
    .
    .
    just Update(GameTime gameTime)

    ReplyDelete