Friday, April 30, 2010

2D Particles: Part 3 Basic Engine

Welcome to the third part of the 2d particle engine tutorial. Today we will be adding our basic particle engine class that will hold all of our particles info. Now what we want this class to do is load, create, update and draw our particles as well as creating new ones. Now before we begin let me say that this class is the core of our engine and will be very complex in the end so throughout the tutorial as we add in more stuff I will also be adding in comments to help explain what everything does. There are some special ways to adding in comments that I will explain part way between the tutorial that will help when coding large projects.

So lets begin, first lets create a new class named ParticleEngine and add in the following variables.
Now lets go over the variables we added since some of them might not be obvious. Our random variable is going help us in creating a lot of completely different particles using random numbers. Our list of particles holds our particles and texture is the texture of our particles. Our speed variable is the speed of our particles, amount is how many particles we want to create every second and our time per particle lets us know how long it takes for each particle. This is similar to our sprite animation updating. Lifetime is how long our particles will be alive which we set to 1 second, our speed of rotation for our particles and the blend mode we will be using for our drawing. This is set to alpha blend now but additive is also a good one. The last two keep track of how many particles we have to create this frame and how much time has passed before our next particle is created. More explanation when we go over the updating method.

Now lets create the constructor for our engine. The only variable we need is our texture so lets add that as our only parameter. We also want to initialize our particle list and our random number generator. That is all for our constructor for now.
Next were going to do our updating. Now this method is going to be called from our game class that will update our particles but it will do more than just update our particles. This method has to create new particles when its time and remove old particles that have died.
So lets first go over the parameters, we need the game time to update the time of our particles and update our particles. We also need the position that our particles are being created from, this will allow us to move our particles very easily later. Now the first block of code is updating our current time. Next we have a while loop that checks if our current time is greater than the time per particle and if it is we add one to our current amount, pretty basic. Next we have a small if statement that checks to see if we need to create any particles. If we need to create some we do a for loop to create particles using a New Particle method that we will create next. Then we use another for loop to cycle through our particles to update them. We also have to check whether or not the particle is still alive. If the particle isn't alive we remove it from our list and go back one in our loop.

Now were going to create the New Particle Function, this function create a new particle that we add to our list. This will be expanded a lot before we end this tutorial but for now we are going to set a velocity that is based on a random angle. Then we simply return a new particle using the particles constructor created in our last tutorial. Note: The reason were using degrees and not radians is because later we will be setting this at run time and its easier to visualize degrees than radians.
Now that we have our particles being created and updated we should make a function that draws our particles since that is what we really want. Our particle class already has a draw method so were basically just going loop through all of them. This method will start and end our sprite batch since we want to use our blend mode and not the one from our game class, so be sure not to start or end the sprite batch from the main game class when you call this method.
One thing to mention is that we have to loop backwards through our particles so that the ones that were created first are drawn on top. Next time we will be adding code in our game class to use our particle engine which will be a short tutorial.

You are free to use or modify this source in any way it is for learning purposes.

No comments:

Post a Comment