gpt4 book ai didi

java - LibGDX Box2D 平铺贴图工件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:15:38 24 4
gpt4 key购买 nike

我在 LibGDX 中渲染分块 map 时遇到问题。当我移动相机时会出现伪像。

这是我的带有边距和间距的图 block 集(片段): 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);

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


编辑2

我再试一次。代码:

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

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

enter image description here

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

现在, map 不显示瓦片。 box2d 的障碍仍然可见。我能做错什么吗?


最后我使用了 OrthoCachedTiledMapRendered。

最佳答案

您应该使用正在设置的Nearest 纹理过滤器打包您的图 block 集。

如果您使用的是线性,GL 会使用周围的像素来平均边界像素,这会导致出现这样的故障。

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

关于java - LibGDX Box2D 平铺贴图工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36144867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com