2 Player Pong Multitouch Help

Hello everyone, i am currently developing a two-player pong game for android. In order to enable multitouch, i used the code i found online:

 public boolean surfaceTouchEvent(MotionEvent event) {
                pointerCount = event.getPointerCount();
                if (pointerCount>2){
                  pointerCount = 2;
                }
                //println("Number of pointers: "+pointerCount);
                points.clear();
                for(int i=1; i<=pointerCount; i++) {

                points.add(new PVector(event.getX(i-1), event.getY(i-1)));
                }

                //if the event is a pressed gesture finishing, 
                // it means the lifting the last touch point
                if(event.getActionMasked() == MotionEvent.ACTION_UP) points.clear();

                // if you want the variables for motionX/motionY, mouseX/mouseY etc.
                // to work properly, you'll need to call super.surfaceTouchEvent().
                return super.surfaceTouchEvent(event);

              }      

Within the void draw(), i did this:

if (speedY>0){
        if (tempPoint.y>height/2){
         //extra code here
          rect (tempPoint.x, height-lineHeight, pongRadius*2, pongHeight*2);

          }  
        if (tempPoint.y<height/2){
          //extra code here
          rect (tempPoint.y, lineHeight, pongRadius*2, pongHeight*2);

        }
      }

      if (speedY<0){
        if (tempPoint.y<height/2){
          //extra code here
          rect (tempPoint.x, lineHeight, pongRadius*2, pongHeight*2);


        }  
        if (tempPoint.y>height/2){
          //extra code here
          rect (tempPoint.x, height-lineHeight, pongRadius*2, pongHeight*2);

        }
      }

     }

Basically, everything is working fine, and the 2 "pongs" or paddles move fine when they are 2 fingers holding down at both ends of the screen. However, the problem arises when one person decides to let go of the screen at the second person puts 2 fingers on their side of the screen, which results in the second person getting 2 paddles and the 1st person not able to get any. This is because i have set the limit of pointerCount to 2 in the code if (pointerCount>2){ pointerCount = 2; }.

Is it possible for me to have a maximum of 1 paddle (1 touch) in each side of the screen(so there will still be two touches in total), and if so, how?

Thanks in advance.

Answers

  • I'm assuming this is set up like a classic Pong screen, divided into two halves, with one paddle in each half? You could check the touch positions in surfaceTouchEvent(), and only allow one paddle per half-screen. If there are two touchpoints in the same half-screen, ignore one of them.

  • edited March 2015

    Thank you for your reply.

    As im pretty unfamiliar with this, how do i check if there are two touch points in either half of the screen, and how do i enable the code to "ignore" one of them?

    Also, i tried doing this to differentiate between touchpoints that are in the top half and touchpoints that are in the bottom half.

        for(int i=0; i<points.size(); i++) {
                if (points.get(i).y>height/2){
                          tempPointB = (PVector) points.get(i);
                }
    
                if (points.get(i).y<height/2){
                      tempPoint = (PVector) points.get(i);
                }
    

    How do i ensure that there is only one point in tempPoint and tempPointB?

    Thanks once again

  • edited March 2015

    Actually, your code should end up with at most one point each in tempPoint or tempPointB. If there are 2 points in (say) tempPoint, the second one will write over the first one.

    And if points.size() > 2, you should still end up with two total points, one in tempPointB and one in tempPoint.

    (Is there a situation that I'm not thinking about?)

  • edited March 2015

    Thanks once again for the reply.

    When there are 2 points in one side, while the first one does overwrite the second one, it still ends up with two paddles in one side. To top it off, the player in the other side of the screen is then not able to get a paddle even when he touches the screen.

    I even tried creating 2 arraylists for tempPointB and tempPointA, called bTempPoint and aTempPoint respectively.

        for(int i=0; i<points.size(); i++) {
            tempPoint = (PVector) points.get(i);
            if (points.get(i)!= null){
              if (points.get(i).y>height/2){
    
                  tempPointB = points.get(i);
    
                  if (bTempPoint.size()== 0){
                  bTempPoint.add( 0, tempPointB);
                  }
                  if (bTempPoint.size()>1){
                      bTempPoint.remove(1);
                  }
                  }
              }
              if (points.get(i) != null){
              if (points.get(i).y<height/2){
    
                  tempPointA = points.get(i);
    
                  if (aTempPoint.size()==0){
                  aTempPoint.add(0, tempPointA);
    
                  }
                  if (aTempPoint.size()>1){
                      aTempPoint.remove(1);
                  }
                  }
              }
    

    after which, i did this to draw the rectangle.

            if (bTempPoint.size()==1){
                     rect (bTempPoint.get(0).x, height-lineHeight, pongRadius*2, pongHeight*2);
    

    Despite all this, i still end up getting 2 rectangles with 2 fingers on the same side of the screen.

    What has gone wrong in my code? Once again, thank you for your help.

  • Ah, maybe I see the problem.

    In surfaceTouchEvent(), are you still forcing the number of touch points to 2?

    // pointerCount forced to 2 max
    for(int i=1; i<=pointerCount; i++) {
           points.add(new PVector(event.getX(i-1), event.getY(i-1)));
    }
    

    If you just leave pointerCount to however many touch points there are, you should be ok. Then you may have > 2 touch points in the points ArrayList, but multiple touch points on the same half-screen will overwrite each other.

  • edited March 2015

    Upon removing the if (pointerCount>2){ pointerCount = 2; } , the issue is not resolved two rectangles still form when two fingers are pressed. Infact, even though the x-coordinate of the rectangle is set as an integer variable "pongCenter", 2 rectangles are still drawn. This seems to suggest that the variable has 2 different values at the same time, which i know is not possible.

    Upon doing some research, i have found that multitouch can be handled by getPointerIndex() and some other methods within the surfaceTouch, however, i dont know how to use it. Do you have any idea about it?

    thanks in advance

  • That's odd… I think I need to see the entire sketch before more suggestions…

Sign In or Register to comment.