Collision Detection
zavirax

Hi, Can anyone please read through the source code of my program here?:

1#include <allegro.h>
2#include <stdio.h>
3 
4int x = 100; /* sets the position of the + sign(x-axis) */
5int y = 260; /*(y-axis)*/
6 
7int a = 320; /* for the puck*/
8int b = 100;
9 
10int c = 520; /* playertwo*/
11int d = 260;
12 
13void playerone(int x, int y, BITMAP *buffer){
14
15 textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
16 /* makes a '+' sign and sets the colour as white and to start where x and y are set*/
17 textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
18 /* create player one's cursor and make him red*/
19}
20void playertwo(int c, int d, BITMAP *buffer){
21 
22 textout_ex(buffer, font, "+",c ,d , makecol(0, 0, 255), makecol(0, 0, 0));
23 /* create player two's cursor and make him blue*/
24 textout_ex(buffer, font, "+", c, d, makecol(0, 0, 255), makecol(0, 0, 0));
25}
26int main()
27 
28{
29
30 allegro_init();
31 install_keyboard();
32 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
33
34 while ( !key[KEY_ESC] ){
35
36 clear_keybuf();
37
38 //acquire_screen();
39
40 BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/
41 buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/
42
43 playerone(x,y,buffer);
44 if (key[KEY_W]) --y; /*up when you press up*/
45 if (key[KEY_S]) ++y; /*down when you press down*/
46 if (key[KEY_D]) ++x; /*right when you press right*/
47 if (key[KEY_A]) --x; /*left when you press Left*/
48 playertwo(c,d,buffer);
49 if (key[KEY_UP]) --d; /*up when you press up*/
50 if (key[KEY_DOWN]) ++d; /*down when you press down*/
51 if (key[KEY_RIGHT]) ++c; /*right when you press right*/
52 if (key[KEY_LEFT]) --c; /*left when you press left*/
53
54 
55
56 /* makes a '+' sign and sets the colour as white and to go where x and y are set*/
57 textout_centre_ex(buffer, font,"Air Hockey" , a, b, makecol(255, 0, 255), -1);
58 //release_screen();
59
60
61 circlefill( buffer, 140, 260, 5, makecol(255, 255, 255));/* puck*/
62 rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/
63 rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/
64 rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/
65 line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/
66 circle(buffer, 320, 260, 50, makecol(255, 255, 255));/*center circle*/
67 /* makes the playing surface*/
68 /* notice that all this is being drawn to the buffer to stop flickering*/
69
70 blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/
71
72 rest(12);
73
74 }
75
76 return 0;
77
78}
79END_OF_MAIN();

Sorry that It's a mess I'm very new to it. I'm trying to crate air hockey, but I'm stuck at getting things to collide. Can i have suggestions please. NOTICE: My Playerone and Playertwo are not images they are text essentially. '+'

Thanks guys.;D

Dustin Dettmer

Generally, when pasting source code its nice to encapsulate them in [code] tags. Like so:

<code>
void foobar(int);

int var = 5;

foobar(5 * 2);
</code>

zavirax

I said I was a noob. Thanks but how does this help my code????

Dustin Dettmer

Because then people will actually read your code and possibly even try to help.

Onewing
Quote:

Because then people will actually read your code and possibly even try to help.

He's right, I really don't want to read it myself. Fix it up and I might take a look. Yeah, I know, it's not really nice, but I just can't stand to look at that conglomeration above...

HardTranceFan

There's no point at the moment of doing any collision detection - you're writing the line "Air Hockey" where the puck should be, and drawing the puck at a fixed position.

Once you get round to the collision detection, have a search for bounding boxes.

BAF

You should draw the puck at the end, after the rest of the playing field has been drawn. Make sure you are drawing stuff in the right order (z-ordering).

zavirax

ok ill try to fix it up, Here's it now i know it's not perfect:

