【问题标题】:First Person camera movement with LibGDL使用 LibGDL 的第一人称相机移动
【发布时间】:2022-01-31 06:41:32
【问题描述】:

我已经找了好几个小时,但除了两个 StackOverflow 问题外,我什么也找不到,其中一个只有一个答案,作者给出的答案并没有真正给出正确的解释。

我正在使用 LWJGL2.9 开发游戏,现在决定切换到 LibGDX,因为它很简单。我有允许我在空间中移动的工作代码 - 但该代码无法 1:1 转换为 LibGDX,我找不到任何示例、解释、教程或类似的东西,甚至在 LibGDX 的 wiki 中也找不到。

这是我用于 LWJGL 的代码:

public Vectorf calculateMovement(MovementDirection direction, float newYaw, float walkSpeed, double deltaTime) {
        Vectorf position = new Vectorf(0f, 0f, 0f); // Just a Vector3f
        float amount = (walkSpeed) * (float) deltaTime;

        switch (direction) {
        case FORWARD:
        case BACKWARD: {
            float dx = (float) (direction.getXAdd() * Math.sin(Math.toRadians(newYaw))) * amount;
            float dz = (float) -(direction.getZAdd() * Math.cos(Math.toRadians(newYaw))) * amount;
            position.add(dx, direction.getYAdd() * amount, dz);
            break;
        }
        case LEFT:
        case RIGHT: {
            float dx = (amount * (float) (Math.sin(Math.toRadians(newYaw + 90))) * direction.getXAdd());
            float dz = (amount * (float) (Math.cos(Math.toRadians(newYaw + 90))) * direction.getZAdd());
            position.add(dx, direction.getYAdd() * amount, dz);
            break;
        }
        default: {
            position.add(direction.getXAdd() * amount, direction.getYAdd() * amount, direction.getZAdd() * amount);
        }
        }

        return position;
    }
public enum MovementDirection {

    FORWARD(1, 1),
    BACKWARD(-1, -1),
    LEFT(-1, 1),
    RIGHT(1, -1),
    UP(1),
    DOWN(-1);

    private final int xAdd;
    private final int yAdd;
    private final int zAdd;

    MovementDirection(int yAdd) {
        this(0, yAdd, 0);
    }

    MovementDirection(int xAdd, int zAdd) {
        this(xAdd, 0, zAdd);
    }

    MovementDirection(int xAdd, int yAdd, int zAdd) {
        this.xAdd = xAdd;
        this.yAdd = yAdd;
        this.zAdd = zAdd;
    }

    public float getXAdd() {
        return xAdd;
    }

    public float getYAdd() {
        return yAdd;
    }

    public float getZAdd() {
        return zAdd;
    }

}

然后我在渲染每一帧之前调用以下方法更新了视图矩阵:

public static Matrix4f createViewMatrix(Camera camera) {
    Matrix4f matrix = new Matrix4f();
    matrix.setIdentity();

    Matrix4f.rotate((float) Math.toRadians(camera.getYaw()), new Vector3f(1, 0, 0), matrix, matrix);
    Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(0, 1, 0), matrix, matrix);
    Matrix4f.rotate((float) Math.toRadians(camera.getDelta()), new Vector3f(0, 0, 1), matrix, matrix);
    Matrix4f.translate(camera.getNegativePosition(), matrix, matrix);

    return matrix;
}

不过,LibGDX 以完全不同的方式使用矩阵,我不知道如何配置它(或者如何正确更新视图矩阵)。这是我所拥有的:

public static Matrix4 createViewMatrix(Camera camera) {
    Matrix4 matrix = new Matrix4();
    matrix.setToLookAt(camera.direction, camera.up);

    return matrix;
}

我什至不确定我会如何使用它,我完全迷失了。我可以在屏幕上绘制一个立方体并移动鼠标以查看它,但是一旦我尝试移动(W,A,S,D)立方体消失(我猜它实际上与视图矩阵有关未应用)。

我知道这是一篇很长的文章,代码甚至可能很糟糕,但我已经几乎找不到 LWJGL 的内容了,更不用说 LibGDX了。

【问题讨论】:

    标签: java opengl camera libgdx


    【解决方案1】:

    我终于明白了。我在移动相机之前正在画画,这显然是问题所在。

    我的渲染循环以前看起来像这样:

    @Override
    public void render() {
        // Other code
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
        Gdx.gl.glClearColor(0, 0, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    
        // Handle camera input
        camera.update();
    
        modelBatch.render(camera);
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
    }
    

    我只是将相机输入处理和相机更新移到“渲染”代码上方:

    @Override
    public void render() {
        // Other code
    
        // Handle camera input
        camera.update();
    
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
        Gdx.gl.glClearColor(0, 0, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    
        modelBatch.render(camera);
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多