Chapter 8. Special Effects

In this chapter, you will learn to add special effects to Canyon Bunny using LibGDX's particle system, and learn about linear interpolation, and several other ways to enhance the visual appearance of the game. You will also design a custom particle effect in a graphical editor, which will serve as dust. This dust effect will be shown whenever the player character is running on rocks. You will be introduced to the concept of linear interpolation, using examples of smoothing the camera's movement while it is following a set target, as well as letting the rocks slowly bob up and down on the water.

In addition to this, you will implement a parallax scrolling effect for the displayed mountains in the background. The clouds will continuously move at random speeds from the right to the left of the level. The game's GUI will also be enhanced by adding some subtle effects for events where the player has lost a life or when the game score has increased.

In this chapter, you will learn to:

  • Create complex effects using LibGDX's particle editor
  • Add a dust particle to our bunny
  • Smooth the movement of clouds and rocks using the linear interpolation (Lerp) operation
  • Add some animations to show changes in the score and life of the player

Creating complex effects with particle systems

A particle system is a great way to simulate complex effects such as fire, smoke, explosions, and so on. Basically, a particle system consists of a number of images that are rendered using either a normal (alpha masked) mode, or an additive blending mode to create interesting results.

Take a look at the following screenshot to see the difference between normal and additive blending modes:

Creating complex effects with particle systems

LibGDX provides a sophisticated particle system through its ParticleEffect class. It's merely a container that allows you to easily work with your final effects on a high level, such as setting the position or triggering or cancelling the designed particle effect.

The following is a brief description of the most important methods of ParticleEffect:

  • start(): This starts the animation of the particle effect
  • reset(): This resets and restarts the animation of the particle effect
  • update(): This must be called to let the particle effect act in accordance to time
  • draw(): This renders the particle effect at its current position
  • allowCompletion(): This allows emitters to stop smoothly even if particle effects are set to play continuously
  • setDuration(): This sets the overall duration the particle effect will run
  • setPosition(): This sets the position to where it will be drawn
  • setFlip(): This sets the horizontal and vertical flip modes
  • save(): This saves a particle effect with all its settings to a file
  • load(): This loads a particle effect with all its settings from a saved file
  • dispose(): This frees all the resources allocated by the particle effect

Using an instance of ParticleEffect alone will not yield anything visible on the screen yet. This is because particles are represented as images and this class does not have such a reference to an image. Instead, an instance of the ParticleEmitter class is required, which among other attributes can take a reference to an image for the particles to be rendered. An emitter manages particles that use the same image. The particles of an emitter also share the same specific behavior, which is usually given in ranged values. These ranged values define the range to generate random values for each spawned particle, which will make the resulting effect look much more natural. If needed, several emitters can be added to a single effect in order to create more complex particle effects that use multiple images and collective behavior.

The following code is an example of how to create a particle effect with one emitter:

ParticleEffect effect = new ParticleEffect();
ParticleEmitter emitter = new ParticleEmitter();
effect.getEmitters().add(emitter);
emitter.setAdditive(true);
emitter.getDelay().setActive(true);
emitter.getDelay().setLow(0.5f);
// ... more code for emitter initialization ...

While there is absolutely nothing wrong with this approach in general, it is not recommended to initialize particle emitters in code. Emitters contain roughly 20 attributes that can be played with, so it is quite obvious at this point that this approach will lead very quickly to a lot of code snippets that are hard to understand and maintain. A much cleaner and fun solution is to use LibGDX's graphical particle editor in order to create new particle effects or modify the existing ones. The editor features a live preview of the current settings of the particle effect, which simplifies the design phase a lot. The preview gives direct feedback on the final result of the particle effect as well as how changed attributes alter the effect.

You can download and run the latest editor by entering http://wiki.libgdx.googlecode.com/git/jws/particle-editor.jnlp in your browser.

To know more about running the editor, check out this wiki article at https://github.com/libgdx/libgdx/wiki/2D-Particle-Editor.

The following screenshot shows the Particle Editor window right after it was started:

Creating complex effects with particle systems

The live preview is located in the top-left corner and shows the current particle effect as well as some runtime information as follows:

  • FPS: This gives the frames per second achieved
  • Count: This gives the current number of particles in use
  • Max: This gives the maximum number of simultaneously existing particles allowed
  • Percentage: This gives the progress between the start (0 seconds) and the end (Duration)

The particle effect in the live preview can be moved around by clicking-and-dragging it with the mouse. This is a pretty neat feature not only because it allows adjustments to the effect's starting position, but also because it can be used to quickly check out how the effect behaves under non-stationary circumstances.