1#include <allegro.h>
2#include <stdio.h>
3 
4int x = 100; /* sets the position of the + sign(x-axis) */
5int y = 260; /*(y-axis)*/
6 
7int a = 140; /* for the puck*/
8int b = 250;
9int c = 150;
10int d = 260;
11 
12int e = 520; /* playertwo*/
13int f = 290;
14 
15int outside_bb_left = 60;/* playing field boundong box*/
16int outside_bb_up = 120;
17int outside_bb_down = 395;
18int outside_bb_right = 575;
19int middle_line = 316;
20 
21int puck_left=a;/* puck bounding box*/
22int puck_up=b;
23int puck_right=c;
24int puck_down=d;
25 
26 
27 
28 
29void puck(int a, int b, int c, int d, BITMAP *buffer){
30 
31rectfill( buffer, a , b,c ,d , makecol(0, 255, 0));/* puck*/
32 
33}
34 
35 
36 
37void playerone(int x, int y, BITMAP *buffer){
38 
39textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
40/* makes a '+' sign and sets the colour as white and to start where x and y are set*/
41textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
42/* create player one's cursor and make him red*/
43}
44void playertwo(int e, int f, BITMAP *buffer){
45 
46textout_ex(buffer, font, "+",e ,f , makecol(0, 0, 255), makecol(0, 0, 0));
47/* create player two's cursor and make him blue*/
48textout_ex(buffer, font, "+", e, f, makecol(0, 0, 255), makecol(0, 0, 0));
49}
50 
51 
52int main()
53 
54{
55 
56allegro_init();
57install_keyboard();
58set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
59 
60/* makes a '+' sign and sets the colour as white and to go where x and y are set*/
61 
62 
63while ( !key[KEY_ESC] ){
64 
65clear_keybuf();
66 
67 
68BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/
69buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/
70 
71textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1);
72//release_screen();
73 
74rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/
75rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/
76rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/
77line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/
78circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/
79/* makes the playing surface*/
80/* notice that all this is being drawn to the buffer to stop flickering*/
81 
82 
83 
84playerone(x,y,buffer);
85if (key[KEY_W]) --y; /*up when you press up*/
86if (key[KEY_S]) ++y; /*down when you press down*/
87if (key[KEY_D]) ++x; /*right wehn you press right*/
88if (key[KEY_A]) --x; /*left when you press ledft*/
89 
90/* stops playerone leaving the playing field*/
91 
92 
93if (x==outside_bb_left){
94x++;
95}
96 
97if (y==outside_bb_up){
98y++;
99}
100if (y==outside_bb_down){
101y--;
102}
103if (x==middle_line){
104x--;
105}
106 
107 
108 
109 
110 
111 
112playertwo(e,f,buffer);
113if (key[KEY_UP]) --f; /*up when you press up*/
114if (key[KEY_DOWN]) ++f; /*down when you press down*/
115if (key[KEY_RIGHT]) ++e; /*right wehn you press right*/
116if (key[KEY_LEFT]) --e; /*left when you press ledft*/
117 
118/* stops playertwo leaving the playing field*/
119 
120if(e==middle_line){
121e++;
122}
123 
124if(f==outside_bb_up){
125f++;
126}
127 
128if(f==outside_bb_down){
129f--;
130}
131 
132if(e==outside_bb_right){
133e--;
134}
135 
136 
137 
138 
139 
140puck(a,b,c,d,buffer);
141/* attempts tp make the puck move when hit*/
142 
143 
144/* this makes sure the puck doesn't leave the playing field*/
145if (a==outside_bb_right){
146a--;
147}
148 
149if (b==outside_bb_left){
150a++;
151}
152 
153if (c==outside_bb_up){
154b++;
155}
156 
157if (d==outside_bb_down){
158b--;
159}
160 
161 
162if (x==a&&x<c&&y<=b&&y>=d){
163a++&&c++;
164}
165if (x<=c&&x>a&&y<=d&&y>b){
166a--&&c--;
167}
168 
169 
170 
171blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/
172 
173rest(12);
174 
175}
176 
177return 0;
178 
179}
180END_OF_MAIN();

i changed the puck to a square just for easiness. My problem now is getting it to move right( Haven't done the up and down...it should move right and left(it only goes left)) thanks guys

Albin Engström

Edit your post before someone sees it :). they've told you already: USE CODE TAGS.

Here's how you do it(again):

crap, i can't make an example but many has already..

it will look like this:

1 
2#include <allegro.h>
3#include <stdio.h>
4 
5int x = 100; /* sets the position of the + sign(x-axis) */
6int y = 260; /*(y-axis)*/
7 
8int a = 140; /* for the puck*/
9int b = 250;
10int c = 150;
11int d = 260;
12 
13int e = 520; /* playertwo*/
14int f = 290;
15 
16int outside_bb_left = 60;/* playing field boundong box*/
17int outside_bb_up = 120;
18int outside_bb_down = 395;
19int outside_bb_right = 575;
20int middle_line = 316;
21 
22int puck_left=a;/* puck bounding box*/
23int puck_up=b;
24int puck_right=c;
25int puck_down=d;
26 
27 
28 
29 
30void puck(int a, int b, int c, int d, BITMAP *buffer){
31 
32rectfill( buffer, a , b,c ,d , makecol(0, 255, 0));/* puck*/
33 
34}
35 
36 
37 
38void playerone(int x, int y, BITMAP *buffer){
39 
40textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
41/* makes a '+' sign and sets the colour as white and to start where x and y are set*/
42textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
43/* create player one's cursor and make him red*/
44}
45void playertwo(int e, int f, BITMAP *buffer){
46 
47textout_ex(buffer, font, "+",e ,f , makecol(0, 0, 255), makecol(0, 0, 0));
48/* create player two's cursor and make him blue*/
49textout_ex(buffer, font, "+", e, f, makecol(0, 0, 255), makecol(0, 0, 0));
50}
51 
52 
53int main()
54 
55{
56 
57allegro_init();
58install_keyboard();
59set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
60 
61/* makes a '+' sign and sets the colour as white and to go where x and y are set*/
62 
63 
64while ( !key[KEY_ESC] ){
65 
66clear_keybuf();
67 
68 
69BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/
70buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/
71 
72textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1);
73//release_screen();
74 
75rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/
76rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/
77rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/
78line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/
79circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/
80/* makes the playing surface*/
81/* notice that all this is being drawn to the buffer to stop flickering*/
82 
83 
84 
85playerone(x,y,buffer);
86if (key[KEY_W]) --y; /*up when you press up*/
87if (key[KEY_S]) ++y; /*down when you press down*/
88if (key[KEY_D]) ++x; /*right wehn you press right*/
89if (key[KEY_A]) --x; /*left when you press ledft*/
90 
91/* stops playerone leaving the playing field*/
92 
93 
94if (x==outside_bb_left){
95x++;
96}
97 
98if (y==outside_bb_up){
99y++;
100}
101if (y==outside_bb_down){
102y--;
103}
104if (x==middle_line){
105x--;
106}
107 
108 
109 
110 
111 
112 
113playertwo(e,f,buffer);
114if (key[KEY_UP]) --f; /*up when you press up*/
115if (key[KEY_DOWN]) ++f; /*down when you press down*/
116if (key[KEY_RIGHT]) ++e; /*right wehn you press right*/
117if (key[KEY_LEFT]) --e; /*left when you press ledft*/
118 
119/* stops playertwo leaving the playing field*/
120 
121if(e==middle_line){
122e++;
123}
124 
125if(f==outside_bb_up){
126f++;
127}
128 
129if(f==outside_bb_down){
130f--;
131}
132 
133if(e==outside_bb_right){
134e--;
135}
136 
137 
138 
139 
140 
141puck(a,b,c,d,buffer);
142/* attempts tp make the puck move when hit*/
143 
144 
145/* this makes sure the puck doesn't leave the playing field*/
146if (a==outside_bb_right){
147a--;
148}
149 
150if (b==outside_bb_left){
151a++;
152}
153 
154if (c==outside_bb_up){
155b++;
156}
157 
158if (d==outside_bb_down){
159b--;
160}
161 
162 
163if (x==a&&x<c&&y<=b&&y>=d){
164a++&&c++;
165}
166if (x<=c&&x>a&&y<=d&&y>b){
167a--&&c--;
168}
169 
170 
171 
172blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/
173 
174rest(12);
175 
176}
177 
178return 0;
179 
180}
181END_OF_MAIN();

