Advertisement
Vita94

Collision detection

Jun 2nd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class cObject
  4. {
  5. private:
  6.     int x, y;
  7.     int width, height;
  8. public:
  9.     cObject() { x = y = 0; width = height = 1; }
  10.     cObject(int x, int y, int width, int height)
  11.     {
  12.         this->x = x;
  13.         this->y = y;
  14.         this->width = width;
  15.         this->height = height;
  16.     }
  17.     int getLeft() { return x; }
  18.     int getRight() { return x + width; }
  19.     int getTop() { return y; }
  20.     int getBottom() { return y + height; }
  21.     bool CollidesWith(cObject & obj)
  22.     {
  23.         if (this->getBottom() <= obj.getTop())
  24.             return false;
  25.         if (this->getTop() >= obj.getBottom())
  26.             return false;
  27.         if (this->getRight() <= obj.getLeft())
  28.             return false;
  29.         if (this->getLeft() >= obj.getRight())
  30.             return false;
  31.         return true;
  32.     }
  33. };
  34. int main()
  35. {
  36.     cObject o1(0, 0, 10, 10);
  37.     cObject o2(10, 8, 1, 1);
  38.     if (o1.CollidesWith(o2))
  39.         cout << "Yes" << endl;
  40.     getchar();
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement