Can someone explain how to check if one rotated rectangle intersect other rectangle?
Lua implementation built in love2d framework. Collision detection function works in pure lua anyway
math.inf = 1e309
function love.load()
pol = {{0, 0}, {30, 2}, {8, 30}}
pol2 = {{60, 60}, {90, 61}, {98, 100}, {80, 100}}
end
function love.draw()
for k,v in ipairs(pol) do
love.graphics.line(pol[k][1], pol[k][2], pol[k % #pol + 1][1], pol[k % #pol + 1][2])
end
for k,v in ipairs(pol2) do
love.graphics.line(pol2[k][1], pol2[k][2], pol2[k % #pol2 + 1][1], pol2[k % #pol2 + 1][2])
end
end
function love.update(dt)
pol[1][1] = love.mouse.getX()
pol[1][2] = love.mouse.getY()
pol[2][1] = pol[1][1] + 30
pol[2][2] = pol[1][2] + 2
pol[3][1] = pol[1][1] + 8
pol[3][2] = pol[1][2] + 30
--lazy way to see that's function works
print(doPolygonsIntersect(pol, pol2))
end
-------------------------------------------------------------------------
function doPolygonsIntersect(a,b)
polygons = {a,b}
for i=1, #polygons do
polygon = polygons[i]
for i1=1, #polygon do
i2 = i1 % #polygon + 1
p1 = polygon[i1]
p2 = polygon[i2]
nx,ny = p2[2] - p1[2], p1[1] - p2[1]
minA = math.inf
maxA = -math.inf
for j=1, #a do
projected = nx * a[j][1] + ny * a[j][2]
if projected < minA then minA = projected end
if projected > maxA then maxA = projected end
end
minB = math.inf
maxB = -math.inf
for j=1, #b do
projected = nx * b[j][1] + ny * b[j][2]
if projected < minB then minB = projected end
if projected > maxB then maxB = projected end
end
if maxA < minB or maxB < minA then return false end
end
end
return true
end