So taking advice from fellow jgo’ers I ditched the Box2d for my breakout clone. I’m just trying to learn and further my experience with java and libgdx. I’ve setup a simple basic collision system which uses the center of the ball to determine which side the ball is hitting the brick on. It still doesn’t seem right though. It does work, but some of the collisions mess up as in going through bricks sometimes, bouncing the wrong direction, or sliding through the brick. The same happens with my ball and paddle. I know there are many ways to do this and i’ve done a bit of research on it but i’m having a hard time determining which to use and how to implement some of them.
I’ll include my collision code. I have a lot of refactoring to do for the whole project really, but i’ll post the link to my git as well.
public void CheckCollision() {
if (playerBall.getRect().overlaps(playerPaddle.getRect())) {
int first = (int) playerPaddle.getPos().x;
int second = (int) playerPaddle.getPos().x + 16;
int third = (int) playerPaddle.getPos().x + 32;
int fourth = (int) playerPaddle.getPos().x + 48;
if (playerBall.getPos().x + 16 > first && playerBall.getPos().x < second) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y * -1));
}
if (playerBall.getPos().x > second && playerBall.getPos().x < third) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x, playerBall.getSpeed().y * -1));
}
if (playerBall.getPos().x > third && playerBall.getPos().x < fourth) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x, playerBall.getSpeed().y * -1));
}
if (playerBall.getPos().x > fourth && playerBall.getPos().x < third + 16) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y * -1));
}
}
for (Brick tBrick : mManager.getBricks()) {
/*
if (tBrick.isAlive == false) {
mManager.destroyBrick(tBrick.getRect(),mManager.getBricks().indexOf(tBrick));
}
*/
if (playerBall.getRect().overlaps(tBrick.getRect())) {
System.out.println("Collision!");
int ballCenterX = (int)playerBall.getRect().x + (int)playerBall.getRect().getWidth() / 2;
int ballCenterY = (int)playerBall.getRect().y + (int)playerBall.getRect().getHeight() / 2;
Rectangle brickTopLeft = new Rectangle(
tBrick.getRect().x,
tBrick.getRect().y + tBrick.getRect().getHeight() / 2,
tBrick.getRect().getWidth(),
tBrick.getRect().getHeight()
);
Rectangle brickTopRight = new Rectangle(
tBrick.getRect().x + tBrick.getRect().getWidth() / 2,
tBrick.getRect().y + tBrick.getRect().getHeight() / 2,
tBrick.getRect().getWidth() / 2,
tBrick.getRect().getHeight() / 2
);
Rectangle brickBottomLeft = new Rectangle(
tBrick.getRect().x,
tBrick.getRect().y,
tBrick.getRect().getWidth() / 2,
tBrick.getRect().getHeight() / 2
);
Rectangle brickBottomRight = new Rectangle(
tBrick.getRect().x + tBrick.getRect().getWidth() / 2,
tBrick.getRect().y,
tBrick.getRect().getWidth() / 2,
tBrick.getRect().getHeight() / 2
);
//Ball collides with top left of brick
if (playerBall.getRect().overlaps(brickTopLeft)) {
if (ballCenterX > brickTopLeft.x && ballCenterY > brickTopLeft.y + brickTopLeft.height) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x, playerBall.getSpeed().y * -1));
tBrick.isAlive = false;
continue;
} else if (ballCenterY < brickTopLeft.y + brickTopLeft.height && ballCenterX < brickTopLeft.x) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y));
tBrick.isAlive = false;
continue;
}
}
//Ball collides with top right of brick
if (playerBall.getRect().overlaps(brickTopRight)) {
if (ballCenterX < brickTopRight.x + brickTopRight.width && ballCenterY > brickTopRight.y + brickTopRight.height) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x, playerBall.getSpeed().y * -1));
tBrick.isAlive = false;
continue;
}else if (ballCenterY < brickTopRight.y + brickTopRight.height && ballCenterX > brickTopRight.x + brickTopRight.width) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y));
tBrick.isAlive = false;
continue;
}
}
//Ball collides with bottom left of brick
if (playerBall.getRect().overlaps(brickBottomLeft)) {
if (ballCenterX > brickBottomLeft.x && ballCenterY < brickBottomLeft.y) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x,playerBall.getSpeed().y * -1));
tBrick.isAlive = false;
continue;
}else if (ballCenterX < brickBottomLeft.x && ballCenterY > brickBottomLeft.y) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y));
tBrick.isAlive = false;
continue;
}
}
//Ball collides with bottom right of brick
if (playerBall.getRect().overlaps(brickBottomRight)) {
if (ballCenterX < brickBottomRight.x + brickBottomRight.width && ballCenterY < brickBottomRight.y) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x, playerBall.getSpeed().y * - 1));
tBrick.isAlive = false;
continue;
}else if (ballCenterX > brickBottomRight.x + brickBottomRight.width && ballCenterY > brickBottomRight.y) {
playerBall.setSpeed(new Vector2(playerBall.getSpeed().x * -1, playerBall.getSpeed().y));
tBrick.isAlive = false;
continue;
}
}
}
}
mManager.bricksToDestroy();
}
Github:
Thanks for any suggestions, advice, or tips!