Where there's smoke, fire and explosions... there's the 3D particle engine, Tranquility
- Posted: Jan 30, 2012 at 6:00 AM
- 14,695 Views
- 3 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Today's Mobile Monday project is one that's both simple, yet complicated. Simple in that this project is just one piece of the overall picture of where you can use it. Complicated in that this might be something the average dev might not be able to just knock out.
Let's say you're writing a cool game and you need an explosion to cap it off. You'd don't want a cheesy one, but your not sure where else to go?
Go here...
Tranquility is a compact, easy to use and highly extendible 3D particle system for Windows Phone 7 XNA projects.
For installation/integration, check out the Getting Started Guide. To learn how to use Tranquility check out Using Tranquility.
Using Tranquility is easy; most tasks can be achieved in a couple of lines of code. The demo project included with the source showcases more advanced scenarios including AppHub’s fire, smoke and explosion samples.
Particle Manager
All particle properties, behavior and drawing are managed by Tranquility's ParticleManager. ParticleManager is a DrawableGameComponent that can simply be added to your Game’s component collection.
....
Using Tranquility
Using Tranquility is easy; most tasks can be achieved in a couple of lines of code. The demo project included with the source showcases more advanced scenarios including AppHub’s fire, smoke and explosion samples.
Particle Manager
All particle properties, behavior and drawing are managed by Tranquility's ParticleManager. ParticleManager is a DrawableGameComponent that can simply be added to your Game’s component collection.
The ParticleManager needs to know how your world is being viewed. Use the SetMatrices method to set the view and projection matrices.
Particle Systems
A ParticleSystem defines a group of particles that share a texture representation. There are two base particle system types in Tranquility:
- StaticParticleSystem: All particles in this system have static properties. Once a particle is added to this system type, it cannot move, grow, change color, etc. Although particles can be added and removed to/from this system on the fly, it is ideal for static allocation of particles.
- DynamicParticleSystem:A dynamic particle system contains particles that can have a velocity, rotation, lifespan and can be affected by various affectors.
Creating and registering a particle system
To create a system, specify the maximum capacity and the texture to be used for this system in the LoadContent method. For example, create a dynamic particle system:
..
Particles
The overloaded AddParticle method can be used to add particles to any system. The RandomHelper class can be used to randomize the generation of particles. For example:
particleSystem.AddParticle(
RandomHelper.Vector3Between(Vector3.Up, Vector3.Down),
RandomHelper.Color(),
RandomHelper.NormalizedVector3(),
RandomHelper.Float(),
TimeSpan.FromSeconds(RandomHelper.IntBetween(1, 3))
);Particle Emitters
Emission of particles can be automated using a particle emitter. To create a custom particle emitter, implement the IParticleEmitter interface:
1: public class CustomParticleEmitter : IParticleEmitterImplementing the Update method will allow the emitter to automatically emit particles in the particle system it is added to. The Emit method can be used to emit particles manually.
...
Particle Affectors
A particle affector can affect one or more properties of all particles in a system. There are three default affectors in Tranquillity that use the particle’s age as the time factor:
- Decelerate: Slows a particle down to a complete stop towards the end of its lifespan. This affectors affects only particles that have a velocity.
- Fadeout: Reduces the alpha of a particle until it becomes completely transparent towards the end of its lifespan.
- Shrink:Reduces the size of a particle until it disappears completely towards the end of its lifespan.
- ...
Here's a snap of the Solution. As you can see, it's not crazy or insanely complicated (nor uses another other libraries).
Here's a snip from the Demo;
public FireParticleSystem(int maxCapacity, Texture2D texture)
: base(maxCapacity, texture)
{
}
public override void Update(GameTime gameTime)
{
EmitParticles(gameTime);
foreach (DynamicParticle particle in liveParticles)
{
particle.Color = Color.Lerp(particle.InitialColor, new Color(1.0f, 1.0f, 1.0f, 0.0f), 1.0f - particle.Age.Value);
particle.Scale += 0.001f;
}
base.Update(gameTime);
}
private void EmitParticles(GameTime gameTime)
{
particlesEmitted += (float)gameTime.ElapsedGameTime.TotalSeconds * (float)EmissionRate;
int emittedCount = (int)particlesEmitted;
if (emittedCount > 0)
{
for (int i = 0; i < emittedCount; i++)
{
AddParticle(
RandomPointOnCircle(),
new Color(255, 255, 255, 100),
RandomHelper.Vector3Between(new Vector3(-0.25f, 0.0f, 0.0f), new Vector3(0.25f, 1.0f, 0.0f)),
RandomHelper.FloatBetween(-0.01f, 0.1f),
TimeSpan.FromSeconds(RandomHelper.IntBetween(1, 2)),
true,
RandomHelper.FloatBetween(0.0f, MathHelper.TwoPi),
RandomHelper.FloatBetween(0.05f, 0.075f));
}
particlesEmitted -= emittedCount;
}
}
This is one of those projects that will help you add that final touch, help you apply a nice bit of polish to your app or game...
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
I've followed this project for a while now. It is nice, but is still in the early stages and is relatively young.
I created DPSF (http://www.xnaparticles.com) back in 2009, it is regularly updated, and it is now very established and used in many XNA projects and indie games. Also, particle systems created in DPSF can be used on PC, Xbox 360, and Windows Phones with few or no changes required. DPSF is fully documented and comes with many tutorials and examples (including source code) to learn from or use directly in your games (including explosions). Like Tranquility, DPSF is free and highly extensible; however it is not open source.
I definitely still recommend checking out Tranquility as it uses some different concepts than DPSF that you may prefer (static vs dynamic particle systems, affectors, etc.), but just be aware that DPSF is another XNA alternative worth checking out.
@deadlydog: Nice! Thanks for the heads up on that.
Look interesting project.
Remove this comment
Remove this thread
close