8
votes

Can I use perspective camera to render my sprite batch?

Aall my sprites(those loaded with same texture) look the same size, but I want the camera to be placed at bottom of screen at some height, so those sprite which are positioned near top of the screen look smaller. Right now it's looking like the one on the left, but I want it to look like the one on the right: enter image description here

1

1 Answers

8
votes

Yes, although you probably need to adjust/scale your coordinates a bit (you can use spriteBatch.setTransformMatrix to do that in one call). Here's a small example:

public class SpriteBatch3DTest extends GdxTest {
   PerspectiveCamera cam;
   CameraInputController camController;
   SpriteBatch spriteBatch;
   Texture texture;

   @Override
   public void create () {
       cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
       cam.position.set(0f, 8f, 8f);
       cam.lookAt(0,0,0);
       cam.near = 0.1f;
       cam.far = 300f;
       cam.update();

       spriteBatch = new SpriteBatch();

       camController = new CameraInputController(cam);
       Gdx.input.setInputProcessor(camController);
       texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
   }

   @Override
   public void render () {
    camController.update();
    spriteBatch.setProjectionMatrix(cam.combined);

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    spriteBatch.begin();
    spriteBatch.draw(texture, -5f, -5f, 10f, 10f);
    spriteBatch.end();
   }

   @Override
   public void dispose () {
       spriteBatch.dispose();
       texture.dispose();
   }

    public boolean needsGL20 () {
        return true;
    }

    public void resume () {
    }

    public void resize (int width, int height) {
    }

    public void pause () {
    }
}