Advertisement
Rakoonic

Simple collision detection routines

May 28th, 2012
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. --------------------------------------------------------------
  2. -- GENERAL COLLISION ROUTINES --------------------------------
  3.  
  4. local class    = {}
  5.  
  6. local mathAbs = math.abs
  7.  
  8. function class.pointInRect( pointX, pointY, left, top, width, height )
  9.     if pointX >= left and pointX <= left + width and pointY >= top and pointY <= top + height then return true
  10.     else                                                                                           return false ; end
  11. end
  12. function class.pointInCircle( pointX, pointY, centerX, centerY, radius )
  13.     local dX, dY = pointX - centerX, pointY - centerY
  14.     if dX * dX + dY * dY <= radius * radius then return true
  15.     else                                         return false ; end
  16. end
  17. function class.pointInDiamond( pointX, pointY, centerX, centerY, radius )
  18.     if mathAbs( pointX - centerX ) + mathAbs( pointY - centerY ) <= radius then return true
  19.     else                                                                        return false ; end
  20. end
  21. function class.rectInRect( left1, top1, width1, height1, left2, top2, width2, height2 )
  22.     if left1 >= left2 and left1 + width1 <= left2 + width2 and top1 >= top2 and top1 + height1 <= top2 + height2 then return true
  23.     else                                                                                                              return false ; end
  24. end
  25. function class.circleInCircle( centerX1, centerY1, radius1, centerX2, centerY2, radius2 )
  26.     if radius1 > radius2 then return false ; end
  27.     local dX, dY, radii = centerX1 - centerX2, centerY1 - centerY2, radius2 - radius1
  28.     if dX * dX + dY * dY <= radii * radii then return true
  29.     else                                       return false ; end
  30. end
  31. function class.diamondInDiamond( centerX1, centerY1, radius1, centerX2, centerY2, radius2 )
  32.     if radius1 > radius2 then return false ; end
  33.         if mathAbs( centerX1 - centerX2 ) + mathAbs( centerY1 - centerY2 ) <= radius2 - radius1 then return true
  34.     else                                                                                   return false ; end
  35. end
  36. function class.intersectRects( left1, top1, width1, height1, left2, top2, width2, height2 )
  37.     if left1 + width1 >= left2 and left1 <= left2 + width2 and top1 + height1 >= top2 and top1 <= top2 + height2 then return true
  38.     else                                                                                                              return false ; end
  39. end
  40. function class.intersectCircles( centerX1, centerY1, radius1, centerX2, centerY2, radius2 )
  41.     local dX, dY, radii = centerX1 - centerX2, centerY1 - centerY2, radius1 + radius2
  42.     if dX * dX + dY * dY <= radii * radii then return true
  43.     else                                       return false ; end
  44. end
  45. function class.intersectDiamonds( centerX1, centerY1, radius1, centerX2, centerY2, radius2 )
  46.     if mathAbs( centerX1 - centerX2 ) + mathAbs( centerY1 - centerY2 ) <= radius1 + radius2 then return true
  47.     else                                                                                   return false ; end
  48. end
  49. function class.intersectRectWithCircle( left, top, width, height, centerX, centerY, radius )
  50.    
  51.     -- Are you within the rectangular extensions?
  52.     if left <= centerX and left + width >= centerX then
  53.         if top + height >= centerY - radius and top <= centerY + radius then return true
  54.         else                                                                 return false ; end
  55.     elseif top <= centerY and top + height >= centerY then
  56.         if left + width >= centerX - radius and left <= centerX + radius then return true
  57.         else                                                                  return false ; end
  58.     else
  59.  
  60.         -- Curved bits - split into the 4 corner checks
  61.         if left < centerX then
  62.             if top < centerY then return class.pointInCircle( left + width, top + height, centerX, centerY, radius )
  63.             else                  return class.pointInCircle( left + width, top, centerX, centerY, radius ) ; end
  64.         else
  65.             if top < centerY then return class.pointInCircle( left, top + height, centerX, centerY, radius )
  66.             else                  return class.pointInCircle( left, top, centerX, centerY, radius ) ; end
  67.         end
  68.     end
  69.        
  70. end
  71. function class.intersectRectWithDiamond( left, top, width, height, centerX, centerY, radius )
  72.    
  73.     -- Are you within the rectangular extensions?
  74.     if left <= centerX and left + width >= centerX then
  75.         if top + height >= centerY - radius and top <= centerY + radius then return true
  76.         else                                                                 return false ; end
  77.     elseif top <= centerY and top + height >= centerY then
  78.         if left + width >= centerX - radius and left <= centerX + radius then return true
  79.         else                                                                  return false ; end
  80.     else
  81.    
  82.         -- Diagonal bits - split into the 4 corner checks
  83.         if left < centerX then
  84.             if top < centerY then return class.pointInDiamond( left + width, top + height, centerX, centerY, radius )
  85.             else                  return class.pointInDiamond( left + width, top, centerX, centerY, radius ) ; end
  86.         else
  87.             if top < centerY then return class.pointInDiamond( left, top + height, centerX, centerY, radius )
  88.             else                  return class.pointInDiamond( left, top, centerX, centerY, radius ) ; end
  89.         end
  90.     end
  91.    
  92. end
  93.  
  94. --------------------------------------------------------------
  95. --------------------------------------------------------------
  96.  
  97. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement