I'm currently trying to learn the LibGDX game engine for java. I'm using a Tiled map, with each tiled being 32*32 pixels. I also have a character sprite with size 48*64. In my game, I'm defining one unit as one tile from the map. For some reason, my map gets rendered correctly, but the player sprite gets rendered abnormally large instead of in its expected size of roughly 2 square units.
I've tried searching for similar issues online, but cannot find anything just like my issue. I know it has something to do with the scaling I'm using, but I don't know of a good way to solve it.
One way is of course to simply scale down the player sprite when drawing it with the batch.draw() function, but I feel like that shouldn't be necessary.
@Override public void create () {
world = new World(new Vector2(0, -20), true); //Create Box2d world
debugRenderer = new Box2DDebugRenderer();
player1 = new Player();
camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT); //Create the camera I'm using, with width = height = 20
map = new TmxMapLoader().load("maps/Maptest.tmx"); //Load a map created in Tiled
this.mapObjects = map.getLayers().get(0).getObjects();
ObjectMapper.createShapes(this.mapObjects, this.world); //Load shapes from map for collision management
ObjectMapper.setSpawn(player1, mapObjects, world); //Set spawnpoint from object in the Tiled map
batch = new SpriteBatch();
renderer = new OrthogonalTiledMapRenderer(map, 1/PPT); //Create a tiled map renderer with unit scale 1/32
camera.update();
}
@Override
public void render () {
player1.update(); //Update player position
camera.position.set(player1.getX(), player1.getY(), 0); //Set position to players position
camera.update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera); //Set renderer's view to camera view
renderer.render();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(player1.getSprite(), player1.getX()-player1.getSprite().getWidth()/2, player1.getY()-player1.getSprite().getHeight()/2); //Draw player sprite
batch.end();
System.out.println(player1.getX() + " " + player1.getY());
world.step(1/60f, 6, 2); //Iterate one step with Box2d world
debugRenderer.render(world, camera.combined); //Draw shapes
long javaHeap = Gdx.app.getJavaHeap();
long nativeHeap = Gdx.app.getNativeHeap();
}
@Override
public void resize (int width, int height) {
camera.viewportWidth = WORLD_WIDTH; // 20 units
camera.viewportHeight = (float)WORLD_HEIGHT*height/width;
camera.update();
}
I expect the sprite to be about the same size as the players hitbox (the circleshape) which has a radius of one unit. Instead, it becomes very large:
https://i.gyazo.com/6d814d10cd5f9bec322b601d538cb9ee.png
While the full sprite looks like this:
https://i.gyazo.com/03178769d4a3b4beaba1612c76d37386.png
Thanks in advance:)