# breakout collision

• 03-08-2003
lambs4
breakout collision
I'm writing collision code for my breakout clone. Thanks to the people at allegro.cc I have gotten this far. Now I need more help with collision detection. I can find out if the ball is within the block and have it bounce off the left side, but that's all. anymore help?

Code:

for(x =0; x < MAX_ROW; x++)
{
for(y = 0; y < MAX_COL; y++)
{
if (ball.x >  blocks[x][y].x &&
ball.x <= blocks[x][y].width &&
ball.y >  blocks[x][y].y &&
ball.y <= blocks[x][y].height)
{
if(blocks[x][y].color > 0)
{
blocks[x][y].color = 0;
if(ball.x > blocks[x][y].x)
ball.dx = -BALL_DELTA; // bounce left
else if(ball.x <= blocks[x][y].width)
ball.dx = BALL_DELTA; // bounce right
else if(ball.y > blocks[x][y].y)
ball.dy = -BALL_DELTA; // bounce up
else if(ball.y <= blocks[x][y].height)
ball.dy = BALL_DELTA; // bounce down
numOfBlocks--;
}
}
}
}

• 03-08-2003
Sang-drax
Re: breakout collision
Quote:

Originally posted by lambs4

Code:

if (ball.x >  blocks[x][y].x &&
ball.x <= blocks[x][y].width &&
ball.y >  blocks[x][y].y &&
ball.y <= blocks[x][y].height)

should be:
Code:

if (ball.x >  blocks[x][y].x &&
ball.x <= blocks[x][y].x + blocks[x][y].width &&
ball.y >  blocks[x][y].y &&
ball.y <= blocks[x][y].y+blocks[x][y].height)

• 03-08-2003
lambs4
I do add the blocks x and width when I create the levels.
• 03-08-2003
Sang-drax
Then you shouldn't call the variable 'width' -- it is very confusing.
• 03-08-2003
Sang-drax
Code:

if(ball.x > blocks[x][y].x)
That condition will always be true. Try to follow and interpret your code.
• 03-13-2003
lambs4
Ok, I've gotten the collision code written for the blocks and and paddle. The new problem is that the ball gets stuck bouncing within the paddle. Any reason why this occurr?

Code:

void checkCollision(void)
{
int x,y,i;
// check ball and block collision
for(x =0; x < MAX_ROW; x++)
{
for(y = 0; y < MAX_COL; y++)
{
if (ball.x > blocks[x][y].x &&
ball.x <= blocks[x][y].x+blocks[x][y].width &&
ball.y > blocks[x][y].y &&
ball.y <= blocks[x][y].y+blocks[x][y].height)
{
if(blocks[x][y].color > 0)
{
if(ball.x+ball.width >= blocks[x][y].x+blocks[x][y].width)
ball.dx = -ball.dx;
if(ball.x+ball.width <= blocks[x][y].x+blocks[x][y].width)
ball.dx = ball.dx;

ball.dy = -ball.dy;

blocks[x][y].color = 0;
numOfBlocks--;
playSound(BALLHITBLOCK);
}
}
}
}
// check ball and paddle collision