Difference between revisions of "DistanceBasedCollision"

m (EPIC FAIL... typed in "bu" instead of "by")
(Code Optimized)
Line 9: Line 9:
 
local dy = by - ay
 
local dy = by - ay
 
local dist = math.sqrt(dx * dx + dy * dy)
 
local dist = math.sqrt(dx * dx + dy * dy)
if dist < ar + br then
+
return dist < ar + br
return true
 
else
 
return false
 
end
 
 
end
 
end
 
</source>
 
</source>

Revision as of 05:17, 5 October 2012

Another way of detecting collisions. This type works perfectly with circles. It checks if the distance of one object to the other is less than the sum of their radii(plural of radius.)

Variable Definitions : ax, ay = circleA's coordinates; bx, by = circleB's coordinates; ar, br = circleA's, and circleB's radii, respectively

function checkCircularCollision(ax, ay, bx, by, ar, br)
	local dx = bx - ax
	local dy = by - ay
	local dist = math.sqrt(dx * dx + dy * dy)
	return dist < ar + br
end

Contributors


Other Languages