3
votes

I want to create a mobile application which allows two players to play pong on the same device. Each player will grab an end of the device and be able to move their goalie back and forth on the y-axis. I'm developing the game using Java w/ LibGDX, and I'm having trouble getting multiple input to work. I can make it so that the platform registers that input is coming in on either the left or right side (determining which player is moving) and I can use this to move each player individually, but I can't make them do this at the same time.

This is how I currently have my movement set up:

        PlayerPaddle playerOnePaddle = ((GameScreen) currentScreen).getPlayerOnePaddle();
        PlayerPaddle playerTwoPaddle = ((GameScreen) currentScreen).getPlayerTwoPaddle();
        Vector2 touchPos = new Vector2(Gdx.input.getX(), Gdx.input.getY() + playerOnePaddle.height / 2);

        if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
        {
            playerOnePaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y;
        }
        if (Gdx.input.getX() > Gdx.graphics.getWidth() / 2)
        {
            playerTwoPaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y;
        }

This works with individual input, meaning that I can tap the left of the screen and move the left player, I can tap the right of the screen and move the right player, but I can't move each player at the same time, which would defeat the point of the game. I need specific examples of how to implement this, as my experience with LibGDX input is very limited and after searching around I wasn't able to find any correct ways to do this. I thought about multithreading the second input, but that would just make the code messy and make the logic unsymmetrical

1

1 Answers

3
votes

To every touch in the screen an int pointer is given. The first touch will get pointer 0, the second will get pointer 1. If you let the touch, the pointer is released and it will be given to the next touch (It will always take the first free pointer). I would recommend you to check the first 5 pointers to be sure:

for (int i=0; i<5; i++){
    if (!Gdx.input.isTouched(i)) continue;
    Vector2 touchPos = new Vector2(Gdx.input.getX(i), Gdx.input.getY(i) + playerOnePaddle.height / 2);
    if (Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2){
        playerOnePaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y;
    }
    if (Gdx.input.getX(i) > Gdx.graphics.getWidth() / 2){
        playerTwoPaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y;
    }
}

Btw, you should use a camera and unproject your touch:

camera.unproject(touchPos.set(Gdx.input.getX(i), Gdx.input.getY(i), 0));

And the use touchPos.x and touchPos.y as your touch. that way it will work the same in every screen resolution.