1
votes

So I am making a breakout game in android studio.

And after making a game loop and basic collisions with the paddle and bricks I've noticed that my ball has started to clip through my bricks and paddle after increasing the speed of it.

The problem lies that it collides two times in a frame, so my basic collision is not enough for it.

I've read about using vector math for a solution. But it's too much for me too wrap my head around it. I'm referring to answer of this question: Refer Here

if (RectF.intersects(bricks[i].getRect(), ball.getRect())) {

Brick brick = bricks[i];

if(ball.getRect().centerX()>brick.minX() && ball.getRect().centerX() < brick.maxX()){

    ball.reverseYVelocity();

    if(ball.getRect().top > brick.getRect().centerY()){

        ball.clearObstacleY(brick.getRect().bottom - ball.getRect().height());

    } else {

        ball.clearObstacleY(brick.getRect().top + ball.getRect().height());

    }

} else {

ball.reverseXVelocity();

if(ball.getRect().left > brick.getRect().centerX()){

    ball.clearObstacleX(brick.getRect().right + ball.getRect().width());

} else {

ball.clearObstacleX(brick.getRect().left - ball.getRect().width());

}
}

And my ball update is:

public void update(long fps, float speed){
    rect.left    = rect.left   + (xVelocity*speed / fps);
    rect.top     = rect.top    + (yVelocity*speed / fps);
    rect.right   = rect.left   + ballWidth;
    rect.bottom  = rect.top    - ballHeight;
}

So how would I transform this into something like this: Demo

Thanks in advance!

1

1 Answers

0
votes

on every loop you should keep a record of the previous position of the ball. when you detect a collision you should immediately move the ball to the previous frame position and then deal with the bounce effect.it should never register a collision twice this means your not handling the collision correctly. if you are saying that when you increase the speed the ball appears to go through the paddle then you either need look at how you work out velocity or increase your frame rate. Whats your FPS set at?

you can also see my answers here and here