Collision Detection Issue - (Brick Breaker game)

Hi, I am working on a unique brick breaker type game and am having an issue with my collision detection.

I am trying to detect hits on all 4 corners and 4 sides of a brick. It works alright but is kind of glitch and sometimes acts like it hit a corner when it clearly hit close to
the center of a top or side of the brick and vice versa.

The easier way I can explain this is for you to play my game and notice it yourself (should not take long)
AtomicBlocks.jar Download Link

CONTROLS
F1 - Generates new level
WASD
Arrow Keys

Here is my current collision detection code.


Rectangle brickRect = aBlocks.get(i).getBounds();
				
				ABlock block = (ABlock) aBlocks.get(i);
				
				// If we hit on the left
				if ((ball.getX() + (ball.getWidth()/2)) < (brickRect.x))
				{
					ball.setDx(-Math.abs(ball.getDx()));
				}
				// If we hit on the right
				else if ((ball.getX()) > (brickRect.x + (brickRect.width/2))) 
				{
					ball.setDx(Math.abs(ball.getDx()));
				}
				// If we hit on the top
				if ((ball.getY() + (ball.getWidth()/2)) < (brickRect.y))
				{
					ball.setDy(-Math.abs(ball.getDy()));
				}
				// If we hit on the bottom
				else if ((ball.getY()) > (brickRect.y + (brickRect.height)/2)) 
				{
					ball.setDy(Math.abs(ball.getDy()));
				}

didnt run? am I doing something wrong :P.

also a thought (this may be wrong :P). but do you actually need the abs? cuase I get that you’re trying to be sure that it goes the right way, but it will always be opposite.

for example, if the ball hits the top of the brick, it HAS to be positive dy value right? so it just make it -ball.getDy() instead of abs.

I doubt this is your problem, just a thought :slight_smile:

Yea I was just trying to be sure of it, and while i was waiting for some responses I changed it a little and came up with some new collision code which seems to be doing the job as far as I can tell.


Rectangle bRect = aBlocks.get(i).getBounds();
				
				ABlock block = (ABlock) aBlocks.get(i);
				
				//TOP LEFT CORNER
				if((ball.getX() + ball.getCenterX()) <
						(bRect.x + (bRect.width/20)) &&
						(ball.getY() + ball.getCenterY()) < (bRect.y + (bRect.height/4)))
				{
					ball.setDx(-1);
					ball.setDy(-1);
				}
				//TOP
				if((ball.getX() + ball.getCenterX()) >
						(bRect.x + (bRect.width/20)) &&
						(ball.getX() + ball.getCenterX()) < (bRect.x + (bRect.width-(bRect.width/20))) &&
						(ball.getY() + ball.getCenterY()) < bRect.y)
				{
					ball.reverseDy();
				}
				//TOP RIGHT CORNER
				if((ball.getX() + ball.getCenterX()) >
				(bRect.x + (bRect.width-(bRect.width/20))) &&
						(ball.getY() + ball.getCenterY()) < (bRect.y + (bRect.height/4)))
				{
					ball.setDx(1);
					ball.setDy(-1);
				}
				//RIGHT SIDE
				if((ball.getY() + ball.getCenterY()) >(bRect.y + (bRect.height/4))&&
						(ball.getY() + ball.getCenterY()) < (bRect.y + (bRect.height - (bRect.height/4))) &&
						(ball.getX() + ball.getCenterX()) > (bRect.x + bRect.width))
				{
					ball.reverseDx();
				}
				//BOTTOM RIGHT CORNER
				if((ball.getX() + ball.getCenterX()) >
				(bRect.x + (bRect.width-(bRect.width/20))) &&
						(ball.getY() + ball.getCenterY()) > (bRect.y - (bRect.height-(bRect.height/4))))
				{
					ball.setDx(1);
					ball.setDy(1);
				}
				//BOTTOM
				if((ball.getX() + ball.getCenterX()) >
						(bRect.x + (bRect.width/20)) &&
						(ball.getX() + ball.getCenterX()) < (bRect.x + (bRect.width-(bRect.width/20))) &&
						(ball.getY() + ball.getCenterY()) > (bRect.y + bRect.height))
				{
					ball.reverseDy();
				}
				//BOTTOM LEFT CORNER
				if((ball.getX() + ball.getCenterX()) <
						(bRect.x + (bRect.width/20)) &&
						(ball.getY() + ball.getCenterY()) > (bRect.y + (bRect.height - (bRect.height/4))))
				{
					ball.setDx(-1);
					ball.setDy(1);
				}
				//LEFT SIDE
				if((ball.getY() + ball.getCenterY()) >(bRect.y + (bRect.height/4))&&
						(ball.getY() + ball.getCenterY()) < (bRect.y + (bRect.height - (bRect.height/4))) &&
						(ball.getX() + ball.getCenterX()) < bRect.x)
				{
					ball.reverseDx();
				}

But, the jar not working is kind of weird. It seems everyone except me (other friends) who try to double click it to run get a main class not found error and instead have to run it from the command line. however, I do not have this problem, I can run the jar by just double clicking it. Any thoughts on why this might be? (my jar is placed away from my .class files also)

Maybe different Java versions or incorrect manifest.

thats what I was thinking. I am running 1.7 whilst my friends were running 1.6.

I just did not think it mattered if I was not using any 1.7 specific stuff.