Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Snippet: Collision check Circle2Circle — Gideros Forum

Snippet: Collision check Circle2Circle

MikeHartMikeHart Guru
edited January 2012 in Code snippets
Hi there,

here is a function, which will check a collision between 2 sprites by checkingif their collision circles overlap.
collCheck_Circle2C = function( source, target )
	local xf = source:getX() - target:getX()
	xf = xf * xf
	local yf = source:getY() - target:getY()
	yf = yf * yf
	local ssx, ssy = source:getScale()
	local tsx, tsy = target:getScale()
 
	local rf = (source.radius*ssx) + (target.radius*tsx)
	rf = rf * rf
	if (xf+yf) < rf then
    	return true
    end
    return false
end
Each sprite needs a radius property. Set or calculate it when you create the sprite. Or dynamically from the maximum of width and height during this function call. But then you don't need to multiply them with the scale factor.

Cheers
Michael
Sign In or Register to comment.