# breakout style game collision detection

• 10-25-2002
m00se123
breakout style game collision detection
i have gotten the paddle to ball and the wall collisions working, but i can't get the ball to block collisions working good. I think the prob is because i am usualy moving the ball 5 units at a time. I am trying using a loop so if i move 5 units at a time i test for each 5 but only draw at the end, but this still does not work.
here is some of the code

Code:

```        for (i =0; i< 6; i++)         {           block_collision_detection();                   textprintf(screen, font, 2,           ball.x += ball.x_speed/6;           ball.y += ball.y_speed/6;         } void block_collision_detection() {         int x,y;         for (x=0; x<SCRX/BLOCK_W; x++)         {                 for (y=0; y<SCRY/BLOCK_H-2; y++)                 {                         if (map.block[x][y].active && BBCD(ball.bounding_box, map.block[x][y].bounding_box))                         {                                 //find out which side of the block the bounce is on and bounce                                 if (!block_hit)                                 {                                         if (ball.bounding_box.bottom >= map.block[x][y].bounding_box.top && ball.bounding_box.bottom <= map.block[x][y].bounding_box.bottom && ball.bounding_box.left >= map.block[x][y].bounding_box.left && ball.bounding_box.right <= map.block[x][y].bounding_box.right) ball.bounce(TOP, xblip);                                         else if (ball.bounding_box.top <= map.block[x][y].bounding_box.bottom && ball.bounding_box.top >= map.block[x][y].bounding_box.top && ball.bounding_box.left >= map.block[x][y].bounding_box.left && ball.bounding_box.right <= map.block[x][y].bounding_box.right) ball.bounce(BOTTOM, xblip);                                         else ball.bounce(LEFT, xblip);                                         map.block[x][y].del();                                         block_hit = true;                                 }                         }                 }         }         return; }```
• 10-26-2002
Magos
I've made a breakout-like game. It's on my homepage (named Ball), source code included.
It's not the best collision detection, especially not in higher velocities, but it works.
Basically, you split the block into 4 triangles and check which one the ball is in (when overlapping).
Code:

```+------+ |\    /| | \  / | |  \/  | |  /\  | | /  \ | |/    \| +------+```
• 10-29-2002
mickey
you could try checking before the ball hits the block,

ie,

if(ball'snextposition >= block's min width/height etc., && <= blocks' max widh height etc.,)
bounce back the ball;
• 10-30-2002
Magos
Quote:

Originally posted by mickey
bounce back the ball;
In which direction then? If it's coming from the bottom right, 45 degrees straight at the corner of the block. Bounce down, or bounce right???
• 10-30-2002
mickey
huh? it's upto you where you want the ball's next direction be??
• 10-31-2002
Magos
Your algorithm only checks whether the ball overlaps the block or not, nothing of which direction it should bounce. The resulting direction may seem logical to an intelligent human, but not to a computer. Should you reverse DX? DY? Or both?
• 10-31-2002
biosninja
Maybe just check if the cooridiates of the ball and the range of the paddles' front side is the same