LibGDX Box2D平铺地图工件

时间:2016-03-22 01:49:24

标签: java libgdx box2d artifacts tiled

我在LibGDX中渲染平铺地图时遇到问题。当我移动相机时出现伪影。

这是我的tileset,边距和间距(片段): tileset

当我移动相机时出现伪影: artifacts

这是我的渲染世界级。

private GameApp game;

private OrthographicCamera gameCamera;
private Viewport viewport;

private TiledMap map;
private TmxMapLoader mapLoader;
private OrthogonalTiledMapRenderer mapRenderer;

private MainCharacter character;

private World world;
private Box2DDebugRenderer worldRenderer;

public GameplayScreen(GameApp game) {
    this.game = game;

    gameCamera = new OrthographicCamera();

    viewport = new FitViewport(
            game.getDisplayWidth() / GameApp.PPM,
            game.getDisplayHeight() / GameApp.PPM,
            gameCamera
        );

    mapLoader = new TmxMapLoader();
    map = mapLoader.load("testmap1.tmx");
    mapRenderer = new OrthogonalTiledMapRenderer(map, 1 / GameApp.PPM);

    gameCamera.position.set(new Vector2(600 / GameApp.PPM, 200 / GameApp.PPM), 0);
    gameCamera.zoom -= 0.5;

    world = new World(new Vector2(0, -10), true);
    worldRenderer = new Box2DDebugRenderer();

    BodyDef bDef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fDef = new FixtureDef();
    Body body;

    for (MapObject mapObject : map.getLayers().get("obstacles").getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();

        bDef.type = BodyDef.BodyType.StaticBody;
        bDef.position.set(
                (rectangle.getX() + rectangle.getWidth() / 2) / GameApp.PPM, 
                (rectangle.getY() + rectangle.getHeight() / 2) / GameApp.PPM
        );

        body = world.createBody(bDef);

        shape.setAsBox(rectangle.getWidth() / 2 / GameApp.PPM, rectangle.getHeight() / 2 / GameApp.PPM);
        fDef.shape = shape;

        body.createFixture(fDef);
    }

    character = new MainCharacter(world);
}

@Override
public void show() {

}

public void handleInput(float delta) {
    if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
        character.getBody().applyLinearImpulse(new Vector2(0, 3.8f), character.getBody().getWorldCenter(), true);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && character.getBody().getLinearVelocity().x <= 1) {
        character.getBody().applyLinearImpulse(new Vector2(0.05f, 0), character.getBody().getWorldCenter(), true);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && character.getBody().getLinearVelocity().x >= -1) {
        character.getBody().applyLinearImpulse(new Vector2(-0.05f, 0), character.getBody().getWorldCenter(), true);
    }
}

public void update(float delta) {
    handleInput(delta);

    world.step(1/60f, 6, 2);

    gameCamera.position.x = character.getBody().getPosition().x;
    gameCamera.position.y = character.getBody().getPosition().y;

    gameCamera.update();
    mapRenderer.setView(gameCamera);
}

@Override
public void render(float delta) {
    update(delta);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    mapRenderer.render();
    worldRenderer.render(world, gameCamera.combined);
}

@Override
public void resize(int width, int height) {
    viewport.update(width, height);
}

...

我读到了tileset中的间距有帮助。但它仍然无法正常工作。

编辑:

TmxMapLoader.Parameters par = new TmxMapLoader.Parameters();
  par.textureMinFilter = TextureFilter.Nearest;
  par.textureMagFilter = TextureFilter.Nearest;

map = mapLoader.load("testmap1.tmx", par);

我编辑了我的代码,但仍无法正常工作。寻求解决方案几天。

EDIT2

我再试一次。代码:

atlasMapLoader = new AtlasTmxMapLoader();
testMap = atlasMapLoader.load("testmap1.tmx");

这会加载我的地​​图。我有“.txt”文件:

enter image description here

在Tiled Editor中,我将属性添加到名为“atlas”值“TilesetBig.txt”的地图中。

现在,地图不显示图块。 box2d的障碍仍然可见。我能做错什么?

最后我使用了OrthoCachedTiledMapRendered。

1 个答案:

答案 0 :(得分:2)

您应该使用最近纹理过滤器来设置您的图块集。

如果你使用线性,那么GL使用周围的像素来平均边界像素会导致像这样的毛刺。

Read this article获取有关LibGDX中纹理过滤器的更多信息。