This post is a portion of the Appendix in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.

I don’t really have any other place to put this one. Simply helping someone through some code issues.

As a follow up (even though I am typing this before the video is done processing) I wanted to provide the code for a more accurate “flat” based collision detection. Once again, this is not perfect. If I wanted better detection, I would go into my “else” for determining the collision and test for each of the four possible corners. Read the code below if what I just said didn’t make any sense:

if(ball.x - ball.boundx < brick[i].x + brick[i].boundx &&
 ball.x + ball.boundx > brick[i].x - brick[i].boundx &&
 ball.y - ball.boundy < brick[i].y + brick[i].boundy &&
 ball.y + ball.boundy > brick[i].y - brick[i].boundy)
{
	if(abs(ball.x - brick[i].x) < 15) //Verticall collision
	{
		if(ball.y < brick[i].y)
			ball.dirY = abs(ball.dirY) * -1;
		else
			ball.dirY = abs(ball.dirY);
	}
	else if(abs(ball.y - brick[i].y) < 15) //Horizontal collision
	{
		if(ball.x < brick[i].x)
			ball.dirX = abs(ball.dirX) * -1;
		else
			ball.dirX = abs(ball.dirX);
	}
	else  //Diagonal collision
	{
		ball.dirX *= -1;
		ball.dirY *= -1;
	}

	brick[i].count--;
	al_play_sample_instance(instance2);

	if(totalBricks < 40 && totalBricks % 10 == 0)
	{
		ball.velX += 1;
		ball.velY += 1;
	}
}