简体   繁体   English

LibGDX 平铺地图在相机移动时闪烁

[英]LibGDX tile map flickering when camera moving

I'm attempting to make a short game for my family for christmas using libgdx and when going forward through the level the edge of the screen flickers but when going backwards there is no flickering and it's quite annoying.我正在尝试使用 libgdx 为我的家人制作一个圣诞节小游戏,当前进通过关卡时,屏幕边缘会闪烁,但是当向后时,没有闪烁,这很烦人。

Here is a demo of what I mean.是我的意思的演示。

Also, here is my code:另外,这是我的代码:

if (direction == "right") {
    body.setTransform(body.getPosition().x + 1 / PPM, body.getPosition().y, body.getAngle());
    b2dCam.position.x += (1 / PPM);
    camera.position.x += (1*(PPM/(8/2)));
} else if (direction == "left") {
    b2dCam.translate(-1 / PPM, 0);
    camera.translate(-1*(PPM/(8/2)), 0);
}

tmr.setView(camera);
tmr.render();
camera.update();
b2dCam.update();
b2dr.render(world, b2dCam.combined);

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

Any help would be greatly appreciated, thanks.任何帮助将不胜感激,谢谢。

I just solved this issue by calling camera.update before everything else so instead of:我只是通过在其他所有事情之前调用 camera.update 来解决这个问题,而不是:

tmr.setView(camera);
tmr.render();
camera.update();
b2dCam.update();
b2dr.render(world, b2dCam.combined);

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

I now use:我现在使用:

    camera.update();
    tmr.setView(camera);
    tmr.render();
    b2dCam.update();
    b2dr.render(world, b2dCam.combined);

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

Two things come to mind.想到两件事。

  1. Do your tiles have at least 2px of padding around them?您的磁贴周围是否至少有 2px 的填充?

When OpenGL pulls textures from an image, it blends the pixels surrounding the texture region you are using with the edge of the texture region.当 OpenGL 从图像中提取纹理时,它会将您正在使用的纹理区域周围的像素与纹理区域的边缘混合。 Annoying huh?烦人吧? But there are reasons for it.但这是有原因的。 I couldn't tell for sure, but your video looks like you are getting horizontal gutters (the flickering at the bottom and between the house and the ground).我无法确定,但您的视频看起来像是出现了水平排水沟(底部以及房屋与地面之间的闪烁)。

To fix this, each tile on your image asset needs to have at least 2 pixels of padding all around it.为了解决这个问题,图像资产上的每个图块都需要至少有 2 个像素的填充周围。 To create the padding, create a 2px wide border around each tile in your image and then copy the edge pixels of the tile into this 2px wide border.要创建填充,请在图像中的每个图块周围创建一个 2px 宽的边框,然后将图块的边缘像素复制到这个 2px 宽的边框中。

  1. VSync垂直同步

If you still have issues after trying suggestion 1, I have had some flickering issues with libgdx scrolling when vsync was disabled.如果您在尝试建议 1 后仍有问题,那么当 vsync 被禁用时,我在 libgdx 滚动时遇到了一些闪烁的问题。 You can make sure it is enabled in your "Launcher" classes with:您可以通过以下方式确保在“启动器”类中启用它:

LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.vSyncEnabled = true;

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

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