TheHeckingDeveloper

ElementCollisions

Aug 6th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. --- A module responsible for checking for collisions between 2 elements ---
  2.  
  3. --- Math functions ---
  4. local abs = math.abs
  5.  
  6. --- Local functions ---
  7. local function absVector(vector)
  8.     return Vector2.new(abs(vector.X), abs(vector.Y))
  9. end
  10.  
  11. local function getVectors(element)
  12.     return element.AbsolutePosition, element.AbsoluteSize
  13. end
  14.  
  15. --- Returned function ---
  16. return function(element1, element2)
  17.     local pos1, size1 = getVectors(element1)
  18.     local pos2, size2 = getVectors(element2)
  19.    
  20.     local rangeVector = (size1 + size2) / 2
  21.     local distance = absVector(pos1 - pos2)
  22.    
  23.     if (distance.X <= rangeVector.X) and (distance.Y <= rangeVector.Y) then
  24.         return true
  25.     end
  26.    
  27.     return false
  28. end
Advertisement
Add Comment
Please, Sign In to add comment