LibGDX World units and width and height..

I’ve got a tiledmap rendered and i’m rendering the player sprite over it…When I call my player.draw() it draws a texture region of the player specifying x,y,width,height…which is good, it’s rendering the correct place and moving around on the tiles…But the width and height are incorrect…My tiles are 16x16 and my player sprite is 24x32…so i’m getting a really blown up player sprite…How can I fix this?

This is my gamescreen class:


package com.psillicoder.farmGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class gameScreen implements Screen {

	gameClass game;
	
	
	
	private OrthographicCamera camera;
	private OrthogonalTiledMapRenderer renderer;
	private TiledMap map;
	private PlayerClass player;
	
	
	SpriteBatch batch;
	SpriteBatch guibatch;
	
	BitmapFont font;
	
	
	float unitScale = 1/16f;
	int tileSize = 16;
	
	
	
	 public gameScreen(gameClass game) {
		 this.game = game;
		 
		
	 }
	 
	 @Override
	 public void render(float delta) {
		 
		 Gdx.gl.glClearColor(0, 0, 0, 1);
			Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
			
		
			//camera.position.x = player.getPos().x - (Gdx.graphics.getWidth() / 2);
			//camera.position.y = player.getPos().y - (Gdx.graphics.getHeight() / 2);
			
			camera.position.x = player.getPos().x;
			camera.position.y = player.getPos().y;
			
			camera.update();
			
			
			batch.setProjectionMatrix(camera.combined);
			
			renderer.setView(camera);
			renderer.render();
			
			
			
			batch.begin();
			player.render(batch);	
			batch.end();
			
			guibatch.begin();
			font.draw(guibatch, "FPS: " + Gdx.graphics.getFramesPerSecond() , 10, Gdx.graphics.getHeight() - 10);
			font.draw(guibatch, "Player X: " + player.getPos().x + " | Player Y: " + player.getPos().y , 10, Gdx.graphics.getHeight() - 20);
			guibatch.end();

		processInput();
		
		 
	 }
	 
	 @Override
	 public void resize(int width, int height) {
		 
	 }
	 
	 @Override
	 public void show() {
		 
		 map = new TmxMapLoader().load("tdmap1.tmx");
		 
		 renderer = new OrthogonalTiledMapRenderer(map,unitScale);
		
		 
		 font = new BitmapFont();
		 
		 camera = new OrthographicCamera();
		 
		 camera.setToOrtho(false,40,40);
		 renderer.setView(camera);
		 camera.update();
		 
		 
		 
		 batch = new SpriteBatch();
		 guibatch = new SpriteBatch();
		 player = new PlayerClass(30, 30);
			
	 }
	 
	 
	 
	 public void processInput() {
		 if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) player.setPos((int)player.getPos().x - player.getSpeed(),(int)player.getPos().y);
		 
		 if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) player.setPos((int)player.getPos().x + player.getSpeed(),(int)player.getPos().y);
		 
		 if (Gdx.input.isKeyPressed(Input.Keys.UP)) player.setPos((int)player.getPos().x,(int)player.getPos().y + player.getSpeed());
		 
		 if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) player.setPos((int)player.getPos().x,(int)player.getPos().y - player.getSpeed());
		 
	 }
	 
	 @Override
	 public void hide() {
		dispose(); 
	 }
	 
	 @Override 
	 public void pause() {
		 
	 }
	 
	 @Override
	 public void resume() {
		 
	 }
	 
	 @Override
	 public void dispose() {
		 System.out.println("Disposed");
		 batch.dispose();
		 renderer.dispose();
		 map.dispose();
		 player.dispose();
		 font.dispose();
	 }
}

and my player class render method:


	public void render(SpriteBatch batch) {
		
		batch.draw(downRegion, playerX, playerY);
	
	}
	

Also, i’m confused as how camera.setToOrtho works. I’m using 40 as width and height and it seems to work okay…From what I understand, this is how many tiles I want to show on the screen?

Try scaling your player sprite by the world unit scale.

batch.draw(downRegion, playerX, playerY, 0, 0, 24, 32, 1.0f/16.0f, 1.0f/16.0f, 0);

Note that you might need to change the origin-parameters (the 4th and 5th arguments) if you need to center the sprite.

Yeah, I wrote that quickly while I was playing around with the texture regions and forgot. Already fixed.

I also just divided the width and height by tile size, but do I need to do that to everything? Or is there a better way?

You’re going to have to scale something, you could change the scale of the tilemap instead, but that means changing the graphical assets to compensate.

It sounds like you’ve already figured it out yourself, now you just need to figure out a way to make it convenient for you to apply across all your entities.

Yeah, not too bad I guess. Now just to figure out the movement system. The character moves around but freely. The plan is to center my sprite and make the base occupy one tile and use that to move…then it shouldn’t be too hard for collision detection.