Advertisement
Guest User

Sat

a guest
Jul 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstdlib>
  3. #include<cstddef>
  4. #include<algorithm>
  5.  
  6. // gets the size of an array
  7. template<typename T, size_t ARRAY_S>
  8. size_t size_(T(&)[ARRAY_S]){
  9.   return ARRAY_S;
  10. }
  11.  
  12. class Vector{
  13.      public:
  14.             double x,y;
  15.      Vector(double x,double y){
  16.      this->x=x;
  17.      this->y=y;
  18.      }
  19.  
  20.      // return a vector p orthogonal to q
  21.     Vector ortho(){
  22.     return  Vector(y,-x);
  23.     }
  24.  
  25.      };
  26.  
  27. Vector *polyEdges(Vector[],int);
  28. Vector project(Vector[],Vector,int);
  29. bool isOverlap(Vector,Vector);
  30.  
  31. // sat algorithm function
  32.  bool  isColiding(Vector poly1[],Vector poly2[],int poly1Size,int poly2Size){
  33.  int axesSize=poly1Size+poly2Size;
  34.  //edges of the 1st poly
  35.  Vector *edge1=polyEdges(poly1,poly1Size);
  36.  //edges of 2nd poly
  37.  Vector *edge2= polyEdges(poly2,poly2Size);
  38.  //holds all the edges of poly1&poly2
  39.  Vector *edges= (Vector*)malloc((axesSize)*sizeof(Vector));
  40.  Vector *Axis;
  41.  // copy the value of the edges of poly one
  42.   for(int i=0;i<poly1Size;i++){
  43.          edges[i]=edge1[i];
  44.   }
  45.   // copy the values of the edges of poly 2
  46.    for(int i =poly1Size;i<axesSize;i++){
  47.          edges[i]=edge2[i];
  48.    }
  49.  
  50.    Axis=(Vector*)malloc((axesSize)*sizeof(Vector));
  51.    // get the orthogonal value of the current vector
  52.    for(int i=0;i<axesSize;i++){
  53.      Axis[i]=edges->ortho();
  54.    }
  55.    // are the axis colliding or not
  56.    for(int i=0;i<axesSize;i++){
  57.          if(isOverlap(project(poly1,Axis[i],poly1Size),project(poly2,Axis[i],poly2Size))){
  58.            // return true if its overlapping
  59.               return true;
  60.  
  61.    }}
  62.  
  63.    return false;
  64.  }
  65. // function that returns a vector from  a1 to a2
  66. Vector edgeVec(Vector a1, Vector a2){
  67.  double x=a2.x-a1.x;
  68.  double y=a2.y-a1.y;
  69.   return  Vector(x,y);
  70. }
  71.  
  72. //function that return edges of a polygon as  list vector
  73.  Vector *polyEdges(Vector polygon[],int len){
  74.  
  75.    Vector *edges= (Vector*)malloc(len*sizeof(Vector));
  76.  
  77.     for(int i=0;i<len;i++){
  78.       edges[i]=edgeVec(polygon[i],polygon[(i+1)%len]);
  79.     }
  80.       return edges;
  81.  }
  82.  
  83.  
  84.   // return the dot product of a1 and a2
  85.   double dotPoduct(Vector a1, Vector a2){
  86.    return (a1.x*a2.x)+(a1.y*a2.y);
  87.    }
  88.  
  89.    //projection
  90. Vector project(Vector p[],Vector axis,int len){
  91.    double dotP[len];
  92.     double max_=INT_MAX;
  93.  
  94.      double min_=INT_MIN;
  95.  
  96.      // get the dot product
  97.       for(int i=0;i<len;i++){
  98.         dotP[i]=dotPoduct(p[i],axis);
  99.       }
  100.       //get the min and max value of the dotproduct
  101.      max_=dotP[0];
  102.       min_=dotP[0];
  103.       for(int i=0;i<len;i++){
  104.         if(dotP[i]<min_){
  105.          min_=dotP[i];
  106.         }
  107.           if(dotP[i]>max_){
  108.             max_=dotP[i];
  109.             }}
  110.           return Vector(min_,max_);
  111.  
  112.    }
  113.    // return true if is  the 2 vectors are overlapping
  114.    bool isOverlap(Vector p1, Vector p2){
  115.      return((p1.x<=p2.y)&&(p2.x<=p1.y));
  116.    }
  117. int main()
  118. {
  119.  
  120.  
  121.   double w=30;
  122.   double h=30;
  123.   double x=40;
  124.   double y=40;
  125.   double x2=90;
  126.   double y2=90;
  127.  
  128.   Vector test1[]={Vector(x,y),  Vector(x+w,y),Vector(x,y+h),Vector(x+w,y+h),Vector(x+w/2,y+h/2)};
  129.  Vector test2[]={ Vector(x2,y2), Vector(x2+w,y2),Vector(x2,y2+h),Vector(x2+w,y2+h),Vector(x2+w,y2+h)};
  130.  
  131.   if(isColiding(test1,test2,size_(test1),size_(test2))){
  132.   std::cout<<"Collision";
  133.   }else{
  134.     std::cout<<"No colision";
  135.   }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement