Guest User

Untitled

a guest
Nov 19th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. float Body::CalculateOverlap(float aMinProj, float aMaxProj, float bMinProj, float bMaxProj){
  2. return std::min(bMaxProj, aMaxProj) - std::max(aMinProj, bMinProj);
  3. }
  4.  
  5. //ALL COLLIDERS ARE BOXES
  6. //vertices is simply a glm::vec3 array
  7. void Body::CheckCollision(Body* other){
  8.  
  9. if(isStatic && other->isStatic){
  10. return;
  11. }
  12.  
  13. glm::vec3 axis;
  14.  
  15. //calculate the fucking normal axis:
  16.  
  17. glm::vec3 mtvAxis;
  18. float overlap = std::numeric_limits<float>::infinity();
  19.  
  20. for(uint32_t i = 0; i < COLLIDER_VERTEX_COUNT; i++){
  21. glm::vec3 curr = vertices[i];
  22. glm::vec3 edges[2];
  23.  
  24. edges[0] = vertices[(i + 1) % COLLIDER_VERTEX_COUNT]- curr;
  25. edges[1] = vertices[(i + 2) % COLLIDER_VERTEX_COUNT]- curr;
  26.  
  27. //surface relative to box1
  28. axis = glm::normalize(glm::cross(edges[1], edges[0]));
  29.  
  30. OverlapInfo overlapInfo = CheckOverlap(this, other, axis);
  31. if(!overlapInfo.isOverlapping){
  32. return;
  33. }
  34. if(overlapInfo.overlap < overlap){
  35. overlap = overlapInfo.overlap;
  36. mtvAxis = axis;
  37. }
  38.  
  39. curr = other->vertices[i];
  40. edges[0] = other->vertices[(i + 1) % COLLIDER_VERTEX_COUNT]- curr;
  41. edges[1] = other->vertices[(i + 2) % COLLIDER_VERTEX_COUNT]- curr;
  42.  
  43. //surface relative to box2
  44. axis = glm::normalize(glm::cross(edges[1], edges[0]));
  45.  
  46. overlapInfo = CheckOverlap(this, other, axis);
  47. if(!overlapInfo.isOverlapping){
  48. return;
  49. }
  50. if(overlapInfo.overlap < overlap){
  51. overlap = overlapInfo.overlap;
  52. mtvAxis = axis;
  53. }
  54.  
  55. //surface relative to cross products of boxes 1 and 2
  56. axis = glm::normalize(glm::cross(vertices[(i + 1) % COLLIDER_VERTEX_COUNT] - curr,
  57. other->vertices[(i + 1) % COLLIDER_VERTEX_COUNT] - other->vertices[i]));
  58.  
  59. overlapInfo = CheckOverlap(this, other, axis);
  60. if(!overlapInfo.isOverlapping){
  61. return;
  62. }
  63. if(overlapInfo.overlap < overlap){
  64. overlap = overlapInfo.overlap;
  65. mtvAxis = axis;
  66. }
  67.  
  68. //another surface relative to cross products of boxes 1 and 2
  69. axis = glm::normalize(glm::cross(vertices[(i + 2) % COLLIDER_VERTEX_COUNT] - curr,
  70. other->vertices[(i + 2) % COLLIDER_VERTEX_COUNT] - other->vertices[i]));
  71.  
  72. overlapInfo = CheckOverlap(this, other, axis);
  73. if(!overlapInfo.isOverlapping){
  74. return;
  75. }
  76.  
  77. if(overlapInfo.overlap < overlap){
  78. //get the minimal distance and direction to travel to resolve the collision
  79. overlap = overlapInfo.overlap;
  80. mtvAxis = axis;
  81. }
  82.  
  83. }
  84.  
  85. //Some weird floating point weirdness, this fixed it
  86. mtvAxis.x = static_cast<float>(static_cast<int>(mtvAxis.x * 10)) / 10;
  87. mtvAxis.y = static_cast<float>(static_cast<int>(mtvAxis.y * 10)) / 10;
  88. mtvAxis.z = static_cast<float>(static_cast<int>(mtvAxis.z * 10)) / 10;
  89.  
  90.  
  91. //Resolving collision
  92. pos -= mtvAxis * overlap;
  93.  
  94. vel.y = 0;
  95.  
  96. }
  97.  
  98. OverlapInfo Body::CheckOverlap(Body* b1, Body* b2, glm::vec3 axis){
  99.  
  100.  
  101. OverlapInfo overlapInfo;
  102.  
  103. float aMaxProj = -std::numeric_limits<float>::infinity();
  104. float aMinProj = std::numeric_limits<float>::infinity();
  105.  
  106. float bMaxProj = -std::numeric_limits<float>::infinity();
  107. float bMinProj = std::numeric_limits<float>::infinity();
  108.  
  109.  
  110. for(glm::vec3 p : b1->vertices){
  111. float proj = glm::dot(axis, p);
  112.  
  113. if(proj < aMinProj){
  114. aMinProj = proj;
  115. }
  116. if(proj > aMaxProj){
  117. aMaxProj = proj;
  118. }
  119. }
  120.  
  121. for(glm::vec3 p : b2->vertices){
  122. float proj = glm::dot(axis, p);
  123.  
  124. if(proj < bMinProj){
  125. bMinProj = proj;
  126. }
  127. if(proj > bMaxProj){
  128. bMaxProj = proj;
  129. }
  130. }
  131. if(aMaxProj < bMinProj || bMaxProj < aMinProj){
  132. overlapInfo.isOverlapping = false;
  133. overlapInfo.overlap = std::numeric_limits<float>::infinity();
  134. return overlapInfo;
  135. }
  136.  
  137. overlapInfo.isOverlapping = true;
  138. overlapInfo.overlap = CalculateOverlap(aMinProj, aMaxProj, bMinProj, bMaxProj);
  139. return overlapInfo;
  140. }
  141.  
Add Comment
Please, Sign In to add comment