0
\$\begingroup\$

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):

enter image description here

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.tiledMap = (new TiledMapLoader()).load("level/test-level.tmx");
        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 :)

\$\endgroup\$
  • \$\begingroup\$ What is the picture showing? \$\endgroup\$ – Anko Sep 9 '15 at 15:35
  • \$\begingroup\$ @Anko That's the screen content (including the rendered map and the debug renderer objects). \$\endgroup\$ – RegisteredUser Sep 9 '15 at 16:15
  • \$\begingroup\$ 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. \$\endgroup\$ – dimitris93 Sep 9 '15 at 17:25
  • \$\begingroup\$ @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.". \$\endgroup\$ – RegisteredUser Sep 9 '15 at 17:29
  • \$\begingroup\$ 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. \$\endgroup\$ – dimitris93 Sep 9 '15 at 17:33
2
\$\begingroup\$

I just stumbled upon the answer:

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 never saw an example in which a viewport was used together with box2d, tilemap and unitScale, so I didn't even know about that method.

The greatest thanks to @Shiro for his help! :)

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – dimitris93 Sep 9 '15 at 20:06
  • \$\begingroup\$ @Shiro The problem with the box2d bodies is now solved too, everything is now zoomed and displayed properly :) So in fact there was nothing wrong with the code, but one call was missing... \$\endgroup\$ – RegisteredUser Sep 9 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.