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();
}
}