Box2d Light, pooled Particle Effects and ParticleEmitter2d

Objective

Shows how to:

  • Use Box2d Light with an ambient and projector
  • Use pooled Particle Effect to show – to reuse a particle Effect multiple times
  • Use ParticleEmitterBox2D to have your Particle Effect be affected by other Box2D bodies.

Concepts used in the code

  • Importing a Shape as Body (The gear wheel in the middle).

In this tutorial we start from the previous sample in which we import the gear wheel and generate random ball.

We Create a class BallGenerator who takes care of this action and create it as a singleton. As part of the Ball Generator we create a ParticleEffectPool so that the same explosion effect can be recylecled for all the collisions.We start a ParticleEffect for each collision.

We create two spotligth and place them at the top left and top right to illustrate how shadows are cast using Box2D Ligth.

We create a Continous Particle Emitter and modify it so that the Particles can be influenced by the other bodies. We do this by replacing the Emitter within the Particle Effect with a ParticleEmitterBox2D object.

Box2d Light

creation


rayHandler = new RayHandler(world);
rayHandler.setAmbientLight(0.1f, 0.1f, 0.1f, 1f);
rayHandler.setBlurNum(3);
PointLight pl = new PointLight(rayHandler, 128, new Color(0.2f,1,1,1f), 10,-5,2);
PointLight pl2 = new PointLight(rayHandler, 128, new Color(1,0,1,1f), 10,5,2);

rayHandler.setShadows(true);
pl.setStaticLight(false);
pl.setSoft(true);

render


rayHandler.updateAndRender();

Code

Full Code available for download here

Main class

public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
private World world;
private Stage stage;
private Box2DDebugRenderer debugRenderer;

private RayHandler rayHandler;

@Override
public void create () {
Gdx.app.setLogLevel(Application.LOG_DEBUG);
world = new World(new Vector2(0, -3), true);
world.setContactListener(new B2dContactListener());
batch = new SpriteBatch();

Gdx.input.setInputProcessor(stage);
float ratio = (float)(Gdx.graphics.getWidth()) / (float)(Gdx.graphics.getHeight());

stage = new Stage(new ScreenViewport());
stage.getCamera().position.set(0,0,10);
stage.getCamera().lookAt(0,0,0);
stage.getCamera().viewportWidth = 10;
stage.getCamera().viewportHeight = 10/ratio;
debugRenderer = new Box2DDebugRenderer();

GearActor gearActor = new GearActor(world,0,0,3,3);
stage.addActor(gearActor);

new WindowsFrame(world,stage.getCamera().viewportWidth,stage.getCamera().viewportHeight);

rayHandler = new RayHandler(world);
rayHandler.setAmbientLight(0.1f, 0.1f, 0.1f, 1f);
rayHandler.setBlurNum(3);

PointLight pl = new PointLight(rayHandler, 128, new Color(0.2f,1,1,1f), 10,-5,2);
PointLight pl2 = new PointLight(rayHandler, 128, new Color(1,0,1,1f), 10,5,2);

rayHandler.setShadows(true);
pl.setStaticLight(false);
pl.setSoft(true);
BallGenerator.getInstance().setup(stage,world);
stage.addActor(new FireEmitter(world));
}

@Override
public void render () {
Gdx.gl.glClearColor(0f, 0f, 0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

stage.act();
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
stage.draw();

//debugRenderer.render(world, stage.getCamera().combined);

BallGenerator.getInstance().emit();
rayHandler.setCombinedMatrix(stage.getCamera().combined,0,0,1,1);
rayHandler.updateAndRender();
}

@Override
public void dispose () {
batch.dispose();
rayHandler.dispose();
}
}

Advertisements