简体   繁体   English

在渲染中的Libgdx TiledMap错误

[英]Libgdx TiledMap bug in render

I do a Mario like game with the libgdx library. 我使用libgdx库做了一个类似Mario的游戏。

All works fine but sometime (especially when the camera goes fast) my TileMap has a little bug during the render. 一切正常但有时(特别是当相机快速运行时)我的TileMap在渲染过程中有一点小错误。

A picture worth thousand word, so here it is : http://postimg.org/image/4tudtwewn/ 一张价值千言万语的图片,所以这里是: http//postimg.org/image/4tudtwewn/

I have tried to increment FPS, but there is no change. 我试图增加FPS,但没有变化。 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();
    ...

I think its about the filtering. 我认为它是关于过滤的。 This will help: 这将有助于:

TiledMapRenderer Artifact TiledMapRenderer神器

If the problem you are referring to, is the spacing you can fix when you import the tileset as it says Tenfour04 如果您指的是问题,那么在导入tileset时可以修复的间距如Tenfour04所示

在此输入图像描述

add or change pixel padding. 添加或更改像素填充。

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. 我能想到的最简单的解决方法是确保Camera位置始终与屏幕空间坐标完美对齐。

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. 如果您正在使用基于像素的坐标视口,则应将相机位置舍入到最接近的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM