有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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 球员似乎在震动,好像摄像机跟不上他或其他什么

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


共 (2) 个答案

  1. # 1 楼答案

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

    把这个放在你的setPosition后面

    cam.update();

  2. # 2 楼答案

    你可以试试这样的,让我知道它对你有什么作用

        // 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零件,并查看其作用