8

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 Answer 1

8

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 () {
    }
}
3
  • thanks works great. But my circular sprites used to look ver smooth, now they look jagged at the circumference? Also my touchpad which I was using from here bigerstaff.com/libgdx-touchpad-example shows up, but I cant use it, I mean the knob doesnt move and so I dont get any input from it
    – user494461
    Aug 2, 2013 at 9:12
  • why doesnt this perspectiver camera not change touchpad which is an actor in the stage? I use the same SpriteBatch for normal object and touchapd, but just the normal objects look as how I want to
    – user494461
    Aug 2, 2013 at 9:30
  • 2
    By default stage uses OthographicCamera, you can change the camera using stage.setCamera(...);. Also, stage uses camera.unproject to translate screen to stage coordinates, in which it assumes z=0. This might not work for you, so you'll probably want to extend Stage and override screenToStageCoordinates. To get a better (smooth) render, try to change your texture's minFilter/magFilter. I believe it's Nearest at default, try setting it to Linear. Tl;dr Stage is not designed for use with PerspectiveCamera, I would not recommend it.
    – Xoppa
    Aug 2, 2013 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.