collision.lua 1.5 KB

1234567891011121314151617181920212223242526272829
  1. -- collision check file for Navy
  2. local collision = {}
  3. function collision.check(obj1, obj2)
  4. --local k = {x = 600, y = 400, width = 10, height = 10}
  5. --obj2 = obj2 or k
  6. if obj1.x >= obj2.x and obj1.x <= obj2.x + obj2.width and obj1.y >= obj2.y and obj1.y <= obj2.y + obj2.height then
  7. return true
  8. elseif obj1.x + obj1.width >= obj2.x and obj1.x + obj1.width <= obj2.x + obj2.width and obj1.y + obj1.height >= obj2.y and obj1.y + obj1.height <= obj2.y + obj2.height then
  9. return true
  10. elseif obj1.x + obj1.width >= obj2.x and obj1.x + obj1.width <= obj2.x + obj2.width and obj1.y >= obj2.y and obj1.y <= obj2.y + obj2.height then
  11. return true
  12. elseif obj1.x >= obj2.x and obj1.x <= obj2.x and obj1.y + obj1.height >= obj2.y and obj1.y + obj1.height <= obj2.y + obj2.height then
  13. return true
  14. elseif obj2.x >= obj1.x and obj2.x <= obj1.x + obj1.width and obj2.y >= obj1.y and obj2.y <= obj1.y + obj1.height then
  15. return true
  16. elseif obj2.x + obj2.width >= obj1.x and obj2.x + obj2.width <= obj1.x + obj1.width and obj2.y + obj2.height >= obj1.y and obj2.y + obj2.height <= obj1.y + obj1.height then
  17. return true
  18. elseif obj2.x + obj2.width >= obj1.x and obj2.x + obj2.width <= obj1.x + obj1.width and obj2.y >= obj1.y and obj2.y <= obj1.y + obj1.height then
  19. return true
  20. elseif obj2.x >= obj1.x and obj2.x <= obj1.x and obj2.y + obj2.height >= obj1.y and obj2.y + obj2.height <= obj1.y + obj1.height then
  21. return true
  22. else
  23. return false
  24. end
  25. end
  26. return collision