I’m making a brick breaker game. For collision detection I am using Rectangle.intersects(). I construct a new Rectangle from the Ball object, and Brick extends rectangle. My problem is that the collision points seem to be off set by a few pixles. When the ball collides with the top or left side of a brick it is colliding before it should and from the bottom and right side it is colliding later than it should. The pictures illustrate this. Here is the code for the brick detection method. This method is inside the Ball class. My question is why is this happening.
/**
* Check for brick collisions and adjust ball direction.
*/
public void detectBrick(Level level) {
Rectangle r1 = new Rectangle(x, y, radius * 2, radius * 2);
for (Brick b: Level.theBricks) {
if (r1.intersects(b)) {
if (y > b.y && y < b.y + Brick.BHEIGHT)
dx = -dx;
else if (x > b.x && x < b.x + Brick.BWIDTH)
dy = -dy;
else {
dx = -dx;
dy = -dy;
}
// level.remove(b); // remove the brick after its been hit
break;
}
}
}