Simple 2D square collision

Hello, I am working on a simple Brick-breaker game for a programming class. I have been using a 2d array to spawn the objects (blocks), and have gotten collision detection for the ball and blocks to work. My problem is, it is only for a horizontal line across the brick. I cant seem to get the side detection of the block to work. This is the boolean for the collision detection. It is done inside of the brick-class itself, because I could not seem to call the brick's x's & y's because it is a 2D array.

  boolean collision () {
    if (ball.ballY >= y - (h /2 + 10) && ball.ballY <= y + (h /2) && ball.ballX >= x - (w / 2) && ball.ballX <= x + (w / 2)) {
     println("Hit ");
     y = -3000;
     ball.ballYSpeed = -ball.ballYSpeed;
      return true;
    } 


    if (ball.ballY >= y - (h /2 - 10) && ball.ballY <= y + (h /2 + 10) && ball.ballX >= x - (w / 2) && ball.ballX <= x + (w / 2)) {
     println("Hit");
     y = -3000;
     ball.ballYSpeed = -ball.ballYSpeed;
      return true;
    } 
    else {
      return false;
    }

  }
Sign In or Register to comment.