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.

11 comments:

  1. Excelent tutorial.
    Simple, clare and working.

    Thanks a lot.

    ReplyDelete
  2. Man, I have a question. How do I know what textures I must load?

    ReplyDelete
  3. The textures are in the BasicExplosion.XML file. Just open it with a text editor (Notepad++). The particle names are next to the 'ParticleTextureAssetName'. The \MercuryExample1\Content folder. You need to add the textures that make up the effect to your project.

    ReplyDelete
  4. It seems to not display anything for me, despite literally copying your code word or word. Could anyone else confirm this?

    ReplyDelete
  5. The same thing happened to me (as our anonymous buddy) this needs to be out of the spriteBatch.Begin() -- End() area

    spriteBatch.Begin();
    ... Draw your stuff ...
    spriteBatch.End();

    // Here put the draw effect method
    myRenderer.RenderEffect(myEffect);
    base.Draw(gameTime);

    Hope this helps!

    ReplyDelete
  6. I'm having some difficult to use the Mercury Particle, even tough my code is OK, compiles, buts doesn't display anything =/

    ReplyDelete
  7. Not sure what i am doing wrong.
    get and error with:
    myEffect = Content.Load("BasicExplosion");

    says error while deserializing intermediate XML?

    and does it make a difference
    myEffect = Content.Load("BasicExplosion");
    or
    myEffect = Content.Load("BasicExplosion");

    ReplyDelete
    Replies
    1. meant
      myEffect = Content.Load("BasicExplosion");
      or
      myEffect = Content.Load("BasicExplosion");

      Delete
    2. those are 4 time the exact same line of code dude ...

      Delete
  8. This steps doesnt work at all ... there are no .trigger methods in the particleEffect class ...

    ReplyDelete