# LibGDX: Issue with unitScale for Tiled and Box2D

I've got the following: I use a TiledMap (loaded from a .tmx), each tile 32x32 pixels in size and generate the Box2D bodies for collision from the TiledMap's ObjectLayer. That all works fine, no errors or issues. The only thing is a very strange behavior of the OrthogonalTiledMapRenderer: It gets a unitScale with the value 1 / 32 (because one unit is 32 pixels) as advised on the LibGDX GitHub wiki page. The same unit scale is used for Box2D. Thought in theory this should work just fine it renders the map very small (same is the case for the Box2D bodies, they're shown very small by the Box2DDebugRenderer):

Here is my code (more or less simplified):

public class MainScreen {
private OrthographicCamera camera;
private ScreenViewport viewport;
private SpriteBatch batch;
private World world;
private TiledMap tiledMap;
private OrthogonalTiledMapRenderer tiledMapRenderer;

public MainScreen() {
this.batch = new SpriteBatch();
this.world = new World(new Vector2(), true);
float unitScale = 1.0f / Constants.TILE_SIZE;

this.camera = new OrthographicCamera();
this.camera.setToOrtho(false, Gdx.graphics.getWidth() * unitScale,
Gdx.graphics.getHeight() * unitScale);
this.viewport = new ScreenViewport(this.camera);
this.tiledMapRenderer = new OrthogonalTiledMapRenderer(this.tiledMap,
unitScale, this.batch);

// Create bodies in world based on map
Collisions.create(this.world, this.map, unitScale);
}

public void render(float delta) {
// Clear the screen
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

this.world.step(delta, 6, 2);
this.camera.update();
this.tiledMapRenderer.setView(this.camera);
this.tiledMapRenderer.render();
}

public void resize(int w, int h) {
this.viewport.update(w, h);
}
}


I've got no idea why this is happening... Thanks in advance :)

• What is the picture showing? – Anko Sep 9 '15 at 15:35
• @Anko That's the screen content (including the rendered map and the debug renderer objects). – marcel Sep 9 '15 at 16:15
• Why do you have a unitScale on the renderer ? What does that achieve ? What you are doing is drawing everything 32 times smaller. Instead, you want to render the world as-is from tiled map. Why change it ? You don't want to be using a unitScale for the renderer. – dimitris93 Sep 9 '15 at 17:25
• @Shiro I read that on the wiki: github.com/libgdx/libgdx/wiki/Tile-maps#rendering-tiled-maps - There it says "The unit scale tells the renderer how many pixels map to a single world unit.". – marcel Sep 9 '15 at 17:29
• Huh... I think you are missing this.batch.setProjectionMatrix(this.camera.combined); before creating the renderer. You are passing the batch without first setting its projection matrix. – dimitris93 Sep 9 '15 at 17:33

I checked my ScreenViewport and seemingly the viewport somehow ignores the setToOrtho(false, width, height) and the method setUnitsPerPixel(unitScale) has to becalled first...
• I always use a FitViewport and I didn't have this problem before. As for the box2d bodies that were "staying small" it is because we only changed the unitScale to 1 just for the renderer only just to see what is wrong. I think that the screenshot you show was 32x32 cause your resolution was 1024x1024. So, 32 times smaller becomes only 32x32. – dimitris93 Sep 9 '15 at 20:06