Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct CollisionBox
- {
- glm::vec3 min;
- glm::vec3 max;
- };
- float normalizeAngle(float angle)
- {
- if (angle > 0)
- while (angle - 3.14 > 0)
- angle -= 3.14;
- else
- while (angle + 3.14 < 0)
- angle += 3.14;
- return angle;
- }
- CollisionBox GetBox(std::vector<glm::vec3> vertices, glm::mat4 transform)
- {
- glm::vec4 vector;
- glm::vec3 min, max;
- min.x = 1000;
- min.y = 1000;
- min.z = 1000;
- max.x = -1000;
- max.y = -1000;
- max.z = -1000;
- std::vector<glm::vec3> verticesVector = vertices;
- for (std::vector<glm::vec3>::iterator it = verticesVector.begin(); it != verticesVector.end(); ++it)
- {
- glm::vec3 p = *it;
- vector = glm::vec4(p, 1);
- vector = transform * vector;
- p = glm::vec3(vector[0], vector[1], vector[2]);
- if (p.x < min.x) min.x = p.x;
- if (p.y < min.y) min.y = p.y;
- if (p.z < min.z) min.z = p.z;
- if (p.x > max.x) max.x = p.x;
- if (p.y > max.y) max.y = p.y;
- if (p.z > max.z) max.z = p.z;
- }
- CollisionBox box;
- box.min = min;
- box.max = max;
- return box;
- }
- bool Collides(glm::mat4 matA, glm::mat4 matB)
- {
- matA = glm::scale(matA, glm::vec3(1, 1, 1));
- matB = glm::scale(matB, glm::vec3(1, 1, 1));
- CollisionBox a = GetBox(vertices, matA);
- CollisionBox b = GetBox(vertices, matB);
- int x = 0;
- if ((b.max.x > a.min.x) && (b.min.x < a.max.x)) x++;
- if ((b.max.y > a.min.y) && (b.min.y < a.max.y)) x++;
- if ((b.max.z > a.min.z) && (b.min.z < a.max.z)) x++;
- return x == 3;
- }
- ....................................
- if (Collides(modelMatrix, carModelMatrix) && !collided)
- {
- if (carSpeed >= 0)
- collideDir = true;
- else
- collideDir = false;
- carSpeed = 0;
- collided = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement