Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Collision detection, Arrays and the state of the union

This thread is locked; no one can reply to it. rss feed Print
Collision detection, Arrays and the state of the union
joshua.tilson
Member #7,552
July 2006
avatar

Good morning!

I have a couple of questions, one simple, one not so simple. Any way, I have been working with arrays to represent tile maps.

 int map[32][32];
for (int i=0;i<32;i++){
for (int j=1;j<32;j++){
map<i>[j] = rand()%2;}}

Now here i can easily set all my array elements to random numbers (this reflects my next question), But How do i initialize the array with all 1024 elements set as I want them? IE: trying to plan out a tilemap that isn't random?? I have seen examples like

 int map[32][32] = {{1,1,1,1,1,1,1,1,1,1}}
{{1,1,1,1,1,1,1,1}}
{{.........}}

But that has never worked for me i always get a compile error!

The next subject i have come to is collision detection, this seems to be the hardest for me so far. LELete clarify, When I adhad aet map in my pong game, i could easily do the cocollisionetection because i always knew where the walls and paddles would be, but with a random tileset like my above example, how do i tell where the tiles are in my code?
What i mean is this, i have the code that makes a ball move, therefore i will always be able to check the position of the ball, but how do i detect for the placement of random collidable tiles?

Thanks for stopping by and taking a look!

Josh

Archon
Member #4,195
January 2004
avatar

Quote:

But How do i initialize the array with all 1024 elements set as I want them?

'new' them and then iterate through them.

Quote:

but with a random tileset like my above example, how do i tell where the tiles are in my code?

Use position variables to represent where you are colliding. Then match the position of your chracter against the tilemap.

LennyLen
Member #5,313
December 2004
avatar

Quote:

But that has never worked for me i always get a compile error!

Well, in the example you just gave, you missed the commas between the sets. Try compiling the following and see what happens.

1#include <stdio.h>
2 
3int map[10][10] =
4 {{1, 2, 3, 2, 1, 2, 3, 6, 3, 5},
5 {8, 2, 3, 2, 1, 2, 3, 6, 3, 5},
6 {1, 2, 3, 2, 6, 2, 3, 6, 3, 5},
7 {2, 2, 4, 2, 1, 4, 3, 6, 1, 5},
8 {1, 2, 8, 2, 1, 2, 3, 6, 3, 5},
9 {1, 2, 3, 2, 1, 2, 3, 3, 5, 8},
10 {1, 2, 3, 2, 1, 2, 3, 6, 3, 5},
11 {8, 2, 3, 8, 1, 2, 3, 9, 3, 5},
12 {1, 2, 3, 2, 1, 2, 3, 6, 3, 5},
13 {1, 2, 3, 2, 1, 2, 3, 6, 3, 5}};
14
15int x, y;
16
17int main() {
18
19 for (x = 0; x < 10; x++) {
20
21 for (y = 0; y < 10; y++) {
22
23 printf("%d ", map[x][y]);
24
25 }
26
27 printf("\n");
28
29 }
30
31 return 0;
32
33}

By the way, this is a valid way of assing values to arrays, but it means you have to recompile your game every time you change a map. It's better to load the values from a file.

joshua.tilson
Member #7,552
July 2006
avatar

Lenny,

Perfect thank you, The previous examples i had seen had shown ; instead of , So that explains my problem there.

well let me show, whoever is interested, My lame attempt at collision detection, it works about 15%...

