Florent Descroix
Florent Descroix

Reputation: 611

Libgdx TiledMap bug in render

I do a Mario like game with the libgdx library.

All works fine but sometime (especially when the camera goes fast) my TileMap has a little bug during the render.

A picture worth thousand word, so here it is : http://postimg.org/image/4tudtwewn/

I have tried to increment FPS, but there is no change. I have no idea where that is come from.

Here is my code :

public void show() {
    TmxMapLoader loader = new TmxMapLoader();
    this.plan = loader.load("maps/level-"+this.world+"-"+this.level+".tmx");
    this.renderer = new OrthogonalTiledMapRenderer(this.plan);
    ...

public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    this.renderer.render();// rendu de la carte
    Batch batch = this.renderer.getSpriteBatch();
    ...

Upvotes: 4

Views: 731

Answers (3)

Lake
Lake

Reputation: 4092

This happens when your Camera's position is not perfectly aligned with screen-space coordinates (pixels). This results in some sprites being rounded to the next pixel while some other (that were connected to those) being rounded to the previous one, resulting in visible ugly glitches.

The easiest fix I could come up with is making sure that the Camera position is always perfectly aligned with screen-space coordinates.

public class TileMapCamera extends OrthographicCamera {

    // Map tile size to round to
    private int tileSize;

    /**
     * Create a pixel-perfect camera for a map with the specified tile size
     * @param tileSize
     */
     public TileMapCamera(int tileSize){
        this.tileSize = tileSize;
     }

     @Override
     public void update(){
         // Round position to avoid glitches
         float prevx = position.x;
         float prevy = position.y;
         position.x = (int)(position.x * tileSize) / (float)tileSize;
         position.y = (int)(position.y * tileSize) / (float)tileSize;
         super.update();
         position.set(prevx, prevy, 0);
    }
}

This works for a tile-based coordinate viewport:

mapViewport = new FitViewport(16, 15, new TileMapCamera(map.getProperties().get("tilewidth", Integer.class)));

If you're working with pixel-based coordinate viewports, you should round the camera position to the nearest integer instead.

Upvotes: 1

Angel Angel
Angel Angel

Reputation: 21756

If the problem you are referring to, is the spacing you can fix when you import the tileset as it says Tenfour04

enter image description here

add or change pixel padding.

Upvotes: 0

ADDER
ADDER

Reputation: 404

I think its about the filtering. This will help:

TiledMapRenderer Artifact

Upvotes: 0

Related Questions