Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- glm::vec2 projectedPoint(glm::vec2 point, float slope)
- {
- float inverseSlope = -1.0f / slope;
- float b = point.y - (inverseSlope * point.x);
- float x = b / (slope - inverseSlope);
- float y = slope * x;
- return glm::vec2(x, y);
- }
- string vectorComparison(glm::vec2 vector1, glm::vec2 vector2)
- {
- if (sqrt(pow(vector1.x, 2) + pow(vector1.y, 2)) > sqrt(pow(vector2.x, 2) + pow(vector2.y, 2)))
- return "greater";
- else if (sqrt(pow(vector1.x, 2) + pow(vector1.y, 2)) < sqrt(pow(vector2.x, 2) + pow(vector2.y, 2)))
- return "lesser";
- return "equal";
- }
- float rotatedvalueY(float x, float y, float centerX, float centerY, float angle)
- {
- angle = angle * (M_PI / 180.0f);
- return centerY - (x - centerX)* sin(angle) + (centerY - y) * cos(angle);
- }
- float rotatedvalueX(float x, float y, float centerX, float centerY, float angle)
- {
- angle = angle * (M_PI / 180.0f);
- return centerX + (x - centerX)* cos(angle) + (centerY - y) * sin(angle);
- }
- float slope(glm::vec2 P1, glm::vec2 P2)
- {
- return (P2.y - P1.y) / (P2.x - P1.x);
- };
- 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)
- {
- glm::vec2 box1_point[]{A_Topleft, A_Topright, A_Bottomright, A_Bottomleft};
- glm::vec2 box2_point[]{B_Topleft, B_Topright, B_Bottomright, B_Bottomleft};
- glm::vec2 box1_min(0, 0);
- glm::vec2 box1_max(0, 0);
- glm::vec2 box2_min(0, 0);
- glm::vec2 box2_max(0, 0);
- glm::vec2 point1(0, 0);
- glm::vec2 point2(0, 0);
- bool box1_min_undefined = true;
- bool box2_min_undefined = true;
- bool slopeUndefined = false;
- float normal = 0;
- float lineSlope = 0;
- for (int i = 0; i < 8; i++)
- {
- box1_min = glm::vec2(0, 0);
- box1_max = glm::vec2(0, 0);
- box2_min = glm::vec2(0, 0);
- box2_max = glm::vec2(0, 0);
- box1_min_undefined = true;
- box2_min_undefined = true;
- slopeUndefined = false;
- if (i < 4)
- {
- int q = i + 1;
- if (q >= 4)
- q = 0;
- lineSlope = slope(box1_point[i], box1_point[q]);
- point1 = box1_point[i];
- point2 = box1_point[i + 1];
- if (lineSlope == 0 || box1_point[i].x - box1_point[i + 1].x == 0)
- slopeUndefined = true;
- normal = -1.0f / lineSlope;
- }
- else
- {
- int q = i - 4 + 1;
- if (q >= 4)
- q = 0;
- lineSlope = slope(box2_point[i - 4], box2_point[q]);
- point1 = box2_point[i - 4];
- point2 = box2_point[i - 4 + 1];
- if (lineSlope == 0 || box2_point[i - 4].x - box2_point[i - 4 + 1].x == 0)
- slopeUndefined = true;
- normal = -1.0f / lineSlope;
- }
- if (slopeUndefined)
- {
- if (point1.x - point2.x == 0)
- {
- for (int p = 0; p < 8; p++)
- {
- if (p < 4)
- {
- if (vectorComparison(glm::vec2(box1_point[p].x, 0), box1_max) == "greater")
- box1_max = glm::vec2(box1_point[p].x, 0);
- else if (vectorComparison(glm::vec2(box1_point[p].x, 0), box1_min) == "lesser" || box1_min_undefined == true)
- {
- box1_min = glm::vec2(box1_point[p].x, 0);
- box1_min_undefined = false;
- }
- }
- else
- {
- if (vectorComparison(glm::vec2(box2_point[p - 4].x, 0), box2_max) == "greater")
- box2_max = glm::vec2(box2_point[p - 4].x, 0);
- else if (vectorComparison(glm::vec2(box2_point[p - 4].x, 0), box2_min) == "lesser" || box2_min_undefined == true)
- {
- box2_min = glm::vec2(box2_point[p - 4].x, 0);
- box2_min_undefined = false;
- }
- }
- }
- }
- else
- {
- for (int p = 0; p < 8; p++)
- {
- if (p < 4)
- {
- if (vectorComparison(glm::vec2(0, box1_point[p].y), box1_max) == "greater")
- box1_max = glm::vec2(0, box1_point[p].y);
- else if (vectorComparison(glm::vec2(0, box1_point[p].y), box1_min) == "lesser" || box1_min_undefined == true)
- {
- box1_min = glm::vec2(0, box1_point[p].y);
- box1_min_undefined = false;
- }
- }
- else
- {
- if (vectorComparison(glm::vec2(0, box2_point[p - 4].y), box2_max) == "greater")
- box2_max = glm::vec2(0, box2_point[p - 4].y);
- else if (vectorComparison(glm::vec2(0, box2_point[p - 4].y), box2_min) == "lesser" || box2_min_undefined == true)
- {
- box2_min = glm::vec2(0, box2_point[p - 4].y);
- box2_min_undefined = false;
- }
- }
- }
- }
- }
- if (!slopeUndefined && !lineSlope == 0)
- {
- for (int p = 0; p < 8; p++)
- {
- if (p < 4)
- {
- if (vectorComparison(projectedPoint(box1_point[p], normal), box1_max) == "greater")
- box1_max = projectedPoint(box1_point[p], normal);
- else if (vectorComparison(projectedPoint(box1_point[p], normal), box1_min) == "lesser" || box1_min_undefined == true)
- {
- box1_max = projectedPoint(box1_point[p], normal);
- box1_min_undefined = false;
- }
- }
- else
- {
- if (vectorComparison(projectedPoint(box2_point[p - 4], normal), box2_max) == "greater")
- box2_max = projectedPoint(box2_point[p - 4], normal);
- else if (vectorComparison(projectedPoint(box2_point[p - 4], normal), box2_min) == "lesser" || box2_min_undefined == true)
- {
- box2_max = projectedPoint(box2_point[p - 4], normal);
- box2_min_undefined = false;
- }
- }
- }
- }
- 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")
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment