Showing posts with label Particles. Show all posts
Showing posts with label Particles. Show all posts

Monday, July 5, 2010

How to: Mercury Particle Engine

I was looking for good, fast and reliable particle engine, for XNA and after a while, i decided that i will use Mercury Particle Engine. It looks fantastic and its fast and it comes with graphical particle effect editor. How cool is that? Theres really no limit, on what kinds of effect you could create.

Mercury project page can be found from here!

( Source of this tutorial and compiled exe )
Download: MercuryExample.Zip


Only thing that bothers me, is the lack of tutorials for it. There really isnt anything, that helps "newbie" coders to get those beatifull particles from editor, to XNA. Well, i made a short example project, that loads and draws the "BasicExplosion", that comes as an example with the editor.

Its quite easy to get particles to drawn on screen. You need to add that particle xml file and all the textures it needs, to content of your XNA project. After that, its just coding.

// Add MercuryProject.dll reference to your project
// Add using statements
using ProjectMercury;
using ProjectMercury.Emitters;
using ProjectMercury.Modifiers;
using ProjectMercury.Renderers;
Inside the Game class, add:

 // Renderer that draws particles to screen
 Renderer myRenderer;
 // Particle effect object to store the info about particle
 ParticleEffect myEffect;
 Inside the Game constructor add:

 // Create new renderer and set its graphics devide to "this" device
 myRenderer = new SpriteBatchRenderer
 {
         GraphicsDeviceService = graphics
  };

 myEffect = new ParticleEffect();
Inside LoadContent add:
  myEffect = Content.Load("BasicExplosion");
  myEffect.LoadContent(Content);
  myEffect.Initialise();
  myRenderer.LoadContent(Content);

Inside Update add:
  // get the latest mouse state
  MouseState ms = Mouse.GetState();
  // Check if mouse left button was presed
  if (ms.LeftButton == ButtonState.Pressed)
 {
         // Add new particle effect to mouse coordinates
         myEffect.Trigger(new Vector2(ms.X, ms.Y));
  }
  // "Deltatime" ie, time since last update call
  float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  myEffect.Update(SecondsPassed);
And finally, add to Draw:
 myRenderer.RenderEffect(myEffect);


Now, compile.
Remember to add the dll to Content references too, or else, it wont know how to import those files. Now, if its running, just click with mouse and enjoy.