First Person camera movement with LibGDL

Viewed 54

I've looked for hours but I couldn't find anything but two StackOverflow questions, one of which had one answer, by the author, not really giving proper explanation.

I was working on a game with LWJGL2.9 and now decided to switch to LibGDX for its simplicity. I had working code that allowed me to move through space - but that code can't be converted 1:1 to LibGDX and I can't find any example, explanation, tutorial or anything of the like, not even in LibGDX's wiki.

This is the code that I used for 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;
    }

}

I then updated the view matrix before rendering every frame calling the following method:

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, though, uses matrixes in a totally different way and I have no idea how to configure it (or how to properly update the view matrix, for that matter). Here's what I have:

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

    return matrix;
}

I'm not really even sure how I would be using that, I'm completely lost. I can draw a cube on the screen and move my mouse around to see it but as soon as I try to move (W,A,S,D) the cube disappears (I'm guessing it has to do with the view matrix practically not being applied).

I know that it's a long read and the code may even be bad but I already could find little to nothing for LWJGL let alone LibGDX.

1 Answers

I finally got it. I was drawing before moving my camera and that, apparently, was the issue.

My render loop previously looked like this:

@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);
}

I simply moved the camera input handling and camera update above the "render" code:

@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);
}
Related