1

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!

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
MrPako
  • 28
  • 4

1 Answers1

0

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

Rob85
  • 1,719
  • 1
  • 23
  • 46
  • My fps is set at 60. But I think the solution lies in seeing if my next ball position intercepts any block or paddle, and if it does set it to the closest wall and reverse the velocity. – MrPako Aug 16 '17 at 16:03
  • Incorrect! as stated above when you detect a collision you should set the position of the ball to previous position before the collision happened, then you can reverse the velocity allthough that won't take in to account angle of deflection. So you need to store the previous frame position each loop – Rob85 Aug 16 '17 at 16:08
  • Hmm. But in that way if the ball position was very far and the ball is very fast, then it can miss collisions. Actually, I finally figured it out and I will post my answer after I get everything working :D – MrPako Aug 16 '17 at 19:04