java在LibGDX中如何防止相机延迟?
我试图在LibGDX中创建一个移动系统,其中所有东西都以5像素的增量移动,以使它看起来更像像素y。我现在一次移动玩家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 球员似乎在震动,好像摄像机跟不上他或其他什么
有人知道发生了什么吗?我错过什么了吗
# 1 楼答案
您正在更新相机的内部位置,但没有告诉相机更新视图矩阵,我猜这一定是在其他地方不规则发生的,因此视觉上不同步。 如果更改摄影机参数,请更新视图矩阵(视图转换为屏幕的方式)
把这个放在你的
setPosition
后面cam.update();
# 2 楼答案
你可以试试这样的,让我知道它对你有什么作用
您也可以尝试从凸轮位置X上移除
-15
零件,并查看其作用