Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling multitouch in libgdx

okay so I have been trying to implement a multitouch solution for my libgdx game and having some trouble. Basically I have a dpad in the bottom left quadrant of the screen which controls the direction of my character and on the remaing portion of the screen want to process swipes in order to have the character jump , punch or throw something at the enemy. I originally considered using fling in GestureListener However I have since been informed that fling is not supported in multitouch and have since began using pan to detect the swipes. The problem I am currently having is that if I have a finger on the screen while using a swipe (the player is moving and attempting to jump), the pointer passed to pan stop seems to be incorrect (at least basse on my logging and understanding of the implementation) anyways here is the code below and a link to the docs on gesture detector here if anyone can help to explain what is going on it would be greatly appreciated.

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    if (x < width / 3 && y > height / 2) {
        directionPointer= pointer;
        if(x< width/6){     
            controller.leftPressed();
            message=" driection TOUCH DOWN POINTER IS: " + pointer  + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
            Gdx.app.log("INFO", message);   
        return true;
    }else{oller.rightPressed(); 
        message=" direction TOUCH DOWN POINTER IS: " + pointer  + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
        Gdx.app.log("INFO", message);   
        return true;

    }       

}else{
    actionPointer= pointer;
    down_x=x;
    down_y=y;
    message= "Pointer value is" + actionPointer;
}

message="TOUCH DOWN POINTER IS: " + pointer  + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
Gdx.app.log("INFO", message);   
return false;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {

    if ( pointer == directionPointer) {         
        controller.leftReleased();
        controller.rightReleased(); 
        directionPointer=-1;
        return true;                
    }
    if (x < width / 3 && y > height / 2) {

        controller.leftReleased();
        controller.rightReleased(); 
        directionPointer=-1;
        message= "TOUCHUP pointer is ->"+ pointer + "actionPointer->"+ actionPointer + "directionPointer-> "+ directionPointer;
        Gdx.app.log("INFO", message);
            return true;                
    }

    message= "touchUP was detected";
    Gdx.app.log("INFO", message);
    return false;

}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
    message="swipe not processed";
    if( pointer==actionPointer){
        delta_x= Math.abs(down_x -x);
        delta_y= Math.abs(down_y - y);
        if (delta_x < delta_y ){
            // this is an up or down value 
            if(down_x > x){
                controller.bobPunch();
                message = "SWIPE DOWNWARD " ;           
            }else {
                // swipe up 
                controller.bobJump();
                message = "SWIPE UPWARD " ;
            }           
        }else{
            if(down_y< y){
                controller.throwPressed();
                message=" SWIPE RIGHT";
                //swipe right ward 
            }else{
                controller.throwPressed();
                message=" SWIPE LEFT";
               // swipe left    
            }
        }
    }
    Gdx.app.log("INFO", message);
    message="panstop pointer is : " + pointer  + "the action pointer-> " +actionPointer + "directionPointer->" + directionPointer;
    Gdx.app.log("INFO", message);
    actionPointer=-1;
    // TODO Auto-generated method stub
    return true;
}

now this is the relevant code to detecting what actions should be taken (directionPointer and actionPointer are global ints initialized ad -1) the problem is that the pointer returned by panStop are not as I expected here are some output log files for different touch actions to show what is happening :

case 1: hold down bottom leftt corner of screen (bob moves left):

direction TOUCHdOWN POINTER IS: 0 action pointer->-1 directionpointer->0

case2 swipe up (bob jumps)

TOUCHdOWN POINTER IS: 0 action pointer->0 directionpointer->-1
SWIPE UPWARD
panstop pointer is: 0 actionpointer->0 directionpointer->-1

case 3 move and jump (here is where the problem is):

direction TOUCHdOWN POINTER IS: 0 action pointer->-1 directionpointer->0
TOUCHdOWN POINTER IS: 1 action pointer->1 directionpointer->0
swipe not processed
panstop pointer is: 0 actionpointer->1 directionpointer->0

now the last line although I can not prove it to you is the problem as the finger I pan with is registered as the action pointer the pointer for direction(which does not leave the screen) is the one returned. Can anyone point out wht I am doing wrong or if this is a bug with libgdx itself or the more likely my code itself

`

like image 889
brendosthoughts Avatar asked Aug 05 '14 19:08

brendosthoughts


1 Answers

What you actually could do is to let the DPAD be an actor connected to a Stage, while the onscreen controls are handled bu a InputProcessor or GestureListener and use a InputMultiplexer to combine them:

InputMultiplexer im = new InputMultiplexer(stage, inputProcessor);
Gdx.input.setInputProcessor(im);

also if I'm not mistaken, the order of the parameters sent to the InputMultiplexer counts as priorities. Meaning that in the above case the Stage (DPAD) would be prioritized before the on screen controls. So in your case, you probably would want to switch them.

I'm aware of that this might not be a good solution for you, but I've been doing it this way in some of my games.

like image 59
Nine Magics Avatar answered Nov 11 '22 17:11

Nine Magics