Game Development Reference
In-Depth Information
Using pointInCircle
In the tower-defense genre of games, each defense tower or structure has a range; this range
is displayed in the form of a circle around the center of the structure. We can check whether
the midpoint of the enemy is inside of this circle to determine if the enemy is in the range of this
structure or not, as follows:
function pointInCircle( ptX, ptY, circleX, circleY, radius )
local dx = ptX - circleX
local dy = ptY - circleY
if dx * dx + dy * dy <= radius * radius then
return true
end
return false
rect2Height )
if rect1X >= rect2X and rect1X + rect1Width <= rect2X + rect2Width and
rect1Y >= rect2Y and rect1Y + rect1Height <= rect2Y + rect2Height then
return true
end
return false
end
Determining Whether a Circle Is Within Another Circle
Similarly, you might want to check whether a circular shape is completely contained inside another
one. Here's the function to use for this:
function circleInCircle( 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
 
Search WWH ::




Custom Search