greatly simplify overlap detection

Exploits the knowledge that w and h are non-negative.

All it took was a question on the LÖVE Discord, which pointed me at the
technical term of AABB (axis-aligned bounding box) collision detection.
This commit is contained in:
Kartik K. Agaram 2023-12-10 10:31:45 -08:00
parent b2a22736be
commit 15aef61fd5
5 changed files with 2 additions and 17 deletions

View File

@ -1,3 +0,0 @@
straddles_interval = function(lo,hi, border)
return lo <= border and hi >= border
end

View File

@ -1,6 +0,0 @@
overlapping_intervals = function(alo,ahi, blo,bhi)
return straddles_interval(alo,ahi, blo)
or straddles_interval(alo,ahi, bhi)
or straddles_interval(blo, bhi, alo)
or straddles_interval(blo, bhi, ahi)
end

View File

@ -1,5 +1,5 @@
-- return true if a is less than some distance from b
overlapping_areas = function(a, b)
return overlapping_intervals(a.x, a.x+a.w, b.x-20, b.x+b.w+20)
and overlapping_intervals(a.y, a.y+a.h, b.y-20, b.y+b.h+60) -- leave more space below existing definitions
return a.x < b.x + b.w+20 and a.x + a.w > b.x-20
and a.y < b.y + b.h+60 and a.y + a.h > b.y-20
end

View File

@ -1,3 +0,0 @@
test_straddles_interval = function()
check(straddles_interval(-20, 21, 0), '1')
end

View File

@ -1,3 +0,0 @@
test_overlapping_intervals = function()
check(overlapping_intervals(0, 1, -20, 21), 'within')
end