Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » circle - circle collision

This thread is locked; no one can reply to it. rss feed Print
circle - circle collision
chaseC
Member #8,368
February 2007

Hi, I am making an air hockey game and I need some help with puck to the thing you use to hit the puck collision. The collision isn't the real problem, the problem is that if I move the puck hitting thing to fast it goes through the puck and sends it in the wrong direction. I need some advise???

BAF
Member #2,981
December 2002
avatar

What, one account and thread wasn't enough to get people to program your game for you?

Ceagon Xylas
Member #5,495
February 2005
avatar

Line collision. Begin the line at the puck, end it at the puck's coordinates plus it's velocity. Check to see if that line collides with any of the walls (other lines)

chaseC
Member #8,368
February 2007

What I mean is that if the paddle (I don't know if it is actually called a paddle) moves to fast when it is in the path of the puck, it changes position so many pixels at a time that it moves into the puck and sends it in the wrong direction. Also I am not zavirx, my code doen't look anything like his ->

1#include <allegro.h>
2 
3#define BALL_SPEED_MOD 12
4 
5struct comp{
6 float x;
7 float y;
8
9 float x_speed;
10 float y_speed;
11
12 int h;
13 int w;
14
15 int time;
16};
17 
18struct player{
19 float x;
20 float y;
21
22 float x_speed;
23 float y_speed;
24
25 int h;
26 int w;
27};
28 
29comp p2;
30comp puck;
31 
32player p1;
33 
34BITMAP *buffer;
35BITMAP *Player;
36BITMAP *Comp;
37BITMAP *Puck;
38 
39int GameSpeed;
40 
41void SpeedCounter(){
42 GameSpeed++;
43} END_OF_FUNCTION(SpeedCounter);
44 
45void SetUp();
46void HandlePuck();
47void Bounce();
48void Bounce2();
49void MovePlayer();
50void DrawStuff();
51 
52void SetUp(){
53
54 p1.x = 260;
55 p1.y = 240;
56 p1.w = 28;
57 p1.h = 28;
58
59 p2.x = 380;
60 p2.y = 240;
61 p2.w = 28;
62 p2.h = 28;
63
64 puck.x = 320;
65 puck.y = 140;
66 puck.w = 23;
67 puck.h = 23;
68 puck.y_speed = 0;
69 puck.x_speed = -1;
70}
71 
72void HandlePuck(){
73
74 Bounce();
75 Bounce2();
76
77 puck.x += puck.x_speed;
78 puck.y += puck.y_speed;
79
80 if(puck.y_speed < 0 && puck.y - puck.w <= 60 || puck.y_speed > 0 && puck.y + puck.w >= 420){
81 puck.y_speed = -puck.y_speed;
82 }if(puck.x_speed < 0 && puck.x - puck.w <= 20 && puck.y < 180 || puck.x_speed < 0 && puck.x -
83 puck.w <= 20 && puck.y + puck.w > 280|| puck.x_speed > 0 && puck.x + puck.w >= 619&& puck.y -
84 puck.w < 180 || puck.x_speed > 0 && puck.x + puck.w >= 619 && puck.y - puck.w > 280){
85 puck.x_speed = -puck.x_speed;
86 }
87 puck.x_speed *= 0.996;
88 puck.y_speed *= 0.996;
89}
90 
91void Bounce(){
92
93 static int puck_bounce = 0;
94 const float dx = p1.x - puck.x;
95 const float dy = p1.y - puck.y;
96 const float dist = p1.w + puck.w;
97 
98 if(((dx * dx) + (dy * dy)) < (dist * dist) && puck_bounce == 0){
99 puck.x_speed = -(((p1.x + p1.w) - (puck.x + puck.w))/BALL_SPEED_MOD) + (p1.x_speed/BALL_SPEED_MOD) - (puck.x_speed/BALL_SPEED_MOD);
100 puck.y_speed = -(((p1.y + p1.w) - (puck.y + puck.w))/BALL_SPEED_MOD) + (p1.y_speed/BALL_SPEED_MOD) - (puck.y_speed/BALL_SPEED_MOD);
101 puck_bounce = 1;
102 }
103 if(((dx * dx) + (dy * dy)) > (dist * dist)){
104 puck_bounce = 0;
105 }
106}
107 
108void Bounce2(){
109
110 const float dx = p2.x - puck.x;
111 const float dy = p2.y - puck.y;
112 const float dist = p2.w + puck.w;
113 
114 if(((dx * dx) + (dy * dy)) < (dist * dist)){
115 puck.x_speed = -(((p2.x + p2.w) - (puck.x + puck.w))/BALL_SPEED_MOD) + p2.x_speed;
116 puck.y_speed = -(((p2.y + p2.w) - (puck.y + puck.w))/BALL_SPEED_MOD) + p2.y_speed;
117 }
118}
119
120void MovePlayer(){
121
122 int mickeyx;
123 int mickeyy;
124
125 get_mouse_mickeys(&mickeyx, &mickeyy);
126
127 if(!(mouse_b & 1) && !(mouse_b & 2)){
128 p1.x_speed = mickeyx/8;
129 p1.y_speed = mickeyy/8;
130 }else if(mouse_b & 1){
131 p1.y_speed = 0;
132 if(mickeyx <= 1 && mickeyx >= -1){
133 p1.x_speed *= 0.99;
134 }else{
135 p1.x_speed = mickeyx/8;
136 }
137 }else if(mouse_b & 2){
138 p1.x_speed = 0;
139 if(mickeyy <= 1 && mickeyy >= -1){
140 p1.y_speed *= 0.99;
141 }else{
142 p1.y_speed = mickeyy/8;
143 }
144 }
145 p1.x += p1.x_speed;
146 p1.y += p1.y_speed;
147}
148 
149void DrawStuff(){
150
151 clear_to_color(buffer, makecol(255, 255, 255));
152
153 rect( buffer, 20, 60, 619, 420, makecol(255, 0, 0));
154 rect( buffer, 0, 180, 20, 280, makecol( 0, 0, 0));
155 rect( buffer, 619, 180, 639, 280, makecol(0, 0, 0));
156 line( buffer, 320, 60, 320, 420, makecol( 255, 0, 0));
157 circle( buffer, 320, 240, 80, makecol(0, 128, 255));
158 draw_sprite(buffer, Puck, puck.x - puck.w, puck.y - puck.w);
159 draw_sprite(buffer, Comp, p2.x - p2.w, p2.y - p2.h);
160 draw_sprite(buffer, Player, p1.x - p1.w, p1.y - p1.h);
161
162 draw_sprite(screen, buffer, 0, 0);
163}
164 
165int main(int argc, char *argv[]){
166 
167 allegro_init();
168 install_timer();
169 install_keyboard();
170 install_mouse();
171 set_color_depth(32);
172 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
173
174 LOCK_FUNCTION(speed_counter);
175 LOCK_VARIABLE(GameSpeed);
176 install_int_ex(SpeedCounter, BPS_TO_TIMER(90));
177
178 set_mouse_range(20, 60, 320, 420);
179 set_mouse_speed(32, 32);
180
181 buffer = create_bitmap( 640, 480);
182 Player = load_bitmap("hockey_paddle.bmp", NULL);
183 Comp = load_bitmap("hockey_paddle2.bmp", NULL);
184 Puck = load_bitmap("hockey_puck.bmp", NULL);
185 set_window_title("Air Hockey");
186
187 SetUp();
188 while( !key[KEY_ESC]){
189 while(GameSpeed > 1){
190 MovePlayer();
191 HandlePuck();
192 GameSpeed--;
193 }
194 DrawStuff();
195 }
196
197 return 0;
198 
199}
200END_OF_MAIN();

Also, I actually know how to add code tags;D

Ceagon Xylas
Member #5,495
February 2005
avatar

My suggestion will solve this. Just replace the word 'walls' with 'paddles.'

Andrei Ellman
Member #3,434
April 2003

Are the puck and the paddle both circular? The subject of the post is "circle - circle collision", but in the code you posted the puck has a width and a height which means it can be a rectangle. I'll assume you meant a circle-circle collision.

A circle is the set of points that is at an exact distance (the radius) from the circle centre point. This means that any point who'se closest distance to the centre is is less than the radius is colliding with the circle. For two moving circles, you just need to find if the closest distance between the two line segments (each line segment is from the initial position of the puck to the position of the puck with the distance traveled during the iteration (velocity * length_of_iteration)) is less than the combined radii of the two circles. Don't forget to take into account that if the puck rebounds off a wall, the line-segment is broken.

AE.

--
Don't let the illegitimates turn you into carbon.

zavirax
Member #8,634
May 2007

haha, ill be coming here when mines done because mines just square at the moment...finally, someone with a similar problem.

And yeah your code is much more confusing and i guess advance than mine but mine kinda works so i dont mind because i know what im doing just dont know how to do it.

Go to: