Advertisement Jump to content
Sign in to follow this  

TouchDrag input in android

This topic is 979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a space top down shooter type of game with Libgdx and box2d. The controls for the game are from touch screen. Basically if you put you finger anywhere on the screen and drag, the ship will fly in the direction of a drag. The problem is if I have an object on the left side of the screen and I drag it to the right side of the screen, my finger gets to the right side of the screen faster while ship stops in the middle of the screen. The desired goal is to have ship be always caught up with the user's finger and stop at the location where user stopped dragging/or released finger. There must be something wrong with how I update the velocity or delta in the touchDrag method. Can someone please take a look at the code and give me a hint on what am I doing wrong? Also, is it bad to use box2d for an object that changes velocities so frequently? 

public class InputManagerOld extends InputAdapter {

    private Vector2 lastTouch = new Vector2();
    // The queue is used to allow user to place on the screen as many fingers
    // as he wants and without messing up the control of the ship
    private Queue<Integer> activeTouch = new Queue<Integer>();
    private PlayerOld player;

    private Vector2 touchPos    = new Vector2();
    private Vector2 dragPos     = new Vector2();

    float distance = 0;

    public InputManagerOld(PlayerOld player) {
        this.player = player;

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        //lastTouch.set(screenX, screenY);
        if (pointer == activeTouch.first()) {
            lastTouch.set(screenX, screenY);
            touchPos.set(screenX, - screenY);
        return super.touchDown(screenX, screenY, pointer, button);

    public boolean touchUp(int screenX, int screenY, int pointer, int button) {

        if (activeTouch.size > 0) {
            activeTouch.removeValue(pointer, true);
        player.state = PlayerOld.Flying.FLYING_CENTER;

        return super.touchUp(screenX, screenY, pointer, button);

    public boolean touchDragged(int screenX, int screenY, int pointer) {
        if (pointer == activeTouch.first()) {
            Vector2 newTouch = new Vector2(screenX, screenY);
            Vector2 delta = newTouch.cpy().sub(lastTouch);
            lastTouch = newTouch;
            dragPos.set(screenX, - screenY);
            distance = touchPos.dst(dragPos);
            Vector2 velocity = player.b2body.getLinearVelocity();
            System.out.println("Delta x: " + delta.x + " delta y: " + delta.y);
            velocity.add(delta.x, -delta.y);
        return super.touchDragged(screenX, screenY, pointer);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!