Advertisement
ffpaladin

Breakout03 Collision

Jul 10th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1.     //Major method which moves the ball, causes constraints on the ball's movements(within the screen) and bounces the ball
  2.         private void playTurn() {
  3.             double vx = rgen.nextDouble(1.0, 3.0);
  4.             if(rgen.nextBoolean(.5)) vx=-vx;
  5.             double vy = Y_VELOCITY;
  6.             while (true) {
  7.                 if(numBricksLeft == 0)
  8.                     break;
  9.                 if(ball.getY() > APPLICATION_HEIGHT)
  10.                     break;
  11.                 ball.move(vx,vy);
  12.                 pause(ANIMATION_PAUSE);
  13.                
  14.                 vy = reflectOffColliders(vy);          
  15.                 vx = reflectOffSideWalls(vx);
  16.                 vy = reflectOffTopWall(vy);
  17.             }
  18.         }
  19.  
  20.         /**
  21.          * Change the y velocity if it hits a collider
  22.          * @param vy
  23.          * @return
  24.          */
  25.         private double reflectOffColliders(double vy) {
  26.             GObject collider = getCollidingObject();
  27.             if (collider == paddle) {
  28.                 bounceClip.play();
  29.                 vy = -Math.abs(vy);
  30.             }
  31.             else if (collider != null) {
  32.                 remove(collider);
  33.                 vy *= -1;
  34.             }
  35.             return vy;
  36.         }
  37.  
  38.         /**
  39.          * Get top screen bound and reverse velocity
  40.          * @param vy
  41.          * @return
  42.          */
  43.         private double reflectOffTopWall(double vy) {
  44.             if (ball.getY() < 0) {
  45.                 vy *= -1;
  46.             }
  47.             return vy;
  48.         }
  49.  
  50.         /**
  51.          * Get side bounds and reverse x velocity
  52.          * @param vx
  53.          * @return
  54.          */
  55.         private double reflectOffSideWalls(double vx) {
  56.             if (ball.getX() < 0 || ball.getX() > APPLICATION_WIDTH - ball.getWidth()) {
  57.                 vx *= -1;
  58.             }
  59.             return vx;
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement