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:
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.
The first section will describe how to set up a material we can use. This consists of the following steps:
ParticleEmitter
class. Create a new material in the Materials folder by right-clicking and selecting New… and then Empty Material File.Birds.j3m
.Common/Matdefs/Misc/Unshaded.j3md
.Now, we come to the configuration of the ParticleEmitter
class. This section consists of the following steps:
ParticleEmitter
object is created.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
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.Num Particles
to 50 will make more sense.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.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.InitialVelocity
parameter to 3.0, 0.0, 0.0
and the VelocityVariation
to 1.0
to give the particles some individuality.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 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.
3.143.205.136