Libgdx TiledMap bug in render

有些话、适合烂在心里 提交于 2019-12-10 16:48:10

问题


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();
    ...

回答1:


I think its about the filtering. This will help:

TiledMapRenderer Artifact




回答2:


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

add or change pixel padding.




回答3:


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.



来源:https://stackoverflow.com/questions/26908317/libgdx-tiledmap-bug-in-render

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!