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"
20 bool rectcollision(base_type* one, base_type* two)
22 return (one->x >= two->x - one->width + 1 &&
23 one->x <= two->x + two->width - 1 &&
24 one->y >= two->y - one->height + 1 &&
25 one->y <= two->y + two->height - 1);
28 bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
30 return (one->x >= two->x - one->width +off_x + 1 &&
31 one->x <= two->x + two->width + off_x - 1 &&
32 one->y >= two->y - one->height + off_y + 1 &&
33 one->y <= two->y + two->height + off_y - 1);
36 bool collision_object_map(base_type* pbase)
38 int v = (int)pbase->height / 16;
39 int 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(int i = 1; i < h; ++i)
49 if(issolid(pbase->x + i*16,pbase->y + 1))
53 for(int i = 1; i < h; ++i)
55 if( issolid(pbase->x + i*16,pbase->y + pbase->height - 1))
59 for(int i = 1; i < v; ++i)
61 if( issolid(pbase->x + 1, pbase->y + i*16))
64 for(int 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. */
78 float lpath; /* Holds the longest path, which is either in X or Y direction. */
79 float xd,yd; /* Hold the smallest steps in X and Y directions. */
80 float temp, xt, yt; /* Temporary variable. */
86 if(old->x == current->x && old->y == current->y)
90 else if(old->x == current->x && old->y != current->y)
92 lpath = current->y - old->y;
106 else if(old->x != current->x && old->y == current->y)
108 lpath = current->x - old->x;
123 lpath = current->x - old->x;
126 if(current->y - old->y > lpath || old->y - current->y > lpath)
127 lpath = current->y - old->y;
131 xd = (current->x - old->x) / lpath;
132 yd = (current->y - old->y) / lpath;
135 steps = (int)(lpath / (float)16);
140 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
149 if(collision_object_map(old))
154 current->y = old->y - yd;
155 while(collision_object_map(current))
159 current->x = old->x - xd;
160 while(collision_object_map(current))
166 current->x = old->x - xd;
167 current->y = old->y - yd;
168 while(collision_object_map(current))
176 if(!collision_object_map(current))
182 if(!collision_object_map(current))
189 while(!collision_object_map(current))
206 void collision_handler()
208 // CO_BULLET & CO_BADGUY check
209 for(unsigned int i = 0; i < world.bullets.size(); ++i)
211 for(unsigned int j = 0; j < world.bad_guys.size(); ++j)
213 if(world.bad_guys[j].dying != DYING_NOT)
215 if(rectcollision(&world.bullets[i].base, &world.bad_guys[j].base))
217 // We have detected a collision and now call the
218 // collision functions of the collided objects.
219 // collide with bad_guy first, since bullet_collision will
221 world.bad_guys[j].collision(0, CO_BULLET);
222 bullet_collision(&world.bullets[i], CO_BADGUY);
223 break; // bullet is invalid now, so break
228 /* CO_BADGUY & CO_BADGUY check */
229 for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
231 if(world.bad_guys[i].dying != DYING_NOT)
234 for(unsigned int j = i+1; j < world.bad_guys.size(); ++j)
236 if(j == i || world.bad_guys[j].dying != DYING_NOT)
239 if(rectcollision(&world.bad_guys[i].base, &world.bad_guys[j].base))
241 // We have detected a collision and now call the
242 // collision functions of the collided objects.
243 world.bad_guys[j].collision(&world.bad_guys[i], CO_BADGUY);
244 world.bad_guys[i].collision(&world.bad_guys[j], CO_BADGUY);
249 if(tux.dying != DYING_NOT) return;
251 // CO_BADGUY & CO_PLAYER check
252 for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
254 if(world.bad_guys[i].dying != DYING_NOT)
257 if(rectcollision_offset(&world.bad_guys[i].base,&tux.base,0,0))
259 // We have detected a collision and now call the collision
260 // functions of the collided objects.
261 if (tux.previous_base.y < tux.base.y &&
262 tux.previous_base.y + tux.previous_base.height
263 < world.bad_guys[i].base.y + world.bad_guys[i].base.height/2)
265 world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
269 tux.collision(&world.bad_guys[i], CO_BADGUY);
274 // CO_UPGRADE & CO_PLAYER check
275 for(unsigned int i = 0; i < world.upgrades.size(); ++i)
277 if(rectcollision(&world.upgrades[i].base, &tux.base))
279 // We have detected a collision and now call the collision
280 // functions of the collided objects.
281 upgrade_collision(&world.upgrades[i], &tux, CO_PLAYER);
287 Tile* gettile(float x, float y)
289 return TileManager::instance()->get(World::current()->get_level()->gettileid(x, y));
292 bool issolid(float x, float y)
294 Tile* tile = gettile(x,y);
295 return tile && tile->solid;
298 bool isbrick(float x, float y)
300 Tile* tile = gettile(x,y);
301 return tile && tile->brick;
304 bool isice(float x, float y)
306 Tile* tile = gettile(x,y);
307 return tile && tile->ice;
310 bool isfullbox(float x, float y)
312 Tile* tile = gettile(x,y);
313 return tile && tile->fullbox;
316 bool isdistro(float x, float y)
318 Tile* tile = gettile(x,y);
319 return tile && tile->distro;