Skip to content

Instantly share code, notes, and snippets.

@Luiji
Created July 14, 2010 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luiji/476161 to your computer and use it in GitHub Desktop.
Save Luiji/476161 to your computer and use it in GitHub Desktop.
box collision algorithm in lua
-- This script was written by Luiji Maryo, http://github.com/Luiji .
-- It has been released under the Public Domain.
-- Collision detection function.
-- Checks if box1 and box2 overlap.
-- w and h mean width and height.
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1 -- Is b2 under b1?
then
return false -- No collision. Yay!
else
return true -- Yes collision. Ouch!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment