Search code examples
javatransformationlibgdxparticle-system

libgdx particle system Z coordinate


I want to render a particle effect in 3D using the Z coordinate. I've tried to implement a own ParticleEffect using Decals instead of Sprites without success.

Is there any other way to render a ParticleEffect using the Z coordinate? Maybe by manipulating the transformation Matrix of the SpriteBatch?

Update:

working code

// update projection each frame since my camera is moving
spriteBatch.setProjectionMatrix(camera3d.projection);

for (ParticleEffect effect : effects){
    spriteBatch.setTransformMatrix(camera3d.view);
    spriteBatch.getTransformMatrix().translate(x,y,z); // different for each effect
    spriteBatch.getTransformMatrix().scale(0.1f,0.1f,0.1f); //optional
    spriteBatch.begin();

    effect.draw(spriteBatch, delta);

    spriteBatch.end();
    spriteBatch.getTransformMatrix().idt();
}

Solution

  • If your 3D effect is a parallax effect, meaning your particles face the camera perpendicularily, you can indeed set the transformation matrix of the SpriteBatch

    batch.getTransformMatrix().idt().translate(0, 0, z);
    batch.begin();
    ... do your rendering here
    batch.end();
    // reset the matrix, so you can use the batch for other stuff
    batch.idt();
    

    For a perspective effect you'll also have to use perspective projection. The easiest way to cope with this requirement is to use a PerspectiveCamera instead of an OrthographicCamera.