Wednesday, May 5, 2010

2D Particles: Part 5 More Options

After some time I have finally put together part 5 of the particles tutorial. The reason I wasn't sure of what to do for part 5 will be more clear later on. In this tutorial we will be adding more options to our particle engine, as of right now everything is predefined and pretty boring. Well lets change that now. First were going to change our const speed, rotation speed and life time to a minimum and maximum range. We are also going to add in a minimum and maximum angle.
Had to separate it into two images.
Next were going to define a property to let us get the time per particle variable now that our amount will not be constant. We will also be changing the constructor back to no variables since were going to change how we initiate our class.
Note: If you added the #region Properties at the top you must add a #endregion somewhere.
I would do it right below the time per particle property some how I forgot to add it into the picture.

Now that we changed our variables our updating is all messed up, so lets fix that now. Instead of splitting it in 3 parts lets instead split it into 2 parts. Now instead of counting how many particles to add then looping to add the particles lets just add the particles while were counting. Yeah I know I should of thought of this the first time but I didn't...
Next were going to slightly change the particles constructor to instead take speed and angle and not velocity this is so we don't have to store them in the particle engine.
Next we change our new particle method to create our particle based on our min and max values. This is all pretty easy so nothing to really go over here.
Now one thing has changed a lot and that is how we define our particle engine in our game class. Now I was reading some code earlier and I am trying this new way of declaring a class. Anyway in your game1 load content method type in the following, be sure to add the circle texture into your games content download at the bottom. Now you can change the numbers around how you like also note that I did not add in the minimum angle and maximum angle so it is set to the default 360 degrees.
Now as long as you have the particle engine updating and drawing it should work just as it did before. But with 500 particles per second we are allocating a lot of memory for all of these particles. In only one minute of running the game using the CLR profiler I got a chart like this. Thats 30,000 particles per minute being allocated and wasted so in the next part I will be fixing this to allow us to run much smoother without such a huge memory problem. I have already completed this next part so hopefully it will be up tomorrow.

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

No comments:

Post a Comment