Skip to content

Instantly share code, notes, and snippets.

@Ezbob
Created January 3, 2017 17:44
Show Gist options
  • Save Ezbob/f196015d467db338270f2d0f0217c853 to your computer and use it in GitHub Desktop.
Save Ezbob/f196015d467db338270f2d0f0217c853 to your computer and use it in GitHub Desktop.
collision detection 2d rects
local Algorithms = {}
Algorithms.Collision = {}
function Algorithms.Collision.is_colliding_rectangles(reactA, reactB)
local function getMinMax(react)
return {x = {min = math.min(react.upper.x, react.lower.x), max = math.max(react.upper.x, react.lower.x)},
y = {min = math.min(react.upper.y, react.lower.y), max = math.max(react.upper.y, react.lower.y)}}
end
local reactA_minmax = getMinMax(reactA)
local reactB_minmax = getMinMax(reactB)
local is_within_x_range = (
reactB_minmax.x.min <= reactA_minmax.x.min and reactA_minmax.x.min <= reactB_minmax.x.max or
reactB_minmax.x.min <= reactA_minmax.x.max and reactA_minmax.x.max <= reactB_minmax.x.max
)
local is_within_y_range = (
reactB_minmax.y.min <= reactA_minmax.y.min and reactA_minmax.y.min <= reactB_minmax.y.max or
reactB_minmax.y.min <= reactA_minmax.y.max and reactA_minmax.y.max <= reactB_minmax.y.max
)
return is_within_x_range and is_within_y_range
end
return Algorithms
@Ezbob
Copy link
Author

Ezbob commented Jan 3, 2017

The sorting step at line 7-13 is perhaps redundant when lower.x and lower.y is defined to be
lower.x < upper.x and lower.y < upper.y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment