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.


// collides on x axis
boolean cx = Math.abs(x1 - x2) <= (w1 + w2);
// collides on y axis
boolean cy = Math.abs(y1 - y2) <= (h1 + h2);

//then change the if statements to use the booleans.

Excuse my stupidity, but I don’t get that? I don’t see how that will fix the problem as i’m doing


	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 );

Which is about the same thing…The title of this is a bit misleading I guess…My collision detection works, just not correctly all the time.

So you’re taking the x values of the ball and the brick and subtracting them and then seeing if the width of both added together is greater than that value?

I don’t want to just take that code and copy and paste it, i’m trying to figure out how this works to do it for myself. Like I said, this is a learner game…Everything else i’ve done has taught me a lot and I haven’t had as much of a problem…But this is bothering me pretty badly.

The code I provided takes the gap between the centres and checks if it’s less than the sum of the bounds.

Your code is a bit weird. You’re adding the width to the pos with one, and doing the same to the other and halving it. ??? I think that explains the problem well enough.

I just jumped into the discussion, but…

There is some very, very, simple trick:

  1. Move object on x axis by the amount it wants to move

  2. check whether collides, if true => collision on x axis

  3. Move object on y axis by the amount it wants to move

  4. check whether collides, if true => collision on y axis

That’s it :slight_smile:
Hope it helps. Not got that much time to read atm… :-\

Dude…That makes perfect sense and I think that will solve my problem. I didn’t even think about that. It seems the issue is that it detects more than one collision so when it’s moving, it changes direction twice, which puts it going the same way it was. I’m going to try that and let you know. Thanks!

That code was mostly from someone who helped me on here…I just copied and pasted it so I didn’t understand it. I was going to come back to it and break it down…But it caused the problem i’m having now, so I haven’t been able to get past it because I didn’t understand it.

I’m still getting the same issue.

I took a screen record of it, forgive my crappy recording software, very choppy.

Fast forward to around :39 and youll see part of what i’m talking about.

Can we see the new 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()))
					{
		
			
			
			
			
			boolean cx = Math.abs(ball.getRect().x - level.bricks[i].getRect().x) <= Math.abs(ball.getRect().width + level.bricks[i].getRect().width);
					
			boolean cy = Math.abs(ball.getRect().y - level.bricks[i].getRect().y) <= Math.abs(ball.getRect().height + level.bricks[i].getRect().height);
						
			if (cx)
			{
			    // 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: " + cx + " Y: " + cy);
			    
			    return;
			}
		
		
			if (cy)
			{
			    // 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: " + cx + " Y: " + cy);
			    
			    return;
			}
			
					//level.brickCount--;
			
					}		
	}
}
}