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



我正在尝试在LibGDX中制作一个移动系统,其中所有内容都以5像素的增量移动,以使其看起来更像像素。我现在每次移动玩家5个像素,然后移动镜头以匹配玩家的位置,让他始终在屏幕上。这是我用来做的代码(每次渲染被调用时运行):

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 . getincrement()代码:

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玩家似乎在震动,好像摄像机跟不上他或什么的。

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

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

把这个放在你的setPosition后面

cam.update();

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

// 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部分,看看这是什么。