Help needed working out if object is within radius of a circle

Using the code below, how would I work out if the green blob is within the radius of the expanding circle? I’ve tried various methods, all of which were overly complicated, and none of which worked :frowning:
Obviously, in the finished app the green blob would be moving.
[lua]display.setStatusBar(display.HiddenStatusBar)
math.randomseed(os.time())
_h=display.contentHeight
_w=display.contentWidth
_m=math.random
_x=display.contentCenterX
_y=display.contentCenterY

local fred=display.newCircle(_x + 130,_y-50,30)
fred:setFillColor(0,255,0)
fred.life=100
lifeleft = display.newText(fred.life, 0, 0, “HelveticaNeue”, 24)

local maxstars=44
local stars={}
for f=1, maxstars do
stars[f]= display.newCircle(_x,_y,4)
stars[f]:setFillColor(170,0,255)
stars[f].angle=f
stars[f].speed=2
stars[f].alpha=1
stars[f].radius=0
end

function updateStars()
for u=1, maxstars do
stars[u].x= stars[u].x + (math.cos(stars[u].angle) * stars[u].speed)
stars[u].y= stars[u].y + (math.sin(stars[u].angle) * stars[u].speed)
stars[u].alpha=stars[u].alpha-.01
if stars[u].alpha <= 0 then – This only here temporarily.
stars[u].x=_x
stars[u].y=_y
stars[u].alpha=1
end

end
end

function updateGame()
updateStars()
end
Runtime:addEventListener(“enterFrame”, updateGame)[/lua]

Cheers. [import]uid: 7841 topic_id: 18200 reply_id: 318200[/import]

did you tried hitTestObject?
check this: http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds [import]uid: 16142 topic_id: 18200 reply_id: 69552[/import]

Cheers for that. I hadn’t tried hitTestObject as I need to know wether the blob is anywhere within the radius of the circle, not just overlapping. Thanks anyway. [import]uid: 7841 topic_id: 18200 reply_id: 69554[/import]

You need to do find the distance from the centre of the green circle to the centre of the expanding circle, then compare that to the sum of the radius of the green circle and the expanding circle. If the distance is less than the sum, you have an overlap.

[lua]function updateGame()
updateStars()
– get distance from fred to the expanding circle
– going from centre to centre
local diffX = fred.x - _x
local diffY = fred.y - _y
local distance = math.sqrt(diffX * diffX + diffY * diffY)
– fred’s radius
local fredRadius = fred.width / 2
– radius of the circle of stars
local starsDiffX = stars[1].x - _x
local starsDiffY = stars[1].y - _y
local starsRadius = math.sqrt(starsDiffX * starsDiffX + starsDiffY * starsDiffY)
– compare the two
if fredRadius + starsRadius > distance then
print(“Overlap”)
end
end[/lua]

This is pretty inefficient, since there’s a lot of stuff that doesn’t seem to be changing and so it could be pre-calculated. But if your objects are going to be moving, then you’ll need to recalculate at least some of these.

Darren [import]uid: 1294 topic_id: 18200 reply_id: 69570[/import]

@Darren,
That is absolutely spot on! Thank you so so much.
*respect* [import]uid: 7841 topic_id: 18200 reply_id: 69585[/import]