Difference between revisions of "DistanceBasedCollision"

(Grammar Correction)
(added a simpler function.)
Line 12: Line 12:
 
end
 
end
 
</source>
 
</source>
 +
 +
Don't want an extra square root function? Try this other function
 +
<source lang="lua">
 +
function checkCircularCollision(ax, ay, bx, by, ar, br)
 +
local dx = bx - ax
 +
local dy = by - ay
 +
return dx^2 + dy^2 < (ar + br)^2
 +
end
 +
</source>
 +
 +
Although I do keep the sqrt function to know that I am dealing with distances.
  
 
== Contributors ==
 
== Contributors ==

Revision as of 01:26, 20 November 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

Don't want an extra square root function? Try this other function

function checkCircularCollision(ax, ay, bx, by, ar, br)
	local dx = bx - ax
	local dy = by - ay
	return dx^2 + dy^2 < (ar + br)^2
end

Although I do keep the sqrt function to know that I am dealing with distances.

Contributors


Other Languages