STILL having trouble determining which side collided..

Hey guys, i’ve been working on this for some time now. I got a new job and haven’t been able to put as much time in lately, but I can’t get it figured out.

I posted another time on the issue, and someone told me to align the ball with the brick…I tried but cannot get that to work either. I feel like this shouldn’t be as hard as it is, and it’s very disheartening…if I can’t get this figured out, how the hell am I going to program anything of use? ya know?

Anyways, it’s my brickbreaker/breakout game. I’m using LibGDX and i’m using rectangles for the ball and the brick…I feel like I should have used a circle for the ball instead, but oh well. I just want to get this game completely finished so I can move on to something else. This is just a “learner” game.

Anyways, collision detection on the top and bottom seem to be working just fine…But when it hits the side, it acts weird and bounces incorrectly…The corners act a little odd as well…

Here is my collision detection code:


public void checkBrickCollision()
	{
		for (int i=0;i<level.brickCount;i++) {
				
				if (level.bricks[i].GetVisible() == true) {
					if (level.bricks[i].getRect().overlaps(ball.getRect()))
					{
		
			
			
			int xd = (int) Math.abs( ((ball.ballRect.x + ball.ballRect.width) - (level.bricks[i].brickRect.x + level.bricks[i].getRect().width)) /2 );
			int yd = (int) Math.abs( ((ball.ballRect.y + ball.ballRect.height) - (level.bricks[i].brickRect.y + level.bricks[i].getRect().height)) /2 );
			
			if (xd > yd)
			{
			    // Collision on top or bottom, reverse y velocity
				
			    ball.ballSpeedY = -ball.ballSpeedY;
			    Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
			    scoreList.add(score);
			    level.bricks[i].Destroy();
			    //level.brickCount--;
			    System.out.println("Top/Bottom X: " + xd + " Y: " + yd);
			    
			    return;
			}
		
		
			if (yd > xd)
			{
			    // Collision on left or right, reverse x velocity
			    
				ball.ballSpeedX = -ball.ballSpeedX;
			    
			    Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
			    scoreList.add(score);
			    level.bricks[i].Destroy();
			    //level.brickCount--;
			    System.out.println("Left/Right X: " + xd + " Y: " + yd);
			    
			    return;
			}
			
			if (xd == yd)
			{
			    // Collision on corners, reverse both
			    ball.ballSpeedX = -ball.ballSpeedX;
			    ball.ballSpeedY = -ball.ballSpeedY;
			    Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
			    scoreList.add(score);
			    level.bricks[i].Destroy();
			    //level.brickCount--;
			    System.out.println("Corners");
			    
			    return;
			}			
		
					//level.brickCount--;
			
					}		
	}
}
}

I just want to get past this issue so I can continue learning other things. I haven’t done any new work to the game in a while because of this…

The ball is so unrealistic…It only bounces at a 45 degree angle.