Be happy

spellcaster

Use code tags, indent your code. Use braces for each and every code block, even if it's a single line.
Create a struct to hold your player data.
Create an array of this struct so you can easily add more players (and even more important: you don't have to duplicate your code).
Get rid of the ";" after END_OF_MAIN

Oh, and use speaking variable names.

HardTranceFan

I'll say it too - there are posting protocols. Ignoring this advice is breaking one of them. Please update your post and use the [code]...[/code] tags.

I'll help this time, but no more if you don't use tags.

1. Use meaningful variable names. It takes ages to work out and remember what a, b, c... x, y are all for. Use names like xPuck, yPuck, xPlayer1, yPlayer1, xPlayer2 etc. It makes the whole thing easier to understand. [edit]Or, as spellcaster pointed out, use structs with meaningful names[/edit]

2. The line (x==a&&x<c&&y<=b&&y>=d) is wrong, and is why the puck won't go right. There is never a situation when the player position will be above and below the puck at the same time, and x==a is not the test condition you want to perform.

zavirax

How do i do the code tags again?
Is it ... at the start and end respectively?
Also, I'll change the variables.
ALSO, I dont know how to do 'structs', sorry I'm such a noob.
thanks for putting up with me

umm Let's try this: (if you can't read it tell me what to improve again...thanks for the help again)

1 
2#include <allegro.h>
3#include <stdio.h>
4 
5int p1_x = 100; /* sets the position of the + sign(x-axis) */
6int p1_y = 260; /*(y-axis)*/
7 
8 
9int p2_x = 520; /* playertwo*/
10int p2_y = 290;
11 
12int outside_bb_left = 60;/* playing field boundong box*/
13int outside_bb_up = 120;
14int outside_bb_down = 395;
15int outside_bb_right = 575;
16int middle_line = 316;
17 
18int puck_left=140;/* puck bounding box*/
19int puck_up=250;
20int puck_right=150;
21int puck_down=260;
22 
23 
24void puck(int puck_left, int puck_up, int puck_right, int puck_down, BITMAP *buffer){
25
26 rectfill( buffer, puck_left , puck_up, puck_right, puck_down, makecol(0, 255, 0));/* puck*/
27
28}
29 
30 
31 
32void playerone(int p1_x, int p1_y, BITMAP *buffer){
33
34 textout_ex( buffer, font, "+", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
35 /* makes a '+' sign and sets the colour as white and to start where x and y are set*/
36 textout_ex( buffer, font, "+", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
37 /* create player one's cursor and make him red*/
38}
39void playertwo(int p2_x, int p2_y, BITMAP *buffer){
40
41 textout_ex(buffer, font, "+", p2_x,p2_y , makecol(0, 0, 255), makecol(0, 0, 0));
42 /* create player two's cursor and make him blue*/
43 textout_ex(buffer, font, "+", p2_x, p2_y, makecol(0, 0, 255), makecol(0, 0, 0));
44}
45 
46 
47int main()
48 
49{
50
51 allegro_init();
52 install_keyboard();
53 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
54
55 /* makes a '+' sign and sets the colour as white and to go where x and y are set*/
56
57
58 while ( !key[KEY_ESC] ){
59
60 clear_keybuf();
61
62
63 BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/
64 buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/
65
66 textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1);
67
68 rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/
69 rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/
70 rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/
71 line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/
72 circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/
73 /* makes the playing surface*/
74 /* notice that all this is being drawn to the buffer to stop flickering*/
75
76
77 playerone(p1_x,p1_y,buffer);
78 if (key[KEY_W]) --p1_y; /*up when you press up*/
79 if (key[KEY_S]) ++p1_y; /*down when you press down*/
80 if (key[KEY_D]) ++p1_x; /*right wehn you press right*/
81 if (key[KEY_A]) --p1_x; /*left when you press ledft*/
82
83 /* stops playerone leaving the playing field*/
84
85
86 if (p1_x==outside_bb_left){
87 p1_x++;
88 }
89 if (p1_y==outside_bb_up){
90 p1_y++;
91 }
92 if (p1_y==outside_bb_down){
93 p1_y--;
94 }
95 if (p1_x==middle_line){
96 p1_x--;
97 }
98
99
100
101
102
103
104 playertwo(p2_x,p2_y,buffer);
105 if (key[KEY_UP]) p2_y--; /*up when you press up*/
106 if (key[KEY_DOWN]) p2_y++; /*down when you press down*/
107 if (key[KEY_RIGHT]) p2_x++; /*right wehn you press right*/
108 if (key[KEY_LEFT]) p2_x--; /*left when you press ledft*/
109
110 /* stops playertwo leaving the playing field*/
111
112 if(p2_x==middle_line){
113 p2_x++;
114 }
115
116 if(p2_y==outside_bb_up){
117 p2_y++;
118 }
119
120 if(p2_y==outside_bb_down){
121 p2_y--;
122 }
123
124 if(p2_x==outside_bb_right){
125 p2_x--;
126 }
127
128
129
130
131
132 puck(puck_left,puck_up,puck_right,puck_down,buffer);
133 /* attempts to make the puck move when hit*/
134
135
136 /* this makes sure the puck doesn't leave the playing field*/
137 if (puck_right==outside_bb_right){
138 puck_left--&&puck_right--;
139 }
140
141 if (puck_left==outside_bb_left){
142 puck_left++&&puck_right++;
143 }
144
145 if (puck_up==outside_bb_up){
146 puck_up++&&puck_down++;
147 }
148
149 if (puck_down==outside_bb_down){
150 puck_up--&&puck_down--;
151 }
152
153
154 if (p1_x==puck_right&&p1_y<=puck_up&&p1_y>=puck_down){
155 puck_left++&&puck_right++;
156 }
157 if (p1_x==puck_left&&p1_y<=puck_up&&p1_y>=puck_down){
158 puck_left--&&puck_right--;
159 }
160
161
162
163 blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/
164
165 rest(15);
166
167 }
168
169 return 0;
170
171}
172END_OF_MAIN()

Albin Engström

A much nicer view ;).

