Skip to content

Particle Effects

CodePilot58 edited this page Oct 18, 2021 · 10 revisions

Particle effects are supported natively by some very useful aspects of LibGDX. Some good resources are listed at the bottom.

There are

  1. Create a particle effect file using the LibGDX 2D Particle Editor. (See Using the editor).
  2. Add the particle effect file and particle image to 2021-studio-1/source/core/assets/... so that they can be loaded. Ensure the particle image is made into a .atlas file with something like the GDX Texture packer.
  3. Open the effect file: FileHandle effectData = Gdx.files.internal("pathToFile") (ResourceService cannot handle FileHandles)
  4. Open the particle image as an atlas: TextureAtlas textureAtlas = ServiceLocator.getResourceService("pathToAtlas")
  5. Load the particle effect using ParticleFactory.createParticleEffect(effect file path, particle image atlas path). This will return a new particle effect that has been loaded.
  6. This works best in game when loaded into a component to follow the entity-component design pattern. In the component's render(SpriteBatch batch) method, putting the following lines will render the particles in the position of the entity owning it:
particleEffect.setPosition(entity.getPosition().x, entity.getPosition().y);
particleEffect.update(Gdx.graphics.getDeltaTime());
particleEffect.draw(batch);
  1. To start the particle emission, call particleEffect.start() and it will start. Depending on whether you set continuous to true or false in the Particle Editor, this may play continuously or just once.

See ParticleEffectRenderComponent.java for an example.

That is the process. I have tried to mirror the Factory design pattern by using creating ParticleFactory. I have also tried to follow the Service Locator design pattern by keeping ServiceLocator's ResourceService functionality, but I found that AssetManager in LibGDX does not handle the FileHandle that the effect data is kept in, so effect data is loaded without the ResourceManager.

Notes

Using the editor

I found it only worked when I cloned LibGDX source, opened the run.gradle file in Intillej and followed the third method here. Make sure to run ./gradlew fetchNatives in the repo's directory from terminal like is specified here.

In terms of actually creating the particle effects, these tutorials go over it far better than I could. The editor uses particle images as .png files, so just make sure to pack into an atlas and use that in game.

Using ParticleEffectRenderComponent.java

This component plays the given particle effect when the correct thing collides with it. The required arguments are particleEffect, and targetLayer. Note that it scales the particle effect by a 0.02 multiple because the particles are initially very large.

particleEffect: This is the particle effect created by ParticleFactory.

targetLayer: On collision with this component, if the colliding entity has PhysicsLayer equal to targetLayer, the component will play the effect.

Using ParticleFactory

This factory follows the Factory design pattern.

Class diagram for Particle Factory

ParticleFactory.createParticleEffect(String effectData, String textureAtlas)

This returned particle effect will have an effect given by the file at effectData, where each particle is an image of textureAtlas. Both of these argumetns are file paths.

The given particle effect will have had been loaded and is ready to start() and render.

Note that the effectData does not use the ResourceService because the service does not load FileHandle types.

Sequence diagram for Particle Factory

Size

The size of particle effects seems to be quite large when not modified. So you may find it useful to scale the size of the particle effect using:

particleEffect.scaleEffect(0.02f);

Resources

LibGDX Wiki: Particle Editor and making effects

LibGDX Wiki: ParticleEffect class

Implementing particle effects

Full Particle Effects tutorial

Table of Contents

Home

Student Documentation

Design Document

Graphical Design
User Testing
Sound and Music
Code Design / Documentation
Enemies

Game Engine

Getting Started

Technical Aspects
Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics
Game Screens and Areas

Terrain

Map Generation

Concurrency & Threading

Settings

Troubleshooting

Common bugs

MacOS Setup Guide

Clone this wiki locally