Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Array colision detection

This thread is locked; no one can reply to it. rss feed Print
Array colision detection
chaseC
Member #8,368
February 2007

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?:(

CursedTyrant
Member #7,080
April 2006
avatar

First answer: attach yer bloody code :P

You could use simple bounding box collisions.

1 
2struct sball
3{
4 int x, y; //ball's position
5 int r; //ball's radius
6} sball;
7 
8struct sbrick
9{
10 int x, y; //brick's position
11 int w, h; //brick's size
12} sbrick;
13 
14sball ball;
15sbrick brick[32][32]; //assuming it's a 32x32 2D array, change as needed
16 
17//...
18 
19for (int y=0; y<32; y++)
20{
21for (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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

Go to: