collision detection in a simple breakout game

74 Views Asked by At

Im making a simple breakout game in java as a university assignment and i need desperate help in making the squares above have hit detection. The details for the assignement are that there needs to be atleast 3 rows in which each row have different amount of hits. For example, the first row needs 1 hit to dissapear, then the 2nd row need 2 hits and 3rd row need 3. I think you get the point but thats roughly what i need.

I have made a class called SquareCollection that does not extend anything. In SquareCollection i have made three arrays that i will paste below along with the rest. Those three i have already drawed on the main class and now all i need is the collision detection with the ball.

package Game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class SquareCollection {

    private int hits;
    private ArrayList<ColoredBox> row1; 
    private ArrayList<ColoredBox> row2;
    private ArrayList<ColoredBox> row3; 

    public SquareCollection() {
        //super(x, y, Constants.SquareWidth, Constants.SquareHeight);
        hits = 0;

        row1=new ArrayList<ColoredBox>();
        for (int i = 0; i < 8; i++) {
            row1.add(new ColoredBox(i*100 +20, 20,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }

        row2=new ArrayList<ColoredBox>();
        for (int j = 0; j < 8; j++) {
            row2.add(new ColoredBox(j*100 +20, 80,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }

        row3=new ArrayList<ColoredBox>();
        for (int k = 0; k < 8; k++) {
            row3.add(new ColoredBox(k*100 +20, 140,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }
    }

    public void update(Keyboard keyboard) {

    }
    public void draw(Graphics2D graphics) {
        for(int i = 0;i < row1.size();i++) {
            row1.get(i).draw(graphics);
        }
        for(int j = 0; j < row1.size(); j++) {
            row2.get(j).draw(graphics);
        }
        for(int k = 0; k < row1.size(); k++) {
            row3.get(k).draw(graphics);
        }
    }   
}

I understand that this is a quite trivial queation but i am very new to coding and Java in general so forgive me if this is "too easy" but my brain simply has a hard time with this. Also please inform me if have "code dumped" and if it is hard to read. I will gladly try to help if you need details.

EDIT: I forgot to mention that i already have collison between the ball and the "raquet" (the tile that the player is moving). I also have collision on the walls and its as simple as an if function where if the balls x position is less that 0 then it will simply put the direction to -1 so it bounces naturally. If its too unclear i will also share this code.

1

There are 1 best solutions below

2
libik On

If the game does not have to be perfect - which seems it does not have to as it is some small fun project in school - you can "wrap" the ball to invisible square. Then it is very easy to detect collision of that square and any other boxes.

For example here you can see how to do rectangle collision: Collision detection between two rectangles in java there is simple answer that suggest this solution:

if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
    RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)