Simple LibGDX Pong game drag player instead of getting x position

I got a working pong game in LibGDX but when I touch the screen the player x automaticly jumps to the coordinates I touched. I wan't my game to detect I touched and then move the player to the left/right if I swipe my finger to the left or the right.

This is what I tried so far:

if(Gdx.input.isTouched() && Gdx.input.getY()>240*screenheight){
if(tapped)TouchX = Gdx.input.getX();
tapped = false;
DragX = Gdx.input.getX();

distance = player.getX() - TouchX;

PlayerX = Gdx.input.getX() + distance;

Gdx.app.log("PlayerX "+player.getX(), "Distance "+distance);
Gdx.app.log("DragX ", ""+DragX);
}else{
tapped = true;
}

Here I update the player X coordinates

player.setX(PlayerX);

Does anyone know how to make this work?

You can use InputAdapter class and methods

touchDown(int screenX, int screenY, int pointer, int button)
Called when the screen was touched or a mouse button was pressed.

boolean touchDragged(int screenX, int screenY, int pointer)
Called when a finger or the mouse was dragged.

boolean touchUp(int screenX, int screenY, int pointer, int button)
Called when a finger was lifted or a mouse button was released.

You could update the position over time:

currentPos = currentPos + Math.signum(currentPos - touchedPos) * wayPerTime * deltaTime;
• deltaTime is the time passed since the last update.
• currentPos is the current position of your player (might be x, y or both as vector).
• touchedPos is the position the player has clicked on the screen (might be x, y or both as vector).

Math.signum() gives you +1 if the value is positive or -1 if negative. Otherwise 0. This takes care about the same speed regardless how big the distance is between player and clicked position.

The code uses this formula for calculating the way s: • I can't figure out your solution It ends up with making the player x going down each gameloop by 1 I updated my question with what I tried so far can you maybe take a look at that? Jan 28 '15 at 13:25
• This is because you don't incorporate the time and velocity. You implemented only the half of the equation. Use Math.signum() to figure out the direction to move. Then multiply it with the unsigned way you want to move to correct the position. If currentpos - touchedPos is 0 the result of Math.signum is 0 as well and so is the absolute way you add to the current position: The player doesn't move any longer. Jan 28 '15 at 13:32
• Allright how do I exactly get the velocity? I'm not using dx for the player for its speed just the coordinates the mouse is currently at. Jan 28 '15 at 13:38
• You have to design it by yourself. For example you could say the player moves within one second for 50px. If you have the delta time (in seconds) passed since your last update you can simply multiply it with that value: x += Gdx.graphics.getDeltaTimer() * velocity Jan 28 '15 at 13:51
• Did it help? Anything you don't understand yet? Feb 2 '15 at 9:31