Still can't seem to get my collision detection right

I’m using rectangles and checking bounds when they overlap…Sometimes, a whole row of brick disappears! When the brick is hit is sets isVisible to false and I have a check setup so that when it is not visible it takes 1 away from brickcount. Here is my collision code:


		if (brick.GetVisible()==true)
		{
			
		
		if (brick.brickRect.overlaps(ballRect))
		{
		
			if (ballRect.x + ballRect.width >= brick.brickRect.x && ballRect.x < brick.brickRect.x) { 
				ballSpeedX = -ballSpeedX;
				brick.isVisible = false;
				System.out.println("Left Side");
			}
			if (ballRect.x <= brick.brickRect.x+brick.brickRect.width && ballRect.x > brick.brickRect.x) {
				ballSpeedX = -ballSpeedX;
				brick.isVisible = false;
				System.out.println("Right Side");
			}
			if (ballRect.y + ballRect.height >= brick.brickRect.y && ballRect.y < brick.brickRect.y + brick.brickRect.height) {
				ballSpeedY = -ballSpeedY;
				brick.isVisible = false;
				System.out.println("Bottom Side");
			}
			if (ballRect.y <= brick.brickRect.y+brick.brickRect.height && ballRect.y > brick.brickRect.y + brick.brickRect.height) {
				ballSpeedY = -ballSpeedY;
				brick.isVisible = false;
				System.out.println("Top Side");
			}

Not that it should matter, but you have a duplicated line if your last if.

It is hard to debug other peoples code, but I would guess that your error is in either the overlaps function.

Try this to see how many collisions you register:



      if (brick.brickRect.overlaps(ballRect))
      {
            System.out.println("Collision detected");
           
             //rest of code here
       }

I put message out for each side that is hit, and that is definitely the problem. It’s calling them way too much and i cant figure out why.

Well your problem is most likely in your overlap function then, not this if statement.

There is no overlap function. It’s built in. I’m using LibGDX. I just type if rect1.overlaps(rect2)

Could it be due to not testing the x & y at the same time?

For example, let’s say you have a row of bricks all at the same X location but different Y locations. When you test only for the X location, ALL the bricks in the same column will go invisible, since the Y is not taken into consideration.

I don’t think so. I’ve tried this now and it still isn’t working. It’s calling it too much and taking a row at a time off.


	int brickLeft = (int)brick.brickRect.x;
		int brickRight = (int)brick.brickRect.x +(int)brick.brickRect.width;
		int brickTop = (int)brick.brickRect.y + (int)brick.brickRect.height;
		int brickBottom = (int)brick.brickRect.x;
		
		
		if (brick.GetVisible()==true)
		{
			
		
		
		//if (brick.brickRect.(ballRect))
		if (ballRect.overlaps(brick.brickRect))
	
		
		{
		
		
			//Left Collision
			if (ballRight > brickLeft && ballLeft < brickLeft && ballTop > brickBottom && ballBottom < brickTop) {
				ballSpeedX = -ballSpeedX;
			brick.isVisible = false;
			System.out.println("Left Side");
			return;
		}
			//Right
			if (ballLeft < brickRight && ballRight > brickRight && ballTop > brickBottom && ballBottom < brickTop) {
				ballSpeedX = -ballSpeedX;
			brick.isVisible = false;
			System.out.println("Right Side");
			return;
		}
				
			//Top
			if (ballBottom < brickTop && ballTop > brickTop && ballRight > brickLeft && ballLeft < brickRight) {
				ballSpeedY = -ballSpeedY;
			brick.isVisible = false;
			System.out.println("Top Side");
			return;
			
		}
			
			//Bottom
			if (ballTop > brickBottom && ballBottom < brickBottom && ballRight > brickLeft && ballLeft < brickRight) {
				ballSpeedY = -ballSpeedY;
			brick.isVisible = false;
			System.out.println("Bottom Side");
			//return;
		}
	

I got it for the most part. Just set up some Vector variables for the top,left, right, and bottom of the brick and just checked to see which part was in the brick. Here is my code. I still have a problem with the right and left side collisions, but i’m getting it worked out.


public void checkBrickCollision(Brick brick)
	{
		
		
		
		int ballLeft = (int)ballRect.x;
		int ballRight = (int)ballRect.x + (int)ballRect.width;
		int ballTop = (int)ballRect.y + (int)ballRect.height;
		int ballBottom = (int)ballRect.x;
		
		
		
		
		Vector2 Top = new Vector2(ballLeft + (ballRect.width / 2),ballTop );
		Vector2 Bottom = new Vector2(ballLeft + (ballRect.width/2),ballBottom);
		Vector2 Right = new Vector2(ballRight,ballBottom + ballRect.height/2);
		Vector2 Left = new Vector2(ballLeft,ballBottom + ballRect.height/2);
		
		
		int brickLeft = (int)brick.brickRect.x;
		int brickRight = (int)brick.brickRect.x +(int)brick.brickRect.width;
		int brickTop = (int)brick.brickRect.y + (int)brick.brickRect.height;
		int brickBottom = (int)brick.brickRect.x;
		
		
		if (brick.GetVisible()==true)
		{
			
		
		
		//if (brick.brickRect.(ballRect))
		if (ballRect.overlaps(brick.brickRect))
	
		
		{
		
			
			
			if (brick.brickRect.contains(Bottom.x,Bottom.y)) {
				ballSpeedY = -ballSpeedY;
				brick.isVisible = false;
			}
			if (brick.brickRect.contains(Top.x,Top.y)) {
				ballSpeedY = -ballSpeedY;
				brick.isVisible = false;
			}
			if (brick.brickRect.contains(Left.x,Left.y)) {
				ballSpeedX = -ballSpeedX;
				brick.isVisible = false;
			}
			if (brick.brickRect.contains(Right.x,Right.y)) {
				ballSpeedX = -ballSpeedX;
				brick.isVisible = false;
			}
				
		}
		
		}	
		
	
	}