Skip to content

Particle Effects

CodePilot58 edited this page Oct 13, 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 with particleEffect = new ParticleEffect(); and particleEffect.load(effectData, textureAtlas);
  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, put the following lines:
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 for an example.

That is the process. I have tried to mirror the Factory design pattern by using the ObstacleFactory, I didn't think particles deserved their own factory since at the moment there is only one particle effect. 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 effectData, textureAtlas, and targetLayer. Note that it scales the particle effect by a 0.02 multiple because the particles are initially very large.

effectData: This is the particle effect data file saved with the Particle Editor and opened in game as a FileHandle using Gdx.files.internal("pathToFile"). ResourceManager does not seem to be able to handle FileHandles. It is the information telling the game how to play the effect.

textureAtlas: This is the image each particle will look like. Must be a fully white, probably small image packed into an atlas file and opened using the resource manager: ServiceLocator.getResourceService().getAsset("pathToAtlas", TextureAtlas.class)

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

So, an example particle effect is given by ObstacleFactory.createWallParticles:

public static Entity createWallParticles(String particleEffectPath, String particleAtlasPath) {
    FileHandle effectData = Gdx.files.internal("particles/rainbow_spread_2");
    TextureAtlas particleImage = ServiceLocator.getResourceService()
            .getAsset("particles/particles.atlas", TextureAtlas.class);

    return new Entity()
            .addComponent(new PhysicsComponent())
            .addComponent(new HitboxComponent())
            .addComponent(new ParticleEffectRenderComponent(effectData, particleImage,
                    PhysicsLayer.PLAYER));
}

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