Search code examples
javacameralibgdxlag

LibGDX: Camera movement by keys gets lag when mouse is clicked


This is my situation: https://i.sstatic.net/Xjv99.jpg

As you can see in the video, the game gets lag whenever I click a mouse after changing camera movement direction, if I keep the camera moving in the same direction then only the first click causes lag but the later ones. Here is my CameraHandler code:

    public CameraMovementHandler(GameScreen screen) {
        super(screen);

        pressedButton = 0b0000;
        map = screen.getMap();
        camera = screen.getCamera();
        desiredPosition = camera.position;
    }

    public void update() {

        if ((pressedButton & 0b0010) != 0) {
            desiredPosition.x += CAMERA_SPEED * Gdx.graphics.getDeltaTime();
        }

        if ((pressedButton & 0b1000) != 0) {
            desiredPosition.x -= CAMERA_SPEED * Gdx.graphics.getDeltaTime();
        }

        if ((pressedButton & 0b0001) != 0) {
            desiredPosition.y += CAMERA_SPEED * Gdx.graphics.getDeltaTime();
        }

        if ((pressedButton & 0b0100) != 0) {
            desiredPosition.y -= CAMERA_SPEED * Gdx.graphics.getDeltaTime();
        }

        camera.position.lerp(desiredPosition, 0.1f);

        camera.position.x = MathUtils.clamp(camera.position.x, GameScreen.VIEWPORT_WIDTH / 2.f,
                map.getWidth() - GameScreen.VIEWPORT_WIDTH / 2.f);
        camera.position.y = MathUtils.clamp(camera.position.y, GameScreen.VIEWPORT_HEIGHT / 2.f,
                map.getHeight() - GameScreen.VIEWPORT_HEIGHT / 2.f);

        camera.update();
    }

    @Override
    public boolean keyDown(int keycode) {
        catchPressedButton(keycode);
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        catchPressedButton(keycode);
        return false;
    }

    private void catchPressedButton(int keycode) {
        if (keycode == Input.Keys.A) {
            pressedButton ^= 0b1000;
        } else if (keycode == Input.Keys.S) {
            pressedButton ^= 0b0100;
        } else if (keycode == Input.Keys.D) {
            pressedButton ^= 0b0010;
        } else if (keycode == Input.Keys.W) {
            pressedButton ^= 0b0001;
        }
    }

And here is my GameScreen code that registers the CameraHandler

    public GameScreen(UntitledFarmGame untitledFarmGame) {
        this.untitledFarmGame = untitledFarmGame;
        inputMultiplexer = new InputMultiplexer();
        spriteBatch = new SpriteBatch();
        map = new Map(100, 100, spriteBatch, untitledFarmGame.getResourceManager());
        camera = new OrthographicCamera();
        viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
        gameHud = new GameHud(this, untitledFarmGame.getResourceManager());
    }

    public void addPlayerInputHandler(PlayerInputHandler inputHandler) {
        inputMultiplexer.removeProcessor(inputHandler);
        inputMultiplexer.addProcessor(inputHandler);
    }

    public void removePlayerInputHandler(PlayerInputHandler inputHandler) {
        inputMultiplexer.removeProcessor(inputHandler);
    }

    @Override
    public void show() {
        inputMultiplexer.addProcessor(new CameraMovementHandler(this));
        inputMultiplexer.addProcessor(gameHud);

        Gdx.input.setInputProcessor(inputMultiplexer);
    }

    @Override
    public void render(float delta) {
        ScreenUtils.clear(0, 0, 0, 1);

        viewport.apply();
        spriteBatch.setProjectionMatrix(camera.combined);
        map.draw();
        inputMultiplexer.getProcessors().forEach(i -> {
            if (i instanceof PlayerInputHandler) {
                ((PlayerInputHandler) i).update();
            }
        });

        gameHud.getViewport().apply();
        gameHud.act(delta);
        gameHud.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
        camera.setToOrtho(false, width, height);
        gameHud.getViewport().update(width, height);
    }

How can I fix this issue? Thank you.


Solution

  • The problem is I'm using Manjaro Linux with Xorg, and Xorg has the input lag issue. Then after switching to Wayland, the problem is gone.