Hi, I'm making a breakout clone but I've run into a problem with the collision detection. I'm using an array to store the positions of all the bricks but I don't know how to check to see if the ball has hot them.
Can someone help please?:(
First answer: attach yer bloody code 
You could use simple bounding box collisions.
| 1 | |
| 2 | struct sball |
| 3 | { |
| 4 | int x, y; //ball's position |
| 5 | int r; //ball's radius |
| 6 | } sball; |
| 7 | |
| 8 | struct sbrick |
| 9 | { |
| 10 | int x, y; //brick's position |
| 11 | int w, h; //brick's size |
| 12 | } sbrick; |
| 13 | |
| 14 | sball ball; |
| 15 | sbrick brick[32][32]; //assuming it's a 32x32 2D array, change as needed |
| 16 | |
| 17 | //... |
| 18 | |
| 19 | for (int y=0; y<32; y++) |
| 20 | { |
| 21 | for (int x=0; x<32; x++) |
| 22 | { |
| 23 | if (ball.y-ball.r<=brick[x][y].y+brick[x][y].h && ball.x+ball.r>brick[x][y].x & ball.x+ball.r<brick[x][y].x+brick[x][y].w) //check the top bound of the ball versus the bottom bound of the brick |
| 24 | { allegro_message("Hit!"); } |
| 25 | if (ball.x-ball.r<=brick[x][y].x+brick[x][y].w && ball.y-ball.r>brick[x][y].y && ball.y+ball.r<brick[x][y].y) //check the left bound of the ball versus the right bound of the brick |
| 26 | { allegro_message("Hit!"); } |
| 27 | |
| 28 | //... and so on ... |
| 29 | |
| 30 | } |
| 31 | } |
It's not tested, so it may contain mistakes.