I'm building a Breakout Ball game like Arkanoid, and I have a big problem when ball collide with bricks. It happens when ball collide on the corner of brick (brickX,brickX) (brickX,brickX+brickLength) (brickY,brickY) (brickX+brickLength, brickY).
This is my code:
private void checkCollision() {
for(Brick brick : l.getCurrentLevel().getAllBricks()) {
double bx = b.getX();
if(brick.isShot())
continue;
if(b.getBounds().intersects(brick.getBounds())) {
if (
(b.getX() == brick.getX() || b.getX() == brick.getX() + brick.getLength())
&& (b.getY() >= brick.getY() && b.getY() <= brick.getY() + brick.getHeight())
)
b.setDx(-b.getDx());
else
b.setDy(-b.getDy());
if(brick.isDestroyable()) {
brick.shot();
b.setVelocity(b.getVelocity()+0.05);
}
break;
}
}
}