Can I execute SpriteBatch using PerspectiveCamera in libGDX?

Can I use a promising camera to render my sprite lot?

All my sprites (those loaded with the same texture) look the same size, but I want the camera to be placed at the bottom of the screen at a certain height, so those sprites that are closer to the top of the screen look smaller. Right now it looks like the one on the left, but I want it to look like the right: enter image description here

+7
java opengl libgdx
source share
1 answer

Yes, although you probably need to adjust / scale the coordinates a bit (you can use spriteBatch.setTransformMatrix to do this in one call). Here is 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 () { } } 
+7
source share

All Articles