- // CO_BULLET & CO_BADGUY check
- for(unsigned int i = 0; i < bullets.size(); ++i)
- {
- for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
- {
- if((*j)->dying != DYING_NOT)
- continue;
-
- if(rectcollision(bullets[i]->base, (*j)->base))
- {
- // We have detected a collision and now call the
- // collision functions of the collided objects.
- (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
- bullets[i]->collision(CO_BADGUY);
- break; // bullet is invalid now, so break
- }
- }
- }
-
- /* CO_BADGUY & CO_BADGUY check */
- for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
- {
- if((*i)->dying != DYING_NOT)
- continue;
-
- BadGuys::iterator j = i;
- ++j;
- for (; j != badguys.end(); ++j)
- {
- if(j == i || (*j)->dying != DYING_NOT)
- continue;
-
- if(rectcollision((*i)->base, (*j)->base))
- {
- // We have detected a collision and now call the
- // collision functions of the collided objects.
- (*j)->collision(*i, CO_BADGUY);
- (*i)->collision(*j, CO_BADGUY);
- }
- }
- }
- if(player->dying != DYING_NOT) return;
-
- // CO_BADGUY & CO_PLAYER check
- for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
- {
- if((*i)->dying != DYING_NOT)
- continue;
-
- if(rectcollision_offset((*i)->base, player->base, 0, 0))
- {
- // We have detected a collision and now call the collision
- // functions of the collided objects.
- if (player->previous_base.y < player->base.y &&
- player->previous_base.y + player->previous_base.height
- < (*i)->base.y + (*i)->base.height/2
- && !player->invincible_timer.started())
- {
- (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
- }
- else
- {
- player->collision(*i, CO_BADGUY);
- (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
- }
- }
+ if(!collision::intersects(r1, r2))
+ return;
+
+ // calculate intersection
+ float itop = r1.get_bottom() - r2.get_top();
+ float ibottom = r2.get_bottom() - r1.get_top();
+ float ileft = r1.get_right() - r2.get_left();
+ float iright = r2.get_right() - r1.get_left();
+
+ if(fabsf(movement.y) > fabsf(movement.x)) {
+ if(ileft < SHIFT_DELTA) {
+ constraints->right = std::min(constraints->right, r2.get_left());
+ return;
+ } else if(iright < SHIFT_DELTA) {
+ constraints->left = std::max(constraints->left, r2.get_right());
+ return;