Advertisement

TouchDrag input in android

Started by May 13, 2016 05:00 AM
-1 comments, last by cblg 8 years, 7 months ago

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;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        activeTouch.addLast(pointer);
        //lastTouch.set(screenX, screenY);
        if (pointer == activeTouch.first()) {
            lastTouch.set(screenX, screenY);
            touchPos.set(screenX, Gdx.graphics.getHeight() - screenY);
        }
        return super.touchDown(screenX, screenY, pointer, button);
    }

    @Override
    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);
    }

    @Override
    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, Gdx.graphics.getHeight() - screenY);
            distance = touchPos.dst(dragPos);
            System.out.println(distance);
            Vector2 velocity = player.b2body.getLinearVelocity();
            System.out.println("Delta x: " + delta.x + " delta y: " + delta.y);
            velocity.add(delta.x, -delta.y);
            player.b2body.setLinearVelocity(velocity);
        }
        return super.touchDragged(screenX, screenY, pointer);
    }
}

This topic is closed to new replies.

Advertisement