The bottom-left corner holds the already mentioned list of particle emitters. Emitters can be given a name, but this is completely optional. The checkmark next to an emitter's name toggles its visibility. However, take note that this is just an editor-only setting to make the editing of complex effects with many emitters a bit easier. The state of the visibility checkmark is also not going to be saved to the particle effect file. The order of emitters can be changed using the Up and Down buttons to the right of the emitters list. The rendering of the emitters list runs from the top to the bottom. This means that the last emitter in the list is also going to be the last, and therefore the most rendered emitter. New emitters can be added by clicking on the New button. Similarly, a click on the Delete button will remove the currently selected emitter from the emitters list.

The right-hand side of the particle editor is split into two framed parts that contain two types of properties. The upper one is labeled Editor Properties, which controls how the live preview is rendered. The Pixels per meter setting defines the dimensions just like we did for our game screen camera. The Zoom level setting is used to scale things either up or down depending on what can be seen in the live preview.

The last and biggest portion of the particle editor is contained in the frame labeled Emitter Properties. It controls everything about how the particles of an emitter look and how they will behave over a period of time.

Here is a list of all the available properties and their meanings:

  • Image: This is the image file that graphically represents the particle.
  • Count: This is the minimum number of particles that will always exist at the start and the maximum number of particles that are allowed to exist at once. Keep in mind that the maximum value also affects the amount of preallocated memory.
  • Delay: The emitter will pause for the given amount of time in milliseconds before it starts to emit particles. This setting must be activated in order to take effect.
  • Duration: This is the amount of time in milliseconds during which particles will be emitted.
  • Emission: This is the number of emitted particles per second.
  • Life: This is the amount of time in milliseconds until a particle is destroyed.
  • Life Offset: This is the amount of time in milliseconds that is used up of the particle's lifetime. This can be used, for example, to let a particle start at the middle of its life. All interim changes that would have normally been applied will be precalculated and set as the particle's starting state, such as the current displacement, the angle of rotation, and any change of color. This setting must be activated in order to take effect.
  • X Offset: This is the horizontal displacement in world units of the particle from its emitter's position. This setting must be activated in order to take effect.
  • Y Offset: This is the vertical displacement in world units of the particle from its emitter's position. This setting must be activated in order to take effect.
  • Spawn: This defines the shape used to spawn particles. The available shapes are point, line, square, and ellipse. By default, it is point.
  • Size: This is the particle's size in world units.
  • Velocity: This is the particle's speed in world units per second. This setting must be activated in order to take effect.
  • Angle: This is the particle's emission angle in degrees. This setting must be activated in order to take effect.
  • Rotation: This is the particle's local rotation angle in degrees. This setting must be activated in order to take effect.
  • Wind: This is the horizontal force applied to the particle in world units per second. This setting must be activated in order to take effect.
  • Gravity: This is the vertical force applied to the particle in world units per second. This setting must be activated in order to take effect.
  • Tint: This multiplies the colors of the particle's image with the set color. Therefore, using a monochrome-colored image is advisable for best tint results.
  • Transparency: This is the particle's alpha or transparency value. This allows the particle to be anything between fully opaque and fully translucent. Moreover, multiple changes during a particle's life allows the fade-in and fade-out effects.
  • Options: This contains the following five additional checkmarks to configure the emitter's behavior:
    • Additive: This option, if checked, will enable the additive blending mode.
    • Attached: This option, if checked, will allow the existing particles to follow the movement of its emitter.
    • Continuous: This option, if checked, will restart the emitter after its duration of time is over. Emitters that are not set to be continuous will only show up once at the beginning.
    • Aligned: This option, if checked, will add the particle's emission angle to its local rotation. This will turn the particle's image face in the same direction as it is moving.
    • Behind: This option, if checked, will set the corresponding marker flag to true. Otherwise, it is set to false. This is just a marker flag that can be queried by calling isBehind() to decide whether the emitter should be rendered behind or in front of something else.

Some of the emitter's properties contain charts. The x axis in a chart always represents the lifetime of a particle, while the y axis represents the percentage of each emitter property's actual value. A click on the chart adds a new node. Double-clicking on a node removes it again. Clicking-and-dragging a node moves it inside the chart.

The following screenshot shows a portion of the emitter properties:

Creating complex effects with particle systems

A chart can be expanded by clicking on the small + button next to it. The chart has a High and Low value, which defines the absolute values of 100 percent and 0 percent on the y axis in the chart. Most value fields have a > button next to them. Clicking on this button shows a second value field, which changes the single value to a ranged value. A ranged value has a minimum and a maximum value from which a random value is picked for each particle.

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

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