Wednesday, May 12, 2010

2D Particles: Part 7 Even More Options Part 1

Welcome to the seventh part of the 2d particles engine today we will be adding in some more variation to the engine. Also we will be making it run slightly smoother through something I should of realized a long time ago but didn't because I had an idea with it that I probably will not be adding. So lets get to it, first thing were going to do is add and remove some variables from our particles class. We removed the texture variable while adding scale, Acceleration and Origin. Instead of each particle having its own texture because there all the same were just going to use one from the particle engine class. Acceleration and origin will be the same for every particle and for every instance so were going to declare them within the particle engine class when we initialize our particles. You could have also set them in the constructor but there might be more variables like this later so this is fine for now.
Now were going to add in the scale declaration into the initialize method so that we can use it when we draw our particle. Everything else here is the same but I took the whole function for clarity.
Now we need to add in acceleration into our update method other wise it is worthless. This is pretty simple we just add acceleration into velocity then add velocity into position. Our current acceleration rate is actually acceleration times time elapsed which we can easily calculate by taking our total life time and subtracting how much life is left. Also multiplying by how much time has elapsed. We are also going to slightly change our draw method to account for scale and our new origin variable. This method also needs the texture 2d from our particle engine class so were adding that as a new parameter.
Now that we have the items implemented into the particle class lets add them into the particle engine class so that we can define them from our main game class like before. Were going to add in a minimum scale, maximum scale, Acceleration and Range. Acceleration is a vector 2 to allow accelerations in both x and y directions. Range is a variable that we can use to allow the particles to spawn within a range of positions and not just one exact point. Using this you are able to create effects such as water falls or fountains pretty easily.
Now were going to add in the acceleration and origin into the particles from our initialize method after we create each one. One thing to note is that we multiply the y coordinate of our acceleration by -1 because down is actually position so we need to flip it.
Now we also need to add in range and scale into the initialize particle method. The way our range works is that it will pick a random number between the -range and range and add that to our position which essentially acts as the center point. To get a random number between -range and +range we simply pick a number between range and range * 2 and subtract one range. Our scale is also in there, but its the same as our life time code so I do not need to explain it. We also need to edit the drawing method to work with our new drawing method from the particle class. All you need to do is add the texture variable after the sprite batch pretty easy.
Now that we have everything set up lets go into the game class and load in the engine. The following is my sample that doesn't really look like much but shows you the new range, scale and acceleration factors. Be sure to download the Star texture I made or use your own. You can make some pretty cool effects just by changing the numbers around see what you can make.
Well that is it for the seventh part the eighth part is going to include some more options. I might try and start doing video tutorials if I can get the hang of it let me know if you would like them. Any suggestions, comments or help just post a comment. Also I was thinking of making a visualizer that will either export xml files that you can load into your game or just give you the code for loading so that you can copy and paste it in, more news on that later.

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

No comments:

Post a Comment