btw, what's the problem?

ran your game but i don't know what should work and not..

zavirax

Well what It Is, Is that I cant get the puck to move the way i want so i don't know what i should do..can't find any help anywhere except here. By the way im on a deadline, this is a school course...I'm dumb!.:)

Paul whoknows
Quote:

...I'm dumb!:)

A happy one.::)

HardTranceFan
Quote:

Well what It Is, Is that I cant get the puck to move the way i want so i don't know what i should do..can't find any help anywhere except here. By the way im on a deadline, this is a school course...I'm dumb!:)

Here is an allegro pong tutorial, which you can adapt to 4 way movement and collision detection.

zavirax

Sorry but, Im not sure what i should do with it...is there nothing you would suggest for my code?

Three Harris

I suggest you don't create the buffer in the main loop and not destroy it. That is a massive memory leak.

Paul whoknows
Quote:

but I'm stuck at getting things to collide


Just use this:

inline bool collide(int x1, int y1, int x2, int y2, int xx1, int yy1, int xx2, int yy2)
{
     if (x2 <= xx1) return false;
     else if (x1 >= xx2) return false;
     else if (y2 <= yy1) return false;
     else if (y1 >= yy2) return false;
     else return true;
}

HardTranceFan
Quote:

Sorry but, I'm not sure what i should do with it...

Sorry, my bad - the page I linked to and my suggestions didn't match (there is a link to the pong tutorial down that page if you looked). Here is the correct link to the pong tutorial.

zavirax

Yeah I already found It on there, don't worry about It.

btw how would i use that boolean expression thing, could you show in my code please. thanks

Here's my code so far i can push around...any ideas how to make it slide so i only have to hit it not push ... also how would i get it rebound of the sides ... also how can i make a hole in the outside of the field so that it can go in the goal... and also ... however i think i know how, how can make a scoreboard?

lots of questions hehe sorry to be such a stupid person.

Thread #591420. Printed from Allegro.cc