Libgdx tiledmap coordinates to screen coordinates?

Hey guys, I hope the title for this topic is correct…

Some days ago I started playing around with libgdx with the help of some tutorials. I’m not quite sure if I understand the concepts of PPM, viewports and the libgdx orthographic camera correctly.

My player body is always “spawning” at 0,0.

mPlayer = ShapeFactory.createRectangle(new Vector2(0,0), new Vector2(16,16), BodyType.DynamicBody, mWorld, 100f)

Parameters are: position, size, bodytype, world, density, just for clarification :slight_smile:

this is my camera / maploading / rendering setup:


mB2dr = new Box2DDebugRenderer();
mCamera = new OrthographicCamera(Gdx.graphics.getWidth() / 32(PPM), Gdx.graphics.getHeight() / 32(PPM));
mViewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), mCamera);
map = new TmxMapLoader().load("maps/test.tmx");
tmr = new OrthogonalTiledMapRenderer(map);

TiledObjectUtil.parseTiledObjectLayer(mWorld, map.getLayers().get("collision-layer").getObjects());
TiledObjectUtil.createMapBoundaries(mWorld, map);

and this is my draw method:


    private void draw() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        tmr.setView(mCamera);
        tmr.render();
        mBatch.setProjectionMatrix(mCamera.combined);
        mB2dr.render(mWorld, mCamera.combined);
    }

What I’m not understanding is the position of the tiledmap rendering? So my player is at 0,0 it looks like this if I render my tiledmap:

My assumption was that the rectangle should be at the bottom left corner of the tiledmap because the tiledmap is also rendered at 0,0? Or is this because the position is applied to the center of the rectangle? I could set the position of the rectangle to 16,16 and it would fit to the bottom left of the map. How ever I’m not certain wether this is the right way or whether there is just a misunderstanding in my mind. I think this is because my rectangle has a size of 16,16 but rendered in my game it fits a 32,32 big tile? Could anyone help me out here? I’m not really sure how I should use “meters” instead of pixels when developing my game.

I think my misunderstanding is related to the concept of PPM in box2d/libgdx since I’m not really sure at what operations I have to apply PPM. I read somewhere that I’m fine if I only apply this conversion in my camera method? Can anyone point me towards a helpful tutorial or give me some hints? Much appreciated!

I think this was quite good (https://xoppa.github.io/blog/pixels/) but I’m not sure if I understood everything correctly and where my inconsistency comes from.

Hi, can you post the link for that tutorials, I don’t understeand how the camera is set here, but it looks like, the black area is where negative coordinates are

PS

What gona happen if you do it like this?

mCamera = new OrthographicCamera;
mCamera.setToOrtho(false, viewWidth, viewHeight);

mViewport = new FitViewport(viewWidth, viewHeight, mCamera);

Where viewWidth and viewHeight, for example 1024 x 720

Im pretty sure in LibGDX the camera is by default positioned with the bottom left corner at 0, 0. So you have to manipulate the viewport to make the camera at the actual 0,0 (where you want it). If you wanna take a look, my game screen code is here: https://github.com/IanFell/GameTemplate/blob/master/MyGame-core/src/screens/GameScreen.java

This class extends my Screens class (so go back and look at that to set it up) but it’s centered on the player.

And yeah, camera/viewport/game coordinates are confusing as shit.

Thanks for the replies guys! I think I figured it out a little bit more. Your Github link was quite helpful!
Eventhough I’m quite sure that I did not understand the camera / viewport etc. stuff in full depth lol.

I have something else to that, link shared by @VaTTeRGeR https://gamedev.stackexchange.com/questions/120722/screensize-vs-worldsize. It looks like same/similar problem, and there’s some details and explanation(I forgot about it)). I’ll leave it here for someone else, who will seek that.

To be honest, I didn’t either. But it works for now lol.

Hi,i think your OrthographicCamera have some problems when it initialize.
This is a Example for me:


public class FollowCamera{
	private OrthographicCamera gameCam;
	private Viewport gamePort;

	public FollowCamera(){
		//create camera used to follow hero through camera world
		gameCam = new OrthographicCamera();
		//create a FitViewport to maintain virtual aspect ratio despite screen size
		gamePort = new FitViewport(Configs.viewportWidth / Configs.PPM, Configs.viewportHeight / Configs.PPM, gameCam);
	}

	public void update(Vector2 pos, float zoom){
		// change gameCam zoom
		gameCam.zoom = zoom;
		//attach our gameCam to our players coordinate
		gameCam.position.set(pos.x, pos.y, 0);
		//update our gameCam with correct coordinates after changes
		gameCam.update();
	}

	public OrthographicCamera getCamera(){
		return gameCam;
	}

}


	// Virtual Screen size and Box2D Scale(Pixels Per Meter)
	public static final int viewportWidth = 400;//Pixels 
	public static final int viewportHeight = 200;//Pixels 
	public static final float PPM = 100;

Configs.viewportWidth / Configs.PPM --> Viewport width that the camera can see. Unit Meter.
Configs.viewportHeight / Configs.PPM --> Viewport height that the camera can see. Unit Meter.
Virtual Screen and Physical Screen, they’re not a same concept.
Virtual Screen -> Unit Meter.
Physical Screen -> Unit Pixels.
Element’s position in the game must be use the ‘Meter’ such as Hero’s position.
Only you are ready to render them, you can transform the ‘Virtual Position in Meter’ into ‘Physical position in Pixels’;