1/* game design and psuedo code
2** initialize game
3** set up screen
4** draw paddle, ball and bricks
5** player launches ball
6** if ball is not below paddle
7** then move ball
8** else if ball collides with brick
9** then remove brick and add points
10** check to see if ball is below paddle or if all bricks are gone
11** then end game.
12**
13**
14** drawscreen()
15** collision()
16** checkball pos()
17**
18** screen is 20x15tiles X,Y
19*/
20 
21#include <allegro.h>
22 
23BITMAP *ball;
24BITMAP *paddle;
25BITMAP *background;
26BITMAP *brick;
27BITMAP *buf;
28 
29const int MIN_BALL_X = 5;
30const int MAX_BALL_X = 635;
31const int MIN_BALL_Y = 0;
32const int MAX_BALL_Y = 480;
33
34int paddleX = 275;
35int padTemp;
36int ballX, ballY;
37int dx = 0;
38int dy = 0;
39int ballTempX, ballTempY;
40int brickMap[20][13];
41 
42bool ballOn = true;
43 
44void init();
45void deinit();
46void drawScreen();
47int collide();
48bool movePaddle();
49void moveBall();
50void startBall();
51void updateScreen();
52 
53 
54 
55int main() {
56 init();
57 drawScreen();
58 while (!key[KEY_ESC]) {
59 movePaddle();
60 updateScreen();
61 moveBall();
62 }
63 
64 deinit();
65 return 0;
66}
67END_OF_MAIN()
68 
69 
70
71void drawScreen() {
72 background = load_bitmap("background.bmp", NULL);
73 paddle = load_bitmap("paddle.bmp", NULL);
74 ball = load_bitmap("ball.bmp", NULL);
75 brick = load_bitmap("brick.bmp",NULL);
76 draw_sprite(screen, background, 0, 0);
77 draw_sprite(screen, paddle, 275, 450);
78 draw_sprite(screen, ball, 314, 438);
79
80
81 for (int i=0; i <20; i++)
82 for (int j=0; j <13; j++)
83 { if (brickMap<i>[j] == 1)
84 draw_sprite(screen, brick, i * 32, j * 32);
85 }
86 rest(5);
87 }
88
89bool movePaddle() {
90 if (key[KEY_LEFT] && paddleX != 0)
91 {--paddleX;
92 if(ballOn ==true){ --ballX;}
93 }
94 else if (key[KEY_RIGHT] && paddleX + 90 != 640)
95 { ++paddleX;
96 if(ballOn == true) {++ballX;}
97 }
98 else if (key[KEY_SPACE] && ballOn == true) {
99 startBall();
100 ballOn=false;
101 }
102 return true;
103 }
104
105void startBall(){
106 dx= -1;
107 dy= 3;
108 }
109
110void moveBall() {
111 int ballCollisionX, ballCollisionY;
112 ballTempX=ballX;
113 ballTempY=ballY;
114
115 ballCollisionX = ballX / 32;
116 ballCollisionY = ballY /32;
117 if (brickMap[ballCollisionX][ballCollisionY] == 1)
118 {dy = -dy;}
119 if (dy > 0 && ballX > paddleX && ballX < paddleX + 90 && ballY == 438)
120 {
121 dy = -dy; }
122 else
123 
124 if (dx < 0)
125 {
126 if (ballX + dx > MIN_BALL_X)
127 {
128 ballX += dx;
129 }
130 else
131 {
132 ballX = 2 * MIN_BALL_X - dx - ballX;
133 dx = -dx;
134 }
135 }
136
137 else // dx > 0
138 {
139 if (ballX + dx < MAX_BALL_X)
140 {
141 ballX += dx;
142 }
143 else
144 {
145 ballX = 2 * MAX_BALL_X - dx - ballX;
146 dx = -dx;
147 }
148 }
149 
150 if (dy < 0 )
151 {
152 if (ballY + dy > MIN_BALL_Y )
153 {
154 ballY += dy;
155 }
156 else
157 {
158 ballY = 2 * MIN_BALL_Y - dy - ballY;
159 dy = -dy;
160 }
161 }
162
163 else // dy > 0
164 {
165 if (ballY + dy < MAX_BALL_Y)
166 {
167 ballY += dy;
168 }
169 else
170 {
171 ballY = 2 * MAX_BALL_Y - dy - ballY;
172 dy = -dy;
173 }
174
175 }
176
177}
178
179void updateScreen() {
180 padTemp= paddleX;
181 buf = create_bitmap(90,26);
182 draw_sprite(buf, paddle, 0, 0);
183 draw_sprite(screen, buf, paddleX, 450);
184 destroy_bitmap(buf);
185 buf = create_bitmap(12,12);
186 draw_sprite(buf, ball, 0,0 );
187 draw_sprite(screen, buf, ballX, ballY);
188 rest(5);
189 }
190void init() {
191 int depth, res;
192 allegro_init();
193 depth = desktop_color_depth();
194 if (depth == 0) depth = 32;
195 set_color_depth(depth);
196 res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
197 if (res != 0) {
198 allegro_message(allegro_error);
199 exit(-1);
200 }
201 
202 install_timer();
203 install_keyboard();
204 install_mouse();
205
206 ballX= 314;
207 ballY= 438;
208 for(int i=0; i < 20; i++)
209 for(int j=0; j < 13; j++)
210 brickMap<i>[j] = rand()%2;
211 /* add other initializations here */
212}
213 
214void deinit() {
215 clear_keybuf();
216 /* add other deinitializations here */
217}

in particular it is this bit of code that does 'it' wwhateverit is

ballCollisionX = ballX / 32;
   ballCollisionY = ballY /32;
   if (brickMap[ballCollisionX][ballCollisionY] == 1)
      {dy = -dy;}

I take my ball's x and y and divide them by 32, in theory this gives me a whole number 1,2 etc since my tile size is 32x32. I then plug the divdividedy into the tilemap to see if it equals a collidable tile, if it does i change the direction of the ball. Obviously this is very rudimentary and imperfect...

I atatched the BMPs i am using so you can compile and check out exactly what it is doing...

thanks again!

EDIT: i guess one solution would to be ta make sure that the ballCOllisionX and Y are divisible by 1 to make sure they are whoel numbers? i can't use !/ i get a compile error /! works untill i run the program then it crashes!

EDIT 2: wait an int has to be a whol enumber doesn't it, thats why we have floating point variables.... Hmmm so then back to the drawing board on that one!

Go to: