Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: C++  |  size: 2.75 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. struct dsCollisionResult
  2. {
  3.         float distance;
  4.        
  5. };
  6.  
  7. struct dsCollisionCircle
  8. {
  9.         float radius;
  10.  
  11. };
  12.  
  13. struct dsCollisionBoundingBox
  14. {
  15.         float width, height;
  16.  
  17. };
  18.  
  19. struct dsPoint2
  20. {
  21.         float x, y;
  22.        
  23. };
  24.  
  25. struct dsPoint3
  26. {
  27.         float x, y, z;
  28.        
  29. };
  30.  
  31. struct dsCollisionPoly2D
  32. {
  33.         vector<dsPoint2> points;
  34.        
  35. };
  36.  
  37.  
  38.  
  39. // object data calculations
  40.  
  41. float boundingBoxCenterX(dsCollisionBoundingBox &bb)
  42. {
  43.         return (bb.width / 2.0f);
  44.        
  45. }
  46.  
  47. float boundingBoxCenterY(dsCollisionBoundingBox &bb)
  48. {
  49.         return (bb.height / 2.0f);
  50.        
  51. }
  52.  
  53.  
  54.  
  55. // result analysis
  56.  
  57. bool hasCollided(dsCollisionResult &result)
  58. {
  59.         return (result.distance < 0);
  60.        
  61. }
  62.  
  63.  
  64.  
  65. // collision circle vs circle
  66.  
  67. bool checkCollisionCircleVsCircle(dsCollisionResult &result, dsCollisionCircle* cir1, dsCollisionCircle*  cir2)
  68. {
  69.         result.distance = distance(objectA, objectB) - (cir1.radius + cir2.radius);
  70.        
  71.         return (result.distance < 0);
  72.        
  73. }
  74.  
  75.  
  76. // collision bounding box vs bounding box
  77.  
  78. bool checkCollisionBoundingBoxVsBoundingBox(dsCollisionResult &result, dsCollisionBoundingBox* bb1, dsCollisionBoundingBox*  bb2)
  79. {
  80.         return (abs(objectA.x - objectB.x) - (bb1.width+bb2.width)) < 0
  81.                 && (abs(objectA.y - objectB.y) - (bb1.height+bb2.height)) < 0;
  82.        
  83.         //result.distance =
  84.        
  85. }
  86.  
  87.  
  88.  
  89. // collision bounding box vs circle
  90.  
  91. bool checkCollisionBoundingBoxVsBoundingBox(dsCollisionResult &result, dsCollisionBoundingBox* bb, dsCollisionCircle*  cir)
  92. {
  93.         // objectA in this case contains bb, and objectB conatains cir
  94.        
  95.         // temp point data
  96.         dsPoint2 Bc;
  97.        
  98.         // STEP ONE
  99.         // CALC Bc
  100.        
  101.         if(objectB.x > objectA.x+boundingBoxCenterX(bb))
  102.         // cir is to the right of bb's right border
  103.         {
  104.                 // Bc cannot get any farther out right than this
  105.                 Bc.x = objectA.x+boundingBoxCenterX(bb);
  106.                
  107.         }
  108.         else if(objectB.x < objectA.x-boundingBoxCenterX(bb))
  109.         // cir is to the left of bb's top border
  110.         {
  111.                 // Bc cannot get any farther out left than this
  112.                 Bc.x = objectA.x-boundingBoxCenterX(bb);
  113.                
  114.         }
  115.         else
  116.         // now you're somewhere inbetween the left and right edges
  117.         // Bc's x should reflect the x of the circle
  118.         {
  119.                 Bc.x = objectB.x;
  120.                
  121.         }
  122.        
  123.        
  124.         if(objectB.y > objectA.y+boundingBoxCenterY(bb))
  125.         // cir is under bb's bottom border
  126.         {
  127.                 // Bc cannot get any farther down than this
  128.                 Bc.y = objectA.y+boundingBoxCenterY(bb);
  129.                
  130.         }
  131.         else if(objectB.y < objectA.y-boundingBoxCenterY(bb))
  132.         // cir is above bb's top border
  133.         {
  134.                 // Bc cannot get any farther above
  135.                 Bc.y = objectA.y-boundingBoxCenterY(bb);
  136.                
  137.         }
  138.         else
  139.         // now you're somewhere inbetween the top and bottom edges
  140.         // Bc's y should reflect the y of the circle
  141.         {
  142.                 Bc.y = objectB.y;
  143.                
  144.         }
  145.        
  146.        
  147.         // STEP TWO
  148.         // DISTANCE FACTOR Bc AND CIRCLE
  149.        
  150.         return (distance(objectA, objectB) - cir.radius) < 0;
  151.        
  152. }