Advertisement

LibGDX texture rendering larger than intended

Started by November 27, 2018 09:35 AM
1 comment, last by Zakwayda 5 years, 9 months ago

I'm trying to render a player texture on top of a TiledMap in LibGDX, my issue is that the map renders correctly, but the player texture is way larger than it should be. I'm sure it has something to do with my lack of understanding of the camera/viewport system, but I can't quite figure it out. 

game.unitsPerTile = 32 in this case. The texture in question is 32x64.

 


public class GameScreen implements Screen {
	
	final Engine2D game;
	private OrthographicCamera camera;
	static final int GAME_WIDTH = 100;
	static final int GAME_HEIGHT = 100;
	
	private TiledMap map;
	private OrthogonalTiledMapRenderer mapRenderer;
	private Player player;
	
	public GameScreen(Engine2D game) {
		this.game = game;

		float screenWidth = Gdx.graphics.getWidth();
		float screenHeight = Gdx.graphics.getHeight();
		float aspectRatio = screenHeight/screenWidth;
		
		camera = new OrthographicCamera(30,30*aspectRatio);
		map = new TmxMapLoader().load("Map1.tmx");
		mapRenderer = new OrthogonalTiledMapRenderer(map,1/game.unitsPerTile);
		
		TiledMapTileMapObject startPoint = (TiledMapTileMapObject)map.getLayers().get("Objects").getObjects().get("startZone");
		player = new Player();
		player.setPositionX(startPoint.getX()/game.unitsPerTile);
		player.setPositionY(startPoint.getY()/game.unitsPerTile);
	}

	@Override
	public void render(float delta) {
			Gdx.gl.glClearColor(0, 0, 0, 1);
			Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
			game.batch.setProjectionMatrix(camera.combined);

			camera.position.x = player.getPositionX() + camera.viewportWidth/2;
			camera.position.y = player.getPositionY() + camera.viewportHeight/2;
			// tell the camera to update its matrices.
			camera.update();
			
			mapRenderer.render();
			mapRenderer.setView(camera);
			
			
			// begin a new batch and draw the bucket and
			// all drops
			
			game.batch.begin();
			game.batch.draw(player.getTexture(),player.getPositionX(),player.getPositionY());
			game.batch.end();

	}
}

 

I'm not that familiar with LibGDX, so I'm mostly guessing here. But, it looks to me like you're creating an orthographic projection with dimensions of around 30x30 (factoring in aspect ratio), and then rendering the player texture at a size of 32x64. If that's the case, I'd expect the player texture to occupy most of the screen (in other words, to be too large, as you describe).

I'm also wondering if render() and setView() are being called in the right order on mapRenderer, or if it should be the reverse. (Again, I'm guessing here, as I don't know exactly what those functions do.)

You said the tile map is rendering how you want. Even if swapping render() and setView() is the correct thing to do conceptually (I'd consult the documentation about that), you may find that it causes the tile map to no longer render as you expect. It may be though that there are multiple errors that are currently cancelling each other out (for example, I'm a little suspicious of '1/game.unitsPerTile').

Again, I'm just guessing on some of this, but maybe it'll get you pointed in the right direction.

This topic is closed to new replies.

Advertisement