Code: Select all
function update( dt )
local p1 = line.point1
local p2 = line.point2
hit = true
if p1.x < x and p2.x < x then
hit = false
return
end
if p1.x > x + w and p2.x > x + w then
hit = false
return
end
if p1.y < y and p2.y < y then
hit = false
return
end
if p1.y > y+h and p2.y > y+h then
hit = false
return
end
-- last case: first build normal vector
local nx,ny = p2.y-p1.y, -(p2.x-p1.x)
local vx,vy = x-p1.x, y-p1.y
local min = nx*vx+ny*vy
local max = min
vx,vy = x+w-p1.x, y-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
vx,vy = x+w-p1.x, y+h-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
vx,vy = x-p1.x, y+h-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
if min*max>0 then
hit = false
end
end