LibGDX Box2D 平铺贴图工件
LibGDX Box2D Tiled map artifacts
我在 LibGDX 中渲染分块地图时遇到问题。当我移动相机时出现伪影。
这是我的带有边距和间距的图块集(片段):
tileset
当我移动相机时出现伪像:
artifacts
这是我的渲染世界class。
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);
}
...
我读到图块集中的间距有帮助。但是还是不能正常使用。
编辑:
TmxMapLoader.Parameters par = new TmxMapLoader.Parameters();
par.textureMinFilter = TextureFilter.Nearest;
par.textureMagFilter = TextureFilter.Nearest;
map = mapLoader.load("testmap1.tmx", par);
我编辑了我的代码,但仍然无法正常工作。求了几天解决方法。
编辑2
我再试一次。代码:
atlasMapLoader = new AtlasTmxMapLoader();
testMap = atlasMapLoader.load("testmap1.tmx");
这加载了我的地图。我有“.txt”文件:
在 Tiled Editor 中,我将 属性 添加到名为 "atlas" 的地图中,值 "TilesetBig.txt"。
现在,地图不显示图块。 box2d 的障碍仍然可见。我能做错什么吗?
最后我使用了 OrthoCachedTiledMapRendered。
您应该使用设置的 最近 纹理过滤器打包您的图块集。
如果您使用的是线性,则 GL 会使用周围的像素平均边界像素,这会导致出现这样的故障。
Read this article 在 LibGDX 中获取有关纹理过滤器的更多信息。
我在 LibGDX 中渲染分块地图时遇到问题。当我移动相机时出现伪影。
这是我的带有边距和间距的图块集(片段): tileset
当我移动相机时出现伪像: artifacts
这是我的渲染世界class。
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);
}
...
我读到图块集中的间距有帮助。但是还是不能正常使用。
编辑:
TmxMapLoader.Parameters par = new TmxMapLoader.Parameters();
par.textureMinFilter = TextureFilter.Nearest;
par.textureMagFilter = TextureFilter.Nearest;
map = mapLoader.load("testmap1.tmx", par);
我编辑了我的代码,但仍然无法正常工作。求了几天解决方法。
编辑2
我再试一次。代码:
atlasMapLoader = new AtlasTmxMapLoader();
testMap = atlasMapLoader.load("testmap1.tmx");
这加载了我的地图。我有“.txt”文件:
在 Tiled Editor 中,我将 属性 添加到名为 "atlas" 的地图中,值 "TilesetBig.txt"。
现在,地图不显示图块。 box2d 的障碍仍然可见。我能做错什么吗?
最后我使用了 OrthoCachedTiledMapRendered。
您应该使用设置的 最近 纹理过滤器打包您的图块集。
如果您使用的是线性,则 GL 会使用周围的像素平均边界像素,这会导致出现这样的故障。
Read this article 在 LibGDX 中获取有关纹理过滤器的更多信息。