Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx - Tiledmap rendered tiles disappearing from screen

Tags:

java

libgdx

tiled

I'm creating a side-scrolling platformer and I'm using my own Tiled map. I'm rendering it using the OrthogonalTiledMapRenderer, but after adding background images I noticed that they disappear from the screen too soon. On the first picture you can see the background giant trees still being rendered, and in the TiledMap the first background tile horizontally ends exactly where the ladder starts, and then the same picture is added (so it's basically one image pasted multiple times on the level - second picture).

enter image description here enter image description here

However, even before reaching the first image's ending point, it disappears, which looks like this:

enter image description here

Anybody can help with that? Here is the code for the rendering:

OrthogonalTiledMapRenderer mapRenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.PPM);
OrthographicCamera camera = new OrthographicCamera();
float width = Constants.VIEWPORT_WIDTH * camera.zoom * 2;
float height = Constants.VIEWPORT_HEIGHT * camera.zoom * 2;

mapRenderer.setView(camera.combined, cameraX, cameraY, width, height);
Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f, 0xff / 255.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapRenderer.render();

The floats are updated with camera position.

cameraX = camera.position.x - camera.viewportWidth * camera.zoom;
cameraY = camera.position.y - camera.viewportHeight * camera.zoom;

Camera follows the player and is clamped to the borders of the map. Nothing too fancy, I also tried mapRenderer.setView(camera) with the same results.

like image 620
Deso2121 Avatar asked Jan 21 '26 09:01

Deso2121


1 Answers

It looks like this is caused by optimistic culling of tiles on the renderer side, which breaks when tiles are overly large.

One way to fix this would be to not use a single large tile for these backgrounds, but rather to add the background as a tileset using the same tile size as your map. You can still easily place the whole image at once by using click+drag to select all the tiles in this tileset.

Another option may be to place this image as a tile object on an Object Layer instead, but I don't know if libgdx will render those for you by default.

like image 128
Thorbjørn Lindeijer Avatar answered Jan 22 '26 23:01

Thorbjørn Lindeijer