Guest User

Untitled

a guest
Jun 28th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. glm::vec2 projectedPoint(glm::vec2 point, float slope)
  2. {
  3.     float inverseSlope = -1.0f / slope;
  4.     float b = point.y - (inverseSlope * point.x);
  5.     float x = b / (slope - inverseSlope);
  6.     float y = slope * x;
  7.     return glm::vec2(x, y);
  8. }
  9.  
  10. string vectorComparison(glm::vec2 vector1, glm::vec2 vector2)
  11. {
  12.     if (sqrt(pow(vector1.x, 2) + pow(vector1.y, 2)) > sqrt(pow(vector2.x, 2) + pow(vector2.y, 2)))
  13.         return "greater";
  14.  
  15.     else if (sqrt(pow(vector1.x, 2) + pow(vector1.y, 2)) < sqrt(pow(vector2.x, 2) + pow(vector2.y, 2)))
  16.         return "lesser";
  17.  
  18.     return "equal";
  19. }
  20.  
  21. float rotatedvalueY(float x, float y, float centerX, float centerY, float angle)
  22. {
  23.     angle = angle * (M_PI / 180.0f);
  24.     return centerY - (x - centerX)* sin(angle) + (centerY - y) * cos(angle);
  25. }
  26.  
  27. float rotatedvalueX(float x, float y, float centerX, float centerY, float angle)
  28. {
  29.     angle = angle * (M_PI / 180.0f);
  30.     return centerX + (x - centerX)* cos(angle) + (centerY - y) * sin(angle);
  31. }
  32.  
  33. float slope(glm::vec2 P1, glm::vec2 P2)
  34. {
  35.     return (P2.y - P1.y) / (P2.x - P1.x);
  36. };
  37.  
  38. bool SATcollision(glm::vec2 A_Topleft, glm::vec2 A_Topright, glm::vec2 A_Bottomright, glm::vec2 A_Bottomleft, glm::vec2 B_Topleft, glm::vec2 B_Topright, glm::vec2 B_Bottomright, glm::vec2 B_Bottomleft)
  39. {
  40.     glm::vec2 box1_point[]{A_Topleft, A_Topright, A_Bottomright, A_Bottomleft};
  41.     glm::vec2 box2_point[]{B_Topleft, B_Topright, B_Bottomright, B_Bottomleft};
  42.  
  43.     glm::vec2 box1_min(0, 0);
  44.     glm::vec2 box1_max(0, 0);
  45.     glm::vec2 box2_min(0, 0);
  46.     glm::vec2 box2_max(0, 0);
  47.  
  48.     glm::vec2 point1(0, 0);
  49.     glm::vec2 point2(0, 0);
  50.  
  51.     bool box1_min_undefined = true;
  52.     bool box2_min_undefined = true;
  53.     bool slopeUndefined = false;
  54.     float normal = 0;
  55.     float lineSlope = 0;
  56.  
  57.     for (int i = 0; i < 8; i++)
  58.     {
  59.         box1_min = glm::vec2(0, 0);
  60.         box1_max = glm::vec2(0, 0);
  61.         box2_min = glm::vec2(0, 0);
  62.         box2_max = glm::vec2(0, 0);
  63.  
  64.         box1_min_undefined = true;
  65.         box2_min_undefined = true;
  66.  
  67.         slopeUndefined = false;
  68.  
  69.         if (i < 4)
  70.         {
  71.             int q = i + 1;
  72.             if (q >= 4)
  73.                 q = 0;
  74.             lineSlope = slope(box1_point[i], box1_point[q]);
  75.            
  76.             point1 = box1_point[i];
  77.             point2 = box1_point[i + 1];
  78.  
  79.             if (lineSlope == 0 || box1_point[i].x - box1_point[i + 1].x == 0)
  80.                 slopeUndefined = true;
  81.            
  82.             normal = -1.0f / lineSlope;
  83.         }
  84.         else
  85.         {
  86.             int q = i - 4 + 1;
  87.             if (q >= 4)
  88.                 q = 0;
  89.             lineSlope = slope(box2_point[i - 4], box2_point[q]);
  90.  
  91.             point1 = box2_point[i - 4];
  92.             point2 = box2_point[i - 4 + 1];
  93.  
  94.             if (lineSlope == 0 || box2_point[i - 4].x - box2_point[i - 4 + 1].x == 0)
  95.                 slopeUndefined = true;
  96.  
  97.             normal = -1.0f / lineSlope;
  98.         }
  99.  
  100.         if (slopeUndefined)
  101.         {
  102.             if (point1.x - point2.x == 0)
  103.             {
  104.                 for (int p = 0; p < 8; p++)
  105.                 {
  106.                     if (p < 4)
  107.                     {
  108.                         if (vectorComparison(glm::vec2(box1_point[p].x, 0), box1_max) == "greater")
  109.                             box1_max = glm::vec2(box1_point[p].x, 0);
  110.  
  111.                         else if (vectorComparison(glm::vec2(box1_point[p].x, 0), box1_min) == "lesser" || box1_min_undefined == true)
  112.                         {
  113.                             box1_min = glm::vec2(box1_point[p].x, 0);
  114.                             box1_min_undefined = false;
  115.                         }
  116.                     }
  117.  
  118.                     else
  119.                     {
  120.                         if (vectorComparison(glm::vec2(box2_point[p - 4].x, 0), box2_max) == "greater")
  121.                             box2_max = glm::vec2(box2_point[p - 4].x, 0);
  122.  
  123.                         else if (vectorComparison(glm::vec2(box2_point[p - 4].x, 0), box2_min) == "lesser" || box2_min_undefined == true)
  124.                         {
  125.                             box2_min = glm::vec2(box2_point[p - 4].x, 0);
  126.                             box2_min_undefined = false;
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.  
  132.             else
  133.             {
  134.                 for (int p = 0; p < 8; p++)
  135.                 {
  136.                     if (p < 4)
  137.                     {
  138.                         if (vectorComparison(glm::vec2(0, box1_point[p].y), box1_max) == "greater")
  139.                             box1_max = glm::vec2(0, box1_point[p].y);
  140.  
  141.                         else if (vectorComparison(glm::vec2(0, box1_point[p].y), box1_min) == "lesser" || box1_min_undefined == true)
  142.                         {
  143.                             box1_min = glm::vec2(0, box1_point[p].y);
  144.                             box1_min_undefined = false;
  145.                         }
  146.                     }
  147.  
  148.  
  149.                     else
  150.                     {
  151.                         if (vectorComparison(glm::vec2(0, box2_point[p - 4].y), box2_max) == "greater")
  152.                             box2_max = glm::vec2(0, box2_point[p - 4].y);
  153.  
  154.                         else if (vectorComparison(glm::vec2(0, box2_point[p - 4].y), box2_min) == "lesser" || box2_min_undefined == true)
  155.                         {
  156.                             box2_min = glm::vec2(0, box2_point[p - 4].y);
  157.                             box2_min_undefined = false;
  158.                         }
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.  
  164.         if (!slopeUndefined && !lineSlope == 0)
  165.         {
  166.             for (int p = 0; p < 8; p++)
  167.             {
  168.                 if (p < 4)
  169.                 {
  170.                     if (vectorComparison(projectedPoint(box1_point[p], normal), box1_max) == "greater")
  171.                         box1_max = projectedPoint(box1_point[p], normal);
  172.  
  173.                     else if (vectorComparison(projectedPoint(box1_point[p], normal), box1_min) == "lesser" || box1_min_undefined == true)
  174.                     {
  175.                         box1_max = projectedPoint(box1_point[p], normal);
  176.                         box1_min_undefined = false;
  177.                     }
  178.                 }
  179.  
  180.                 else
  181.                 {
  182.                     if (vectorComparison(projectedPoint(box2_point[p - 4], normal), box2_max) == "greater")
  183.                         box2_max = projectedPoint(box2_point[p - 4], normal);
  184.  
  185.                     else if (vectorComparison(projectedPoint(box2_point[p - 4], normal), box2_min) == "lesser" || box2_min_undefined == true)
  186.                     {
  187.                         box2_max = projectedPoint(box2_point[p - 4], normal);
  188.                         box2_min_undefined = false;
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.  
  194.        
  195.  
  196.         if (vectorComparison(box2_min, box1_max) == "greater" || vectorComparison(box1_min, box2_max) == "greater") // || vectorComparison(box2_min, box1_max) == "equal" || vectorComparison(box1_min, box2_max) == "equal")
  197.             return false;
  198.  
  199.     }
  200.     return true;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment