Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using ParticleEmitter – Soaring Birds


Particle Emitters, in general, are good in order to create an atmosphere in the games. The most common case is probably for smoke or fire and explosions. Particles can, however, be used for many interesting things. In this recipe, we're going to explore that by tuning a ParticleEmitter to create birds sailing through the sky.

The particles are still sprites, 2D images, so they will work best either far up in the sky, or below us.

The recipe will be divided into two parts. The first one contains setting up the ParticleEmitter class in the SDK and writing the ParticleInfluencer interface. The second part includes changing the way the ParticleEmitter class behaves and extending our ParticleInfluencer interface to take advantage of this:

Getting ready

First of all, we need a suitable bird texture. There's one supplied with the project in the Birds folder inside Textures, which will be fine if the birds are supposed to be far away. Up close, it will not suffice though.

How to do it…

The first section will describe how to set up a material we can use. This consists of the following steps:

  1. We're going to start by creating a material to supply to the ParticleEmitter class. Create a new material in the Materials folder by right-clicking and selecting New… and then Empty Material File.

  2. Rename it to something suitable, for example, Birds.j3m.

  3. Now, we can open it and are automatically moved to the Material Editor window.

  4. Here, we set the Material Definition value to Common/Matdefs/Misc/Unshaded.j3md.

  5. The only thing we need to change is the ColorMap value, which should be pointed to our birds texture.

Now, we come to the configuration of the ParticleEmitter class. This section consists of the following steps:

  1. Let's begin by creating a new scene and opening it in the SceneExplorer window. Right-click and select Add Spatial.. and then Particle Emitter. A default smoke puffing the ParticleEmitter object is created.

  2. Now, we can bring up the Properties window and start tweaking it.

  3. First of all, we set the material to our newly created material for the birds. Don't worry if it looks terrible!

  4. Looking at the Images X property, we can see that it's set to 15 by default. This is the amount of horizontal "frames" in the texture. If we look at the birds texture, we can see that it's only four frames, so let's change that value. The particles are already looking better.

  5. High Life and Low Life define the maximum or minimum lifespan of a particle. We can assume that the birds should soar across the sky for a while, so let's change it to 30 and 25 respectively.

  6. There are an awful lot of birds now. Setting Num Particles to 50 will make more sense.

  7. Start Size and End Size affect the size of the particles over time. These should be set to 1 for our birds. They shouldn't inflate.

  8. For now, let's increase the radius of the emitter to get a better view. It's a sphere by default and the last value is the radius. Set it to 30.

  9. If we take a look at the birds now, they still just float in space. This is very unbird-like.

  10. Let's scroll down a bit to the ParticleInfluencer class. The ParticleInfluencer class has an opportunity to alter a particle's velocity when it's created, decreasing uniformity. The DefaultParticleInfluencer class can set an initial velocity, and a variation, from 0 to 1.

  11. Set the InitialVelocity parameter to 3.0, 0.0, 0.0 and the VelocityVariation to 1.0 to give the particles some individuality.

  12. To make the birds look in the direction they're flying, check the Facing Velocity box.

Tip

New settings won't take effect immediately, but only when a new particle is generated. If you want to speed up the process, click on the "Emit All" button to emit all the new particles with the new settings.

How it works...

A ParticleEmitter can be described as a cheap way to draw many identical or near-identical bitmaps. Particle Emitters have a single mesh that stores all its particles. As opposed to drawing each particle individually, it renders them all at once. This is considerably cheaper. The drawback is, of course, that they all look the same.

There's more…

There is another thing we can do to improve the appearance of the generated birds. Since we are expecting to look at them from either above or below, it makes sense to flatten the shape of the emitter to be more of a plane. Let's revisit the Emitter Shape property and make a box instead of a sphere, as shown in the following code:

[Box, -30.0, -1.0, -30.0, 30.0, 1.0, 30.0]

The numbers define the extremes of a box, that is, X min, Y min, Z min and X max, Y max, and Z max. In other words, we have created a box that is 60 units wide and long and only 2 units high.