- jMonkeyEngine 3.0 Cookbook
- Rickard Edén
- 829字
- 2021-09-03 10:00:47
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:
- 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. - Rename it to something suitable, for example,
Birds.j3m
. - Now, we can open it and are automatically moved to the Material Editor window.
- Here, we set the Material Definition value to
Common/Matdefs/Misc/Unshaded.j3md
. - 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:
- 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. - Now, we can bring up the Properties window and start tweaking it.
- First of all, we set the material to our newly created material for the birds. Don't worry if it looks terrible!
- 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. High Life
andLow 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.- There are an awful lot of birds now. Setting
Num Particles
to 50 will make more sense. Start Size
andEnd Size
affect the size of the particles over time. These should be set to 1 for our birds. They shouldn't inflate.- 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.
- If we take a look at the birds now, they still just float in space. This is very unbird-like.
- Let's scroll down a bit to the
ParticleInfluencer
class. TheParticleInfluencer
class has an opportunity to alter a particle's velocity when it's created, decreasing uniformity. TheDefaultParticleInfluencer
class can set an initial velocity, and a variation, from 0 to 1. - Set the
InitialVelocity
parameter to3.0, 0.0, 0.0
and theVelocityVariation
to1.0
to give the particles some individuality. - 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.
- C++程序設(shè)計(jì)(第3版)
- Web Development with Django Cookbook
- 實(shí)戰(zhàn)Java程序設(shè)計(jì)
- PhpStorm Cookbook
- Scala程序員面試算法寶典
- Extending Puppet(Second Edition)
- Unity&VR游戲美術(shù)設(shè)計(jì)實(shí)戰(zhàn)
- Machine Learning With Go
- Python機(jī)器學(xué)習(xí)算法與應(yīng)用
- Java EE架構(gòu)設(shè)計(jì)與開(kāi)發(fā)實(shí)踐
- JavaScript Concurrency
- Cinder:Begin Creative Coding
- HTML5 and CSS3:Building Responsive Websites
- C++ Game Development Cookbook
- Kotlin核心編程