Particle systems are a technique to simulate "fuzzy" animations — fire, explosions, smoke, flowing water, clouds, snow, falling leaves, magic spells, etc. The same technique can be used for things like hair or grass, but this can be computationally expensive, and there's little use for such application in our 2D games.
The particle system will be controlled by an emitter. The emitter will generate particles originating from the emitter's location, and give them velocity, rotation, and lifetimes. In a 3D game, an emitter will commonly be an object in the world, and the particles will be generated normal, or perpendicular, to the face(s). This lab will use an small point emitter, in 2D space.
The parameters given to the particles by the emitter will typically be "fuzzy" — rather than specifying an exact value that the emitter gives each particle, the emitter will have a central value, and a range on each side that it can be. For example, the lifetime of each particle in a flame emitter might be 50 frames ±20%.
But what is a particle, exactly? Many 3D games use textured billboard quads.
That's jargon for "a quadrilateral facing the player with an image on it".
While 3D games use other methods as well, this is essentially what we will
be doing.
The sprites you have been drawing up until this point are not very
different from a textured billboard quad: they're quadrilaterals (rectangles)
that always face the player, with an image (texture) on them.
(In truth, there are differences between textures drawn with our
SpriteBatch and a billboard quad — but we can accomplish
our particle system either way, so we will be using the
SpriteBatch to make 2D particles, rather than pretending
the 3D quads are 2D.)
For motivation, first run this 2D Particle Sample from App Hub. Note: The manner in which this is implemented is very different from your lab assignment. You are free to use it (or something similar) on your final project, but it will not be particularly useful for completing the lab.
Implement three particle types: Fire, Water, and Smoke (activated by the B, X, and Y buttons, respectively). The provided code gives you a skeleton Emitter class, and draws a 4×4 red dot at the Emitter's location, to help you track it. The Emitter can be moved around the game with the left thumbstick, and the position can be reset with the A button.
Complete the provided code with these features.
Get random numbers in a given range:
Random rand = new Random();
…
float myRandFloat = (float)rand.NextDouble() * (max - min) + min;
…
int myRandInt = rand.Next(min, max);
You should only use one Random object, no matter how
many random numbers you generate.
Changing a value based on the particle's age:
Particle p = …
float currentValue = MathHelper.Lerp(startingValue, endingValue, p.Lifetime / p.LifeSpan);
(What a funny method name!)
You can also do any multiplication or division to the result of
p.Lifetime / p.LifeSpan to get different behavior.
Try things like Math.Pow(…, 2) or
Math.Pow(…, 0.5) and see what happens!
(This is because, assuming you remove all dead particles and
don't have any particles with negative Lifetime
values, p.Lifetime / p.LifeSpan will always be
between 0 and 1. The result of any multiplication or division will
remain in the same range.)
The Particle structure and the Emitter
class might not have everything you need to complete the assignment.
Feel free to add any methods, variables,
fields, or properties you want!