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 int rectcollision(itop_type* one, itop_type* two)
21 if (*one->x >= *two->x - *one->width &&
22 *one->x <= *two->x + *two->width &&
23 *one->y >= *two->y - *one->height &&
24 *one->y <= *two->y + *two->height )
34 int rectcollision_offset(itop_type* one, itop_type* two, float off_x, float off_y)
37 if (*one->x >= *two->x - *one->width +off_x &&
38 *one->x <= *two->x + *two->width + off_x &&
39 *one->y >= *two->y - *one->height + off_y &&
40 *one->y <= *two->y + *two->height + off_y )
50 void collision_rect_detect(int co_one, int co_two)
54 /* CO_BULLET & CO_BADGUY check */
55 for(i = 0; i < NUM_BULLETS; ++i)
59 for(j = 0; j < NUM_BAD_GUYS; ++j)
63 if(rectcollision(&bullets[i].it,&bad_guys[j].it) == YES)
65 /* We have detected a collision and now call the collision functions of the collided objects. */
66 bullet_collision(&bullets[i], CO_BADGUY);
67 badguy_collision(&bad_guys[j], &bullets[i], CO_BULLET);
75 void collision_handler()
79 /* CO_BULLET & CO_BADGUY check */
80 for(i = 0; i < NUM_BULLETS; ++i)
84 for(j = 0; j < NUM_BAD_GUYS; ++j)
88 if(rectcollision(&bullets[i].it,&bad_guys[j].it) == YES)
90 /* We have detected a collision and now call the collision functions of the collided objects. */
91 bullet_collision(&bullets[i], CO_BADGUY);
92 badguy_collision(&bad_guys[j], &bullets[i], CO_BULLET);
99 /* CO_BADGUY & CO_BADGUY check */
100 for(i = 0; i < NUM_BAD_GUYS; ++i)
102 if(bad_guys[i].alive)
104 for(j = i+1; j < NUM_BAD_GUYS; ++j)
106 if(j != i && bad_guys[j].alive)
108 if(rectcollision(&bad_guys[i].it,&bad_guys[j].it) == YES)
110 /* We have detected a collision and now call the collision functions of the collided objects. */
111 badguy_collision(&bad_guys[j], &bad_guys[i], CO_BADGUY);
118 /* CO_BADGUY & CO_PLAYER check */
119 for(i = 0; i < NUM_BAD_GUYS; ++i)
121 if(bad_guys[i].alive)
123 if(rectcollision_offset(&bad_guys[i].it,&tux.it,0,48) == YES && tux.ym < 0)
125 /* We have detected a collision and now call the collision functions of the collided objects. */
126 badguy_collision(&bad_guys[i], &tux, CO_PLAYER);
128 if(rectcollision(&bad_guys[i].it,&tux.it) == YES)
130 player_collision(&tux, &bad_guys[i], CO_BADGUY);
136 /* CO_UPGRADE & CO_PLAYER check */
137 for(i = 0; i < NUM_UPGRADES; ++i)
139 if(upgrades[i].alive)
141 if(rectcollision(&upgrades[i].it,&tux.it) == YES)
143 /* We have detected a collision and now call the collision functions of the collided objects. */
144 upgrade_collision(&upgrades[i], &tux, CO_PLAYER);