Esempio n. 1
0
 //check pad colliding with ball
 public void checkPadCollision(Paddle pad, float iSpeed)
 {
     //ball in yAxis inside pad                                                              //ball in xAxis  between points
     if ((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos + radius <= pad.getY() + pad.getHeight()/2) && (xPos + radius >= pad.getLL() && xPos + radius < pad.getML()))//if left area
     {//bounce back or up(goes back if hit on the side because of xSpeed changed to 0, and next frame still in same "x" of area, so goes back.
         xSpeed = xSpeed > 0 ? 0 : -iSpeed;
         ySpeed = -iSpeed;
     }
     else if ((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos + radius <= pad.getY() + pad.getHeight()/2) && (xPos + radius >= pad.getML() && xPos - radius <= pad.getMR()))
     {//bounce MIDDLE
         if(xSpeed > 0)
         {
             xSpeed = iSpeed;
         }
         else if(xSpeed <0)
         {
             xSpeed = -iSpeed;//so that the speed gets reset when you hit the pad.
         }
         else if(xSpeed == 0)
         {
             xSpeed = 0;
         }
         ySpeed = -iSpeed;
     }
     else if((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos + radius <= pad.getY() + pad.getHeight()/2) && (xPos - radius > pad.getMR() && xPos - radius <= pad.getRR()))
     {//bounce back or up
         xSpeed = xSpeed < 0 ? 0 : iSpeed;
         ySpeed = -iSpeed;
     }
 }
Esempio n. 2
0
        //check pad colliding with ball
        public void checkPadCollision(Paddle pad, float iSpeed)
        {
            //ball in yAxis inside pad                                                              //ball in xAxis  between points
            if ((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos - radius <= pad.getY() + pad.getHeight()/2) && (xPos + radius >= pad.getLL() && xPos < pad.getML()))//if left area
            {//bounce back or up(goes back if hit on the side because of xSpeed changed to 0, and next frame still in same "x" of area, so goes back.
                xSpeed += -iSpeed * 1.5F;
                xSpeed = xSpeed < -12 ? -12 : xSpeed * 1.5F;

                ballForcey = -accelerationY;
                ySpeed = ySpeed > 12 ? -12 : -ySpeed * 1.5F;
            }
             if ((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos + radius <= pad.getY() + pad.getHeight()/2) && (xPos + radius >= pad.getML() && xPos - radius <= pad.getMR()))
            {//bounce MIDDLE
                if (ySpeed < iSpeed )
                    ySpeed = iSpeed;

                ballForcey = -accelerationY;
                ySpeed = -ySpeed;
            }
             if((yPos + radius >= pad.getY() - pad.getHeight()/2 && yPos - radius <= pad.getY() + pad.getHeight()/2) && (xPos > pad.getMR() && xPos - radius <= pad.getRR()))
            {//bounce back or up
                xSpeed += iSpeed * 1.5F;
                xSpeed = xSpeed > 12 ? 12 : xSpeed * 1.5F;

                ballForcey = -accelerationY;
                ySpeed = ySpeed > 12 ? -12 : -ySpeed * 1.5F;

            }
        }