How to move chase camera by the direction on XZ plane?

If you don’t understand the title I will explain it using images:


Here’s my code:

    @Override
    public void onAnalog(String name, float value, float tpf) {
        Vector3f direction = new Vector3f();

        if (name.equals("MoveChaseNodeForward")) {
            direction.x -= 1;
        }

        if (name.equals("MoveChaseNodeBackwards")) {
            direction.x += 1;
        }

        if (name.equals("MoveChaseNodeRight")) {
            direction.z -= 1;
        }

        if (name.equals("MoveChaseNodeLeft")) {
            direction.z += 1;
        }

        direction.normalizeLocal().multLocal(value);
        chaseNode.move(direction);
    }

Probably this is not enough information to diagnose your problem.

You may have better luck putting together a complete single class test case. 9 times out of 10, the test case will work fine and then you’ll have a place to start figuring out what’s different. That other 1 time we will have something to more easily spot the issue.

Almost by definition, if you don’t understand what is going wrong then it is difficult to understand what code is relevant to post. That’s why a complete and functioning test case is best.

Edit: also, it wasn’t clear to me which is the desired behavior because we don’t know what was being pressed, etc… I think that the ‘green’ is what is desired but the ‘pink’ is what is actually happening? But since “moving” was used both for “where the player is making it go” and “where it is going”, it’s actually ambiguous.

“Where chase node is moving.” meaning where someone is directing it to move or where it’s moving in the view?
“Where it’s actually moving.” meaning where someone is actually directing it to move or where it’s actually moving in the view? And is “it’s” referring to the camera or the node?

1 Like

nvm I did it myself

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
        switch (name) {
            case "MoveChaseNodeForward":
                up = isPressed;
                break;
            case "MoveChaseNodeBackwards":
                down = isPressed;
                break;
            case "MoveChaseNodeRight":
                right = isPressed;
                break;
            case "MoveChaseNodeLeft":
                left = isPressed;
                break;
        }
    }

    @Override
    public void simpleUpdate(float tpf) {
        Vector3f cameraDirection = cam.getDirection();
        Vector3f projectedCD = new Vector3f(cameraDirection.x, 0, cameraDirection.z);

        Vector3f cameraLeft = cam.getLeft();
        Vector3f projectedCL = new Vector3f(cameraLeft.x, 0, cameraLeft.z);

        Vector3f direction = new Vector3f();

        if (up) {
            direction.addLocal( projectedCD );
        }
        if (down) {
            direction.addLocal( projectedCD.negate() );
        }

        if (left) {
            direction.addLocal( projectedCL );
        }
        if (right) {
            direction.addLocal( projectedCL.negate() );
        }

        direction.normalizeLocal().multLocal(tpf);
        chaseNode.move(direction);
    }