Advertisement
Garey

[C++] Basic Collision Detection

Oct 20th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct Circle {
  8.   int x;
  9.   int y;
  10.   int radius;
  11. };
  12.  
  13. struct Rectangle {
  14.   int x;
  15.   int y;
  16.   int width;
  17.   int height;
  18. };
  19.  
  20. bool CirclesCollision(Circle circle1, Circle circle2);
  21. bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1);
  22. void createBoundingBox(Circle circle1);
  23.  
  24. int main() {
  25.  
  26.   Circle circle1;
  27. //  Circle circle2;
  28.  
  29.   Rectangle rectangle1;
  30.  
  31.   cout << "Enter circle1's center X:";
  32.   cin >> circle1.x;
  33.   cout << "Enter circle1's center Y:";
  34.   cin >> circle1.y;
  35.   cout << "Enter circle1's radius:";
  36.   cin >> circle1.radius;
  37. /*
  38.   cout << "Enter circle2's center X:";
  39.   cin >> circle2.x;
  40.   cout << "Enter circle2's center Y:";
  41.   cin >> circle2.y;
  42.   cout << "Enter circle2's radius:";
  43.   cin >> circle2.radius;
  44. */
  45.   cout << "Enter rectangle1's center X:";
  46.   cin >> rectangle1.x;
  47.   cout << "Enter rectangle1's center Y:";
  48.   cin >> rectangle1.y;
  49.   cout << "Enter rectangle1's width:";
  50.   cin >> rectangle1.width;
  51.   cout << "Enter rectangle1's height:";
  52.   cin >> rectangle1.height;
  53.  
  54.   //cout << (CirclesCollision(circle1, circle2) ? "Circles Collision detected!" : "No collision detected!");
  55.   cout << (RectangleCircleCollision(circle1, rectangle1) ? "\nRectangle <-> Circle Collision detected!" : "\nNo collision detected!");
  56.  
  57.   createBoundingBox(circle1);
  58.  
  59.   return 0;
  60. }
  61.  
  62. bool CirclesCollision(Circle circle1, Circle circle2) {
  63.   int x_axis = circle1.x - circle2.x;
  64.   int y_axis = circle1.y - circle2.y;
  65.   int dist = sqrt((x_axis * x_axis) + (y_axis * y_axis));
  66.  
  67.   return dist < circle1.radius + circle2.radius ? true : false;
  68. }
  69. /*
  70. bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1) {
  71.   int dist_x = abs(circle1.x - rectangle1.x);
  72.   int dist_y = abs(circle1.y - rectangle1.y);
  73.  
  74.   if (dist_x > (rectangle1.width / 2 + circle1.radius))
  75.     return false;
  76.  
  77.   if(dist_y > (rectangle1.height / 2 + circle1.radius))
  78.     return false;
  79.  
  80.   if(dist_x <= (rectangle1.width / 2))
  81.     return true;
  82.  
  83.   if(dist_y <= (rectangle1.height / 2))
  84.     return true;
  85.  
  86.   int dest_x = dist_x - rectangle1.width / 2;
  87.   int dest_y = dist_y - rectangle1.height / 2;
  88.  
  89.   return (dest_x * dest_x + dest_y * dest_y <= (circle1.radius * circle1.radius));
  90. }*/
  91.  
  92. bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1) {
  93.   int half_x = (rectangle1.width / 2);
  94.   int half_y = (rectangle1.height / 2);
  95.  
  96.   int center_x = (circle1.x - (rectangle1.x + half_x));
  97.   int center_y = (circle1.y - (rectangle1.y + half_y));
  98.  
  99.   int side_x = (abs(center_x) - half_x);
  100.   int side_y = (abs(center_y) - half_y);
  101.  
  102.   if(side_x > circle1.radius || side_y > circle1.radius) // outside
  103.     return false;
  104.   if(side_x < -circle1.radius && side_y < -circle1.radius) // inside
  105.     return true;
  106.   if(side_x < 0 || side_y < 0) // sidecorner
  107.     return true;
  108.  
  109.   return side_x * side_x + side_y * side_y < circle1.radius * circle1.radius;
  110. }
  111.  
  112. void createBoundingBox(Circle circle1) {
  113.     int dy1 = circle1.y + circle1.radius;
  114.     int dy2 = circle1.y - circle1.radius;
  115.  
  116.     int dx1 = circle1.x - circle1.radius;
  117.     int dx2 = circle1.x + circle1.radius;
  118.  
  119.     int x1 = (dy1 + dx1) - circle1.radius;
  120.     int x2 = (dy1 + dx2) - circle1.radius;
  121.     int x3 = (dy2 + dx2) + circle1.radius;
  122.     int x4 = (dy2 + dx1) + circle1.radius;
  123.  
  124.     cout << "\nCoordinates of the upper left corner of the box: (x: " << x1 << " y: " << dy1 << ")" << endl;
  125.     cout << "\nCoordinates of the upper right corner of the box: (x: " << x2 << " y: " << dy1 << ")" << endl;
  126.     cout << "\nCoordinates of the lower right corner of the box: (x: " << x3 << " y: " << dy2 << ")" << endl;
  127.     cout << "\nCoordinates of the lower left corner of the box: (x: " << x4 << " y: " << dy2 << ")" << endl;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement