@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;
      }
    }
  }
 private void logic(int delta) {
   ball.update(delta);
   bat.update(delta);
   if (ball.getX() <= bat.getX() + bat.getWidth()
       && ball.getX() >= bat.getX()
       && ball.getY() >= bat.getY()
       && ball.getY() <= bat.getY() + bat.getHeight()) {
     ball.setDX(0.3);
   }
 }
 public boolean collidesWith(Ball ball) {
   boolean hasEnteredIf = false;
   if (ball.getX() - ball.getRadius() <= this.x1 || ball.getX() + ball.getRadius() >= this.x2) {
     ball.reflectHorizontal();
     hasEnteredIf = true;
   }
   if (ball.getY() - ball.getRadius() <= this.y1 || ball.getY() + ball.getRadius() >= this.y2) {
     ball.reflectVertical();
     hasEnteredIf = true;
   }
   return hasEnteredIf;
 }
Beispiel #4
0
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2D) g;

    graphics.setPaint(Color.GREEN);

    Ellipse2D.Double circle =
        new Ellipse2D.Double(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());

    // graphics.fillOval(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());
    graphics.fill(circle);
    if (frame.getWidth() != frameWidth || frame.getHeight() != frameHeight) {
      repaint();
      updateSizes(frame.getWidth(), frame.getHeight());
    }

    try {
      Thread.sleep(7);
    } catch (InterruptedException e) {
    }
    if (begin && !grabBall) {
      moveBall();
    } else if (grabBall) {
      followMouse();
    }
  }
Beispiel #5
0
 public boolean isOver(Ball ball) {
   double rX = Math.pow(ball.getX() - x, 2);
   double rY = Math.pow(ball.getY() - y, 2);
   double rR = Math.pow(radius - ball.radius, 2);
   ballOver = (rX + rY) <= rR;
   return ballOver;
 }
Beispiel #6
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();
  }
Beispiel #7
0
 @Override
 public void paint(Graphics g) {
   super.paint(g);
   for (Ball b : ballsList) {
     g.setColor(b.getColor());
     g.fillOval(b.getX(), b.getY(), b.getSize(), b.getSize());
     b.move(getWidth(), getHeight());
   }
 }
Beispiel #8
0
 public boolean attack(Ball ball) {
   int angle = this.calculateAngle(this.getX(), this.getY(), ball.getX(), ball.getY());
   GameInfo.currentMap[this.getY() / Config.slotHeight][this.getX() / Config.slotWidth] =
       this.getMapID() + angle;
   GameManager gameManager = GameManager.getInstance();
   int id = BallCache.generateBallID();
   gameManager.addBall(this.getBulletName(), this.getX(), this.getY(), ball, id);
   SendWrapper.sendBallAction(this, "TOWERATTACK");
   return true;
 }
Beispiel #9
0
 private void saveBalls(Board board) {
   List<Ball> balls = board.getBalls();
   for (Ball ball : balls) {
     fileOutput.format(
         BALL_FORMAT,
         giveName(ball),
         ball.getX(),
         ball.getY(),
         ball.getXVelocity(),
         ball.getYVelocity());
   }
 }
Beispiel #10
0
    public void checkForHit() {

      // change ball speed when ball hits paddle
      if (ball.getShape().intersects(paddle.getShape())) {
        int leftSide = paddle.getX();
        int middleLeft = paddle.getX() + (int) (paddle.getWidth() / 3);
        int middleRight = paddle.getX() + (int) (2 * paddle.getWidth() / 3);
        int rightSide = paddle.getX() + paddle.getWidth();

        if ((ball.getX() >= leftSide) && (ball.getX() < middleLeft)) {
          // change ball speed
          ball.setXspeed(-2);
          ball.setYspeed(-2);
        }
        if ((ball.getX() >= middleLeft) && (ball.getX() <= middleRight)) {
          // change ball speed
          ball.setYspeed(-2);
        }
        if ((ball.getX() > middleRight) && (ball.getX() <= rightSide)) {
          // change ball speed
          ball.setXspeed(2);
          ball.setYspeed(-2);
        }
      }

      // change ball speed when ball hits brick
      for (int i = 0; i < bconfig.getRows(); i++) {
        for (int j = 0; j < bconfig.getCols(); j++) {
          if (bconfig.exists(i, j)) {
            if (ball.getShape().intersects(bconfig.getBrick(i, j).getShape())) {
              Point ballLeft =
                  new Point(
                      (int) ball.getShape().getX(),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballRight =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth()),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballTop =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) ball.getShape().getY());
              Point ballBottom =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight()));
              if (bconfig.getBrick(i, j).getShape().contains(ballLeft)) {
                // change ball speed
                ball.setXspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballRight)) {
                // change ball speed
                ball.setXspeed(-2);
                score++;
              }
              if (bconfig.getBrick(i, j).getShape().contains(ballTop)) {
                // change ball speed
                ball.setYspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballBottom)) {
                // change ball speed
                ball.setYspeed(-2);
                score++;
              }

              // remove brick
              bconfig.removeBrick(i, j);
            }
          }
        }
      }
    }
Beispiel #11
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;
  }