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 : | + | 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( | + | function checkCircularCollision(x1, y1, r1, x2, y2, r2) |
− | local dx = | + | local dx = x2 - x1 |
− | local dy = | + | local dy = y2 - y1 |
local dist = math.sqrt(dx * dx + dy * dy) | local dist = math.sqrt(dx * dx + dy * dy) | ||
− | return dist < | + | 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( | + | function checkCircularCollision(x1, y1, r1, x2, y2, r2) |
− | local dx = | + | local dx, dy = x2 - x1, y2 - y1 |
− | + | return dx^2 + dy^2 < (r1 + r2)^2 | |
− | return dx^2 + dy^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
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info