Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1.  
  2. struct CollisionBox
  3. {
  4. glm::vec3 min;
  5. glm::vec3 max;
  6. };
  7.  
  8. float normalizeAngle(float angle)
  9. {
  10. if (angle > 0)
  11. while (angle - 3.14 > 0)
  12. angle -= 3.14;
  13. else
  14. while (angle + 3.14 < 0)
  15. angle += 3.14;
  16.  
  17. return angle;
  18. }
  19.  
  20. CollisionBox GetBox(std::vector<glm::vec3> vertices, glm::mat4 transform)
  21. {
  22. glm::vec4 vector;
  23. glm::vec3 min, max;
  24.  
  25. min.x = 1000;
  26. min.y = 1000;
  27. min.z = 1000;
  28.  
  29. max.x = -1000;
  30. max.y = -1000;
  31. max.z = -1000;
  32.  
  33.  
  34. std::vector<glm::vec3> verticesVector = vertices;
  35.  
  36. for (std::vector<glm::vec3>::iterator it = verticesVector.begin(); it != verticesVector.end(); ++it)
  37. {
  38. glm::vec3 p = *it;
  39. vector = glm::vec4(p, 1);
  40. vector = transform * vector;
  41. p = glm::vec3(vector[0], vector[1], vector[2]);
  42.  
  43. if (p.x < min.x) min.x = p.x;
  44. if (p.y < min.y) min.y = p.y;
  45. if (p.z < min.z) min.z = p.z;
  46.  
  47. if (p.x > max.x) max.x = p.x;
  48. if (p.y > max.y) max.y = p.y;
  49. if (p.z > max.z) max.z = p.z;
  50. }
  51.  
  52.  
  53. CollisionBox box;
  54.  
  55. box.min = min;
  56. box.max = max;
  57.  
  58. return box;
  59. }
  60.  
  61. bool Collides(glm::mat4 matA, glm::mat4 matB)
  62. {
  63.  
  64. matA = glm::scale(matA, glm::vec3(1, 1, 1));
  65. matB = glm::scale(matB, glm::vec3(1, 1, 1));
  66.  
  67. CollisionBox a = GetBox(vertices, matA);
  68. CollisionBox b = GetBox(vertices, matB);
  69.  
  70. int x = 0;
  71.  
  72. if ((b.max.x > a.min.x) && (b.min.x < a.max.x)) x++;
  73. if ((b.max.y > a.min.y) && (b.min.y < a.max.y)) x++;
  74. if ((b.max.z > a.min.z) && (b.min.z < a.max.z)) x++;
  75.  
  76. return x == 3;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. ....................................
  83.  
  84. if (Collides(modelMatrix, carModelMatrix) && !collided)
  85. {
  86. if (carSpeed >= 0)
  87. collideDir = true;
  88. else
  89. collideDir = false;
  90.  
  91. carSpeed = 0;
  92. collided = true;
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement