Checking circle and rectangle collision is easy, but this method doesn't seem useful for segments.
Code: Select all
--check if circle and rectangle intersect
function Circle_Rect_Intersect(cX,cY, cR, rX, rY, rW, rH)
if Point_Rect_Intersect(cX, cY, rX, rY, rW, rH) then
return true
else
local closestX = clamp(cX, rX, rX + rW)
local closestY = clamp(cY, rY, rY + rH)
local distX = cX - closestX
local distY = cY - closestY
local sqrDist = (distX * distX) + (distY * distY)
return sqrDist < cR * cR
end
end
--Check if point is inside rectangle
function Point_Rect_Intersect(pX, pY, rX, rY, rW, rH)
return pX > rX and pX < rX + rW and pY > rY and pY < rY + rH
end
function clamp(v, min, max)
if v < min then return min
elseif v > max then return max
else return v end
end