Game Development Reference
In-Depth Information
Identifying Overlapping Rectangles
The rectOverlaps function can be used to determine if two rectangles are overlapping. For
example, this could be used to determine whether the player is in contact with a spike or some
other object on the screen, and trigger an event accordingly.
function rectOverlaps( rect1X, rect1Y, rect1Width, rect1Height, rect2X, rect2Y, rect2Width,
rect2Height )
if rect1X + rect1Width >= rect2X and
rect2X + rect2Width <= rect1X and
rect1Y + rect1Height >= rect2Y and
rect2Y + rect2Height <= rect1Y then
return true
end
return false
end
Identifying Overlapping Circles
This can be used to determine if two circles are overlapping.
function circleOverlaps( circle1X, circle1Y, circle1Radius, circle2X, circle2Y, circle2Radius )
local dx = circle1X - circle2X
local dy = circle1Y - circle2Y
local rx = circle1Radius + circle2Radius
if dx * dx + dy * dy <= rx * rx then
return true
end
return false
end
Determining Whether a Circle Overlaps a Rectangle
This can help determine if a circle overlaps a rectangle.
function circleOverlapsRectangle( rectX, rectY, rectWidth, rectHeight, circleX, circleY,
circleRadius )
if rectX <= circleX and rectX + rectWidth >= circleX then
if rectY + rectHeight >= circleY - circleRadius and
rectY <= circleY + circleRadius then
return true
else
return false
end
elseif rectX <= circleY and rectX + rectHeight >= circleY then
 
Search WWH ::




Custom Search