@Override
  public void update(float pDeltaTime) {
    mGame.getInput().getMouseEvents();
    mGame.getInput().getKeyEvents();

    int len = mBalls.size();
    for (int i = 0; i < len; i++) {
      Ball firstBall = mBalls.get(i);

      for (int j = 0; j < len; j++) {
        Ball secondBall = mBalls.get(i);
        if (secondBall == firstBall
            || !OverlapTester.overlapCircles(firstBall.bounds, secondBall.bounds)) {
          continue;
        }
        System.out.println("overlap");
        float newVelocityX1 =
            (firstBall.velocity.x * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.x * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY1 =
            (firstBall.velocity.y * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.y * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityX2 =
            (secondBall.velocity.x * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.x * firstBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY2 =
            (secondBall.velocity.y * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.y * firstBall.mass))
                / (firstBall.mass + secondBall.mass);

        firstBall.velocity.add(newVelocityX1, newVelocityY1);
        secondBall.velocity.add(newVelocityX2, newVelocityY2);
      }

      // firstBall.velocity.add(mGravity.x * pDeltaTime, mGravity.y * pDeltaTime);

      firstBall.update(pDeltaTime);
      if (firstBall.getX() < firstBall.getWidth() / 2) {
        firstBall.setX(firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getX() > mWidth - firstBall.getWidth() / 2) {
        firstBall.setX(mWidth - firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getY() < firstBall.getWidth() / 2) {
        firstBall.setY(firstBall.getWidth() / 2);
        firstBall.velocity.y *= -1;
      }
      if (firstBall.getY() > mHeight - firstBall.getHeight() / 2) {
        firstBall.setY(mHeight - firstBall.getHeight() / 2);
        firstBall.velocity.y *= -1;
      }
    }
  }
예제 #2
0
 public void updateSizes(int width, int height) {
   frameWidth = width;
   frameHeight = height;
   ball.setX((frameWidth - ball.getDiameter()) / 2);
   ball.setY((frameHeight - ball.getDiameter()) / 2);
   //		System.out.println(this.getWidth() +" " + this.getHeight());
   this.setSize(frameWidth, frameHeight);
 }
예제 #3
0
 public void followMouse() {
   if (draggedMouseX != -1 && draggedMouseY != -1) {
     ball.setX(draggedMouseX - ball.getDiameter() / 2);
     ball.setY(draggedMouseY - ball.getDiameter() / 2);
     // System.out.println(draggedMouseX + " "+ draggedMouseY);
     repaint();
   }
 }
예제 #4
0
  public void moveBall() {
    if (moveRight == true) {
      if (ball.getX() < frameWidth - ball.getDiameter()) {
        ball.setX(ball.getX() + 1);

        if (moveDown == true) {
          if (ball.getY() < this.getWidth() - ball.getDiameter()) {
            ball.setY(ball.getY() + 1);
          } else {
            moveDown = false;
          }
        } else {
          if (ball.getY() >= 0) {
            ball.setY(ball.getY() - 1);
          } else {
            moveDown = true;
          }
        }
      } else {
        moveRight = false;
      }
    } else {
      if (ball.getX() >= 0) {
        ball.setX(ball.getX() - 1);

        if (moveDown == true) {
          if (ball.getY() < this.getWidth() - ball.getDiameter()) {
            ball.setY(ball.getY() + 1);
          } else {
            moveDown = false;
          }
        } else {
          if (ball.getY() >= 0) {
            ball.setY(ball.getY() - 1);
          } else {
            moveDown = true;
          }
        }
      } else {
        moveRight = true;
      }
    }

    repaint();
  }
예제 #5
0
  public void paint(Graphics g) {
    // set up the double buffering to make the game animation nice and smooth
    Graphics2D twoDGraph = (Graphics2D) g;

    // take a snap shop of the current screen and same it as an image
    // that is the exact same width and height as the current screen
    if (back == null) back = (BufferedImage) (createImage(getWidth(), getHeight()));

    // create a graphics reference to the back ground image
    // we will draw all changes on the background image
    Graphics graphToBack = back.createGraphics();

    if (lives < 1) {
      ball.setXSpeed(0);
      ball.setYSpeed(0);
      paddle.setSpeed(0);
      graphToBack.drawString("GAME OVER", 387, 30);
    } else if (bricks.isEmpty()) {
      ball.setXSpeed(0);
      ball.setYSpeed(0);
      paddle.setSpeed(0);
      graphToBack.drawString("YOU WIN", 387, 30);
    }

    ball.moveAndDraw(graphToBack);
    paddle.draw(graphToBack);
    for (Block x : bricks) {
      x.draw(graphToBack);
    }
    graphToBack.setColor(Color.white);
    graphToBack.drawString(score + ":" + lives, 387, 15);

    if (ball.didCollide(left) || ball.didCollide(right)) {
      ball.setXSpeed(-ball.getXSpeed());
    }

    if (ball.didCollide(top)) {
      ball.setYSpeed(-ball.getYSpeed());
    }

    if (ball.didCollide(bottom)) {
      ball.draw(graphToBack, Color.white);
      ball.setXSpeed(2);
      ball.setYSpeed(2);
      ball.setX(400);
      ball.setY(500);
      lives--;
      ball.setYSpeed(-ball.getYSpeed());
    }

    // see if the ball hits the paddle
    if (ball.didCollide(paddle)) ball.setYSpeed(-ball.getYSpeed());

    // brick collision
    for (Block x : bricks) {
      if (x.didCollide(ball)) {
        score++;
        x.draw(graphToBack, Color.white);
        bricks.remove(x);
        if (ball.didCollideTop(x) || ball.didCollideBottom(x)) ball.setYSpeed(-ball.getYSpeed());
        else ball.setXSpeed(-ball.getXSpeed());
      }
    }
    // see if the paddles need to be moved
    graphToBack.setColor(Color.black);
    graphToBack.drawString(score + ":" + lives, 387, 15);

    if (keys[0]) paddle.moveLeftAndDraw(graphToBack);
    if (keys[1]) paddle.moveRightAndDraw(graphToBack);

    twoDGraph.drawImage(back, null, 0, 0);
  }
예제 #6
0
  /**
   * Determines if the given ball has collided with the paddle and returns true if it has and false
   * if it has not. If the ball has collided with the paddle the balls movement vectors are modified
   * so the ball will bounce back at the proper angle.
   *
   * @param ball is the ball to examine to see if it has collided with the paddle
   * @return Returns true if ball collides with paddle and if so updates vectors
   */
  public boolean collision(Ball ball) {
    // Temp variables for paddle corner coordinates;
    int pX1 = this.x;
    int pX2 = pX1 + this.width - 1;
    int pY1 = this.y;
    int pY2 = pY1 + this.height - 1;

    // Temp variables  for ball coordinates, movement vectors, and radius
    int bX = ball.getX();
    int bY = ball.getY();
    int vX = ball.getXVector();
    int vY = ball.getYVector();
    int bRad = ball.getRadius();

    // Testing for collisions with the paddle edges and the Ball
    if (bX < pX1 && bY >= pY1 && bY <= pY2 && vX > 0) // left edge
    {
      if (bX + bRad > pX1) // will collide with edge
      {
        ball.setX(pX1 - bRad);
        ball.setXVector(-Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bX > pX2 && bY >= pY1 && bY <= pY2 && vX < 0) // right edge
    {
      if (bX - bRad < pX2) // will collide with edge
      {
        ball.setX(pX2 + bRad);
        ball.setXVector(Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bY < pY1 && bX >= pX1 && bX <= pX2 && vY > 0) // top edge
    {
      if (bY + bRad > pY1) // will collide with edge
      {
        ball.setY(pY1 - bRad);
        ball.setYVector(-Math.abs(vY)); // Change y direction
        return true;
      }
    } else if (bY > pY2 && bX >= pX1 && bX <= pX2 && vY < 0) // bottom edge
    {
      if (bY - bRad < pY2) // will collide with edge
      {
        ball.setY(pY2 + bRad);
        ball.setYVector(Math.abs(vY)); // Change y direction
        return true;
      }
    } else // Otherwise test to see if the ball hits a corner of the paddle
    {
      if (ball.distance(pX1, pY1) <= bRad
          && ball.distance(pX1, pY1) <= ball.distance(pX2, pY1)) { // top-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY1 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY1) <= bRad) { // top-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY1 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY2) <= bRad
          && ball.distance(pX2, pY2) <= ball.distance(pX1, pY2)) { // bottom-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY2 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX1, pY2) <= bRad) { // bottom-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY2 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      }
    }
    return false;
  }