Advertisement
Guest User

SAT collision

a guest
Sep 1st, 2012
1,927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. class Vector2D
  6. {
  7. public :
  8.     // empty constructor
  9.     Vector2D(){};
  10.     // default constructor
  11.     Vector2D(double p_x,double p_y):x(p_x),y(p_y)
  12.     {
  13.         // calculating the magnitude
  14.         magnitude = sqrt((x*x) + (y*y));
  15.         // calculating the normal of the vectors
  16.         norm_x = x / magnitude;
  17.         norm_y = y / magnitude;
  18.     }
  19.  
  20.     // this is used to get the normal vector ( not normalized !! )
  21.     Vector2D& perp()
  22.     {
  23.         Vector2D normal;
  24.         // vector of (x,y) => (-y,x) or (y,-x) i'm using the first one
  25.         normal = Vector2D(-(this->y),this->x);
  26.         return normal;
  27.     }
  28.    
  29.     // this is used to get the dot product ( for the projection )
  30.     double dot(Vector2D& other)
  31.     {
  32.         double DotProduct;
  33.         DotProduct = (this->x * other.x + this->y * other.y);
  34.         return DotProduct;
  35.     }
  36.  
  37.     // used to check if two vectors overlap
  38.     bool overlap(Vector2D& other)
  39.     {
  40.         bool overlap = false;
  41.         // getting this vector
  42.         Vector2D offset = Vector2D(other.x - this->x,other.y - this->y);
  43.         if (offset.magnitude < this->magnitude)
  44.             overlap = true;
  45.         return overlap;
  46.     }
  47.  
  48.     // overloading assignment operators
  49.     Vector2D& operator + (const Vector2D& other)
  50.     {
  51.         Vector2D resultant;
  52.         resultant = Vector2D(other.x + this->x,other.y + this-> y);
  53.         return resultant;
  54.     }
  55.  
  56.     Vector2D& operator - (const Vector2D& other)
  57.     {
  58.         Vector2D resultant;
  59.         resultant = Vector2D(other.x - this->x,other.y - this-> y);
  60.         return resultant;
  61.     }
  62.  
  63.     Vector2D& operator * (double factor)
  64.     {
  65.         Vector2D resultant;
  66.         resultant = Vector2D(this->x * factor,this->y * factor);
  67.         return resultant;
  68.     }
  69.  
  70.     double magnitude;  
  71.     double x;
  72.     double y;
  73.     double norm_x;
  74.     double norm_y;
  75. };
  76.  
  77. class ConvexPolygon
  78. {
  79. public :
  80.     // empty constructor
  81.     ConvexPolygon(){};
  82.     // default constructor
  83.     ConvexPolygon(std::vector<Vector2D> list):vertices(list)
  84.     {
  85.         // calculating the edge vectors
  86.         for (int i = 0; i < vertices.size(); i++)
  87.         {
  88.             // getting the current vertex :
  89.             Vector2D p1 = vertices[i];
  90.             // getting the next vertex :
  91.             Vector2D p2 = vertices[i + 1 == vertices.size() ? 0 : i + 1];
  92.             // getting the edge vector :
  93.             Vector2D edge = p2 - p1;
  94.             // getting the perpendicular :
  95.             Vector2D normal = edge.perp();
  96.             // pushing it back to the edges vector and the axes vector
  97.             edges.push_back(edge);
  98.             axes.push_back(normal);
  99.         }
  100.     }
  101.  
  102.     // used for projection, will return a reference to a Vector2D instance as projection
  103.     Vector2D& proj(Vector2D& other)
  104.     {
  105.         // normalizing the vector for accurate precision
  106.         Vector2D NormOther = Vector2D(other.norm_x,other.norm_y);
  107.         // we get the projection with the min and max dot product
  108.         double min = other.dot(axes[0]);
  109.         double max = min;
  110.         // looping through the vertices and doing the dot product
  111.         for (int i = 0; i < vertices.size(); i++)
  112.         {
  113.             double p = NormOther.dot(vertices[i]);
  114.             if (p < min)
  115.                 min = p;
  116.             else if (p > max)
  117.                 max = p;
  118.         }
  119.         // creating the vector that will be then returned
  120.         Vector2D proj = Vector2D(min,max);
  121.         return proj;
  122.     }
  123.  
  124.     std::vector<Vector2D> vertices;
  125.     std::vector<Vector2D> edges;
  126.     std::vector<Vector2D> axes;
  127.  
  128. };
  129.  
  130. bool SatCollisionCheck(ConvexPolygon& Poly1,ConvexPolygon& Poly2)
  131. {
  132.     for (int i = 0; i < Poly1.axes.size(); i ++)
  133.     {
  134.         // projecting both shapes onto the axis
  135.         Vector2D Proj1 = Poly1.proj(Poly1.axes[i]);
  136.         Vector2D Proj2 = Poly2.proj(Poly1.axes[i]);
  137.         // if the overlap will be returned false otherwise continue the loop
  138.         std::cout << Proj1.x << "|" << Proj1.y << " : " << Proj2.x << "|" << Proj2.y << '\n';
  139.         if (!Proj1.overlap(Proj2))
  140.             return false;
  141.     }
  142.     return true;
  143. }
  144.  
  145. /**
  146. APPLICATION ENTRY POINT
  147. **/
  148. int main()
  149. {
  150.     // Polygon 1
  151.     std::vector<Vector2D> Vertices1;
  152.     Vertices1.push_back(Vector2D(0,0));
  153.     Vertices1.push_back(Vector2D(10,0));
  154.     Vertices1.push_back(Vector2D(10,20));
  155.     Vertices1.push_back(Vector2D(0,10));
  156.     // Polygon 2
  157.     std::vector<Vector2D> Vertices2;
  158.     Vertices2.push_back(Vector2D(9,19));
  159.     Vertices2.push_back(Vector2D(27,576));
  160.     Vertices2.push_back(Vector2D(400,500));
  161.     Vertices2.push_back(Vector2D(9,150));
  162.     // Creating two shapes from the std::vector of Vector2D
  163.     ConvexPolygon Polygon1 = ConvexPolygon(Vertices1);
  164.     ConvexPolygon Polygon2 = ConvexPolygon(Vertices2);
  165.  
  166.     // getting the collision
  167.     bool collide = SatCollisionCheck(Polygon1,Polygon2);
  168.     // printing it to the screen :
  169.     std::cout << collide << std::endl;
  170.     std::cin.get();
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement