2 // C Implementation: collision
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
14 #include "collision.h"
18 bool rectcollision(base_type* one, base_type* two)
20 return (one->x >= two->x - one->width + 1 &&
21 one->x <= two->x + two->width - 1 &&
22 one->y >= two->y - one->height + 1 &&
23 one->y <= two->y + two->height - 1);
26 bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
28 return (one->x >= two->x - one->width +off_x + 1 &&
29 one->x <= two->x + two->width + off_x - 1 &&
30 one->y >= two->y - one->height + off_y + 1 &&
31 one->y <= two->y + two->height + off_y - 1);
34 bool collision_object_map(base_type* pbase)
38 v = (int)pbase->height / 16;
39 h = (int)pbase->width / 16;
41 if(issolid(pbase->x + 1, pbase->y + 1) ||
42 issolid(pbase->x + pbase->width -1, pbase->y + 1) ||
43 issolid(pbase->x +1, pbase->y + pbase->height -1) ||
44 issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1))
47 for(i = 1; i < h; ++i)
49 if(issolid(pbase->x + i*16,pbase->y + 1))
53 for(i = 1; i < h; ++i)
55 if( issolid(pbase->x + i*16,pbase->y + pbase->height - 1))
59 for(i = 1; i < v; ++i)
61 if( issolid(pbase->x + 1, pbase->y + i*16))
64 for(i = 1; i < v; ++i)
66 if( issolid(pbase->x + pbase->width - 1, pbase->y + i*16))
74 void collision_swept_object_map(base_type* old, base_type* current)
76 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
79 float lpath; /* Holds the longest path, which is either in X or Y direction. */
80 float xd,yd; /* Hold the smallest steps in X and Y directions. */
81 float temp, xt, yt; /* Temporary variable. */
87 if(old->x == current->x && old->y == current->y)
91 else if(old->x == current->x && old->y != current->y)
93 lpath = current->y - old->y;
107 else if(old->x != current->x && old->y == current->y)
109 lpath = current->x - old->x;
124 lpath = current->x - old->x;
127 if(current->y - old->y > lpath || old->y - current->y > lpath)
128 lpath = current->y - old->y;
132 xd = (current->x - old->x) / lpath;
133 yd = (current->y - old->y) / lpath;
136 steps = (int)(lpath / (float)16);
141 for(i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
150 if(collision_object_map(old))
155 current->y = old->y - yd;
156 while(collision_object_map(current))
160 current->x = old->x - xd;
161 while(collision_object_map(current))
167 current->x = old->x - xd;
168 current->y = old->y - yd;
169 while(collision_object_map(current))
177 if(!collision_object_map(current))
183 if(!collision_object_map(current))
190 while(!collision_object_map(current))
207 void collision_handler()
211 /* CO_BULLET & CO_BADGUY check */
212 for(i = 0; i < bullets.size(); ++i)
214 for(j = 0; j < bad_guys.size(); ++j)
216 if(bad_guys[j].dying == DYING_NOT)
218 if(rectcollision(&bullets[i].base,&bad_guys[j].base))
220 /* We have detected a collision and now call the collision functions of the collided objects. */
221 bullet_collision(&bullets[i], CO_BADGUY);
222 bad_guys[j].collision(&bullets[i], CO_BULLET);
228 /* CO_BADGUY & CO_BADGUY check */
229 for(i = 0; i < bad_guys.size(); ++i)
231 if(bad_guys[i].dying == DYING_NOT)
233 for(j = i+1; j < bad_guys.size(); ++j)
235 if(j != i && !bad_guys[j].dying)
237 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
239 /* We have detected a collision and now call the collision functions of the collided objects. */
240 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
241 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
250 /* CO_BADGUY & CO_PLAYER check */
251 for(i = 0; i < bad_guys.size(); ++i)
253 if(bad_guys[i].dying == DYING_NOT && rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
255 /* We have detected a collision and now call the collision functions of the collided objects. */
256 if (tux.previous_base.y < tux.base.y &&
257 tux.previous_base.y + tux.previous_base.height < bad_guys[i].base.y + bad_guys[i].base.height/2 &&
258 bad_guys[i].kind != BAD_MONEY && bad_guys[i].mode != HELD)
260 bad_guys[i].collision(&tux, CO_PLAYER);
264 tux.collision(&bad_guys[i], CO_BADGUY);
269 /* CO_UPGRADE & CO_PLAYER check */
270 for(i = 0; i < upgrades.size(); ++i)
272 if(rectcollision(&upgrades[i].base,&tux.base))
274 /* We have detected a collision and now call the collision functions of the collided objects. */
275 upgrade_collision(&upgrades[i], &tux, CO_PLAYER);