6

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();
}
Roman K
  • 3,309
  • 1
  • 33
  • 49
  • Is your game 2d or 3d? What does "using the Z coordinate" mean? That the particles render at different sizes on the screen? Can you show a screenshot of what you have? – P.T. Jun 02 '12 at 19:43
  • My game is 3d, but ParticleEffect of libgdx is for 2d (uses only X and Y, no Z) because it uses Sprites(2d) internally and SpriteBatch for drawing. I want the effect appear partially behind of near objects and in front of far objects with respect of the perspective. – Roman K Jun 04 '12 at 08:01

1 Answers1

9

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.

badlogic
  • 3,049
  • 1
  • 21
  • 12
  • thank you, that was the final hint! i've updated the question with the working code. thank you for libgdx by the way! – Roman K Jun 14 '12 at 06:42