繁体   English   中英

如何防止我的相机在 LibGDX 中延迟?

[英]How do I prevent my camera from being delayed in LibGDX?

我正在尝试在 LibGDX 中制作一个移动系统,其中所有内容都以 5 像素的增量移动,我猜想使它看起来更像像素。 我目前每次将玩家移动 5 个像素,然后移动相机以匹配玩家的位置,让他始终保持在屏幕上。 这是我用来执行此操作的代码(每次调用 render 时都会运行):

Vector2 newCoordinates = MovementUtilities.getIncremented(x, y);
    cam.position.set(newCoordinates.x, newCoordinates.y, 0);
    batch.draw(texture, cam.position.x-15, cam.position.y, width, height);

MovementUtilities.getIncremented() 的代码:

public static final int INCREMENT = 5;

public static Vector2 getIncremented(int x, int y) {
    return new Vector2(x-(x % INCREMENT), y-(y % INCREMENT));
}

但是,当我尝试运行它时,会发生这种情况: https://youtu.be/C_r0mOaxJgU播放器似乎在振动,好像相机跟不上他什么的。

有谁知道发生了什么? 我错过了什么吗?

你可以尝试这样的事情,让我知道它是如何为你工作的。

    // initialize with your beginning position if you'd like.
private float x, y;
// initialize with your beginning position if you'd like.
private final Vector2 coordinates = new Vector2(x, y);

private final float moveSpeed = 5.0f;

public void function() {
    getNewPosition();
    cam.position.set(coordinates.x, coordinates.y, 0.0f);
    cam.update();

    // draw
}

public void getNewPosition() {
    coordinates.set(x - moveSpeed, y - moveSpeed);
}

您也可以尝试从凸轮位置 X 上移除-15部分,看看会发生什么。

您正在更新相机的内部位置,但没有告诉相机更新视图矩阵,我想这一定是在其他地方不规则发生的,因此在视觉上不同步。 如果更改相机参数,请更新视图矩阵(视图如何转换到屏幕)。

把它放在你的setPosition

cam.update();

暂无
暂无

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

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