Creating a muzzle flash using ParticleEmitter

Weapons of some sort are a common feature of many games and muzzle flashes greatly enhance the appearance and feeling when you fire. This recipe will show a way to create good-looking muzzle flashes by tweaking a ParticleEmitter's properties. The following screenshot shows a texture with four muzzle flashes:

Creating a muzzle flash using ParticleEmitter

Getting ready

There are two things needed before we can begin work on the ParticleEmitter:

  • First of all we need a texture for the muzzle flash. This can have anything from one to several images of muzzle flashes. The texture should be gray scaled. We'll add color using ParticleEmitter.
  • Secondly, we need to create a Material using the texture by performing the following steps:
    1. Right-click on your project's material folder and select New.../Empty Material file.
    2. Select Particle.j3md as Material Definition.
    3. Then select the muzzle flash texture as Texture.

How to do it...

Now, we can begin creating the muzzle flash emitter:

  1. Navigate to the Emitters folder in the project and select New.../Empty jme3 Scene. We should now have a fresh scene.
  2. Right-click on the main node in the SceneExplorer window and select Add Spatial/Particle Emitter.
  3. Select the emitter instance and open the Properties window.
  4. Make sure the Shadow Mode option is Off and Queue Bucket is Transparent.
  5. Then, select the muzzle flash material we created in the Geometry/Material section.
  6. Make the Emitter shape really small, for example, something like [Sphere, 0.0, 0.0, 0.0, 0.05].
  7. Num Particles should be 1, and Particles Per Sec should be 0.0.
  8. Set Start Color to something like [1.0, 1.0, 0.4, 1.0] and End Color to [1.0, 0.6, 0.2, 0.7].
  9. Both Start Size and End Size should be 1.0.
  10. High Life and Low Life should be 0.15.
  11. Gravity and Face Normal should be [0.0, 0.0, 0.0].
  12. Check the Facing Velocity box and set the Initial Velocity to[0.0, 0.0, 1.0].
  13. Images X and Images Y should reflect the number of frames in the texture we created.
  14. We can now test the emitter by clicking on the Emit! button.

All these values can be seen in the following screenshot:

How to do it...

How it works...

The muzzle flash works pretty much like a normal ParticleEmitter with a couple of exceptions. Instead of outputting a constant stream of particles, it will only emit one. This is because Num Particles is set to 1, meaning only one particle can be alive at any given time. Particles Per Sec is 0.0 so it won't continuously emit anything.

The colors are set to be yellowish, turning a bit orange and fading slightly at the end of the lifetime; the lifetime being very short in this case, only 0.15 seconds.

A muzzle flash is emitted in one direction only. This is why we set Facing Velocity to true so that the particle will point in the direction of the velocity.

Getting it to appear in the correct position in relation to the weapon can require a bit of tweaking. Using Local Translation can help us in this.

To use the muzzle flash on a weapon, open the target in Scene Composer and then choose Link in Scene on the muzzle flash. This way the original file can be modified and the changes will automatically appear in the places its being used.

There's more...

Now that we have the muzzle flash and added it to a weapon, we can create a control in order to use it within the game by performing the following steps:

  1. Create a new class called WeaponControl extending AbstractControl.
  2. Add a ParticleEmitter field called muzzleFlash.
  3. In the setSpatial method, check whether the supplied spatial has a suitable child, either by type or name (requires that the muzzle flash has a fixed name), and set the muzzleFlash field:
    muzzleFlash = (ParticleEmitter) ((Node)spatial).getChild("MuzzleFlash");
  4. Now, we create a publicly available onFire method and add the following:
    if(muzzleFlash != null){
      muzzleFlash.emitAllParticles();
    }

This control should then be added to the weapon spatial inside the game and onFire should be called whenever the weapon fires. The class is suitable to play sounds and keeps track of ammunition as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169