Difference between revisions of "DistanceBasedCollision"

(Added missing property for the category page.)
Line 1: Line 1:
 +
== Collision between two circles ==
 +
 
Another way of detecting collisions. This type works perfectly with circles.
 
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.)
 
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 center coordinates; bx, by = circleB's center coordinates; ar, br = circleA's and circleB's radii, respectively.
+
Variable Definitions :  
 +
x1, y1, r1 = first circle's center coordinates and radius;  
 +
x2, y2, r2 = second circle's center coordinates and radius.
  
 
<source lang="lua">
 
<source lang="lua">
function checkCircularCollision(ax, ay, bx, by, ar, br)
+
function checkCircularCollision(x1, y1, r1, x2, y2, r2)
local dx = bx - ax
+
local dx = x2 - x1
local dy = by - ay
+
local dy = y2 - y1
 
local dist = math.sqrt(dx * dx + dy * dy)
 
local dist = math.sqrt(dx * dx + dy * dy)
return dist < ar + br
+
return dist < r1 + r2
 
end
 
end
 
</source>
 
</source>
Line 15: Line 19:
 
Don't want an extra square root function? Try this other function
 
Don't want an extra square root function? Try this other function
 
<source lang="lua">
 
<source lang="lua">
function checkCircularCollision(ax, ay, bx, by, ar, br)
+
function checkCircularCollision(x1, y1, r1, x2, y2, r2)
local dx = bx - ax
+
local dx, dy = x2 - x1, y2 - y1
local dy = by - ay
+
return dx^2 + dy^2 < (r1 + r2)^2
return dx^2 + dy^2 < (ar + br)^2
 
 
end
 
end
 
</source>
 
</source>
Line 25: Line 28:
  
 
Do note that while the second may seem faster since it avoids a costly square root, that's still one function, instead of the second version's three squarings. That said, Löve uses LuaJIT, so if one really needs to optimize, check which is faster.
 
Do note that while the second may seem faster since it avoids a costly square root, that's still one function, instead of the second version's three squarings. That said, Löve uses LuaJIT, so if one really needs to optimize, check which is faster.
 +
 +
 +
== Collision between circle and rectangle ==
 +
<source lang="lua">
 +
function checkCircleToRectangleCollision(cx, cy, cr, rectX, rectY, rectW, rectH)
 +
local nearestX = math.max(rectX, math.min(cx, rectX + rectW))
 +
local nearestY = math.max(rectY, math.min(cy, rectY + rectH))
 +
local dx, dy = math.abs(cx - nearestX), math.abs(cy - nearestY)
 +
if dx > cr or dy > cr then return false end
 +
return dx*dx + dy*dy < cr*cr
 +
end
 +
</source>
 +
  
 
[[Category:Snippets]]
 
[[Category:Snippets]]

Revision as of 16:46, 16 January 2024

Collision between two circles

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 : x1, y1, r1 = first circle's center coordinates and radius; x2, y2, r2 = second circle's center coordinates and radius.

function checkCircularCollision(x1, y1, r1, x2, y2, r2)
	local dx = x2 - x1
	local dy = y2 - y1
	local dist = math.sqrt(dx * dx + dy * dy)
	return dist < r1 + r2
end

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

function checkCircularCollision(x1, y1, r1, x2, y2, r2)
	local dx, dy = x2 - x1, y2 - y1
	return dx^2 + dy^2 < (r1 + r2)^2
end

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

Do note that while the second may seem faster since it avoids a costly square root, that's still one function, instead of the second version's three squarings. That said, Löve uses LuaJIT, so if one really needs to optimize, check which is faster.


Collision between circle and rectangle

function checkCircleToRectangleCollision(cx, cy, cr, rectX, rectY, rectW, rectH)
	local nearestX = math.max(rectX, math.min(cx, rectX + rectW))
	local nearestY = math.max(rectY, math.min(cy, rectY + rectH))
	local dx, dy = math.abs(cx - nearestX), math.abs(cy - nearestY)
	if dx > cr or dy > cr then return false end
	return dx*dx + dy*dy < cr*cr
end


Other Languages