Direction Collision(SDL_Rect *a, SDL_Rect *b) { int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; leftA = a->x; rightA = a->x + a->w; topA = a->y; bottomA = a->y + a->h; leftB = b->x; rightB = b->x + b->w; topB = b->y; bottomB = b->y + b->h; if (bottomA <= topB || topA >= bottomB || rightA <= leftB || leftA >= rightB) return NONE; else return CollisionSide(a, b); }
void ZOMBIE::SetCollision(SQUARE &col) { collision.x = col.width; collision.y = col.height; collision.width = col.x; collision.height = col.y; CollisionSide(); }
bool ZOMBIE::CollisionCheck(OBJECT &object) { bool temp = false; //figure out which type of collision to use switch(type) { case 0: switch(object.GetType()) { case 0: if (BoxCollisionCheck(object)) { collisionDetected = true; temp = true; } break; case 5: if (CirleVSSquareCollisionCheck(object.GetPos(), pos, size, object.GetRadius())) { //check how far into edge 1 the collision is collision.x = pos.x + size.x - object.GetPos().x; //check how far into edge 2 the collision is collision.width = object.GetPos().x + object.GetRadius() - pos.x; //check how far into edge 3 the collision is collision.y = pos.y + size.y - object.GetPos().y; //check how far into edge 4 the collision is collision.height = object.GetPos().y + object.GetRadius() - pos.y; collisionDetected = true; temp = true; } break; } break; case 5: switch(object.GetType()) { case 0: if (CirleVSSquareCollisionCheck(pos, object.GetPos(), object.GetSize(), radius)) { //check how far into edge 1 the collision is collision.x = pos.x + radius - object.GetPos().x; //check how far into edge 2 the collision is collision.width = object.GetPos().x + object.GetSize().x - pos.x - radius; //check how far into edge 3 the collision is collision.y = pos.y + radius - object.GetPos().y; //check how far into edge 4 the collision is collision.height = object.GetPos().y + object.GetSize().y - pos.y - radius; collisionDetected = true; temp = true; } break; case 5: if (CirleCollisionCheck(object)) { collisionDetected = true; temp = true; } break; } break; } if (temp) { if (!collisionside.top || !collisionside.bot || !collisionside.left || !collisionside.right) { CollisionSide(); } } return temp; }