2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
geometric intersection routines
|
2020-03-15 10:22:22 +00:00
|
|
|
|
2021-07-20 06:20:15 +00:00
|
|
|
from simple point tests to shape vs shape tests
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-07-20 06:20:15 +00:00
|
|
|
optimised pretty well in most places
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-07-20 06:20:15 +00:00
|
|
|
tests provided:
|
|
|
|
overlap
|
|
|
|
boolean "is overlapping"
|
|
|
|
collide
|
|
|
|
nil for no collision
|
|
|
|
minimum separating vector on collision
|
|
|
|
provided in the direction of the first object
|
|
|
|
optional output parameters to avoid garbage generation
|
2020-01-29 03:26:28 +00:00
|
|
|
]]
|
|
|
|
|
2020-04-07 03:51:13 +00:00
|
|
|
local path = (...):gsub("intersect", "")
|
|
|
|
local vec2 = require(path .. "vec2")
|
2021-10-24 23:32:14 +00:00
|
|
|
local mathx = require(path .. "mathx")
|
2020-03-15 10:22:22 +00:00
|
|
|
|
|
|
|
--module storage
|
2020-01-29 03:26:28 +00:00
|
|
|
local intersect = {}
|
|
|
|
|
|
|
|
--epsilon for collisions
|
|
|
|
local COLLIDE_EPS = 1e-6
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- circles
|
|
|
|
|
|
|
|
function intersect.circle_point_overlap(pos, rad, v)
|
|
|
|
return pos:distance_squared(v) < rad * rad
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.circle_circle_overlap(a_pos, a_rad, b_pos, b_rad)
|
|
|
|
local rad = a_rad + b_rad
|
|
|
|
return a_pos:distance_squared(b_pos) < rad * rad
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.circle_circle_collide(a_pos, a_rad, b_pos, b_rad, into)
|
|
|
|
--get delta
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta = a_pos
|
|
|
|
:pooled_copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
:vector_sub_inplace(b_pos)
|
2020-01-29 03:26:28 +00:00
|
|
|
--squared threshold
|
|
|
|
local rad = a_rad + b_rad
|
2021-06-29 04:43:55 +00:00
|
|
|
local dist = delta:length_squared()
|
|
|
|
local res = false
|
2020-01-29 03:26:28 +00:00
|
|
|
if dist < rad * rad then
|
|
|
|
if dist == 0 then
|
|
|
|
--singular case; just resolve vertically
|
|
|
|
dist = 1
|
2021-07-23 04:15:30 +00:00
|
|
|
delta:scalar_set(0, 1)
|
2020-01-29 03:26:28 +00:00
|
|
|
else
|
|
|
|
--get actual distance
|
|
|
|
dist = math.sqrt(dist)
|
|
|
|
end
|
|
|
|
--allocate if needed
|
|
|
|
if into == nil then
|
2021-07-23 04:15:30 +00:00
|
|
|
into = vec2(0)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
--normalise, scale to separating distance
|
2021-07-23 04:15:30 +00:00
|
|
|
res = into:set(delta)
|
|
|
|
:scalar_div_inplace(dist)
|
|
|
|
:scalar_mul_inplace(rad - dist)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2021-06-29 04:43:55 +00:00
|
|
|
delta:release()
|
|
|
|
return res
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:34:47 +00:00
|
|
|
function intersect.circle_point_collide(a_pos, a_rad, b, into)
|
|
|
|
return intersect.circle_circle_collide(a_pos, a_rad, b, 0, into)
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- line segments
|
|
|
|
-- todo: separate double-sided, one-sided, and pull-through (along normal) collisions?
|
|
|
|
|
2020-04-16 07:17:53 +00:00
|
|
|
--get the nearest point on the line segment a from point b
|
|
|
|
function intersect.nearest_point_on_line(a_start, a_end, b_pos, into)
|
2021-07-23 04:15:30 +00:00
|
|
|
if into == nil then into = vec2(0) end
|
2020-04-16 07:17:53 +00:00
|
|
|
--direction of segment
|
2021-07-23 04:15:30 +00:00
|
|
|
local segment = a_end:pooled_copy()
|
|
|
|
:vector_sub_inplace(a_start)
|
2020-01-29 03:26:28 +00:00
|
|
|
--detect degenerate case
|
2020-04-16 07:17:53 +00:00
|
|
|
local lensq = segment:length_squared()
|
|
|
|
if lensq <= COLLIDE_EPS then
|
2021-07-23 04:15:30 +00:00
|
|
|
into:set(a_start)
|
2020-04-16 07:17:53 +00:00
|
|
|
else
|
|
|
|
--solve for factor along segment
|
2021-07-23 04:15:30 +00:00
|
|
|
local point_to_start = b_pos:pooled_copy()
|
|
|
|
:vector_sub_inplace(a_start)
|
2021-11-22 23:25:09 +00:00
|
|
|
local factor = mathx.clamp01(point_to_start:dot(segment) / lensq)
|
2020-04-16 07:17:53 +00:00
|
|
|
point_to_start:release()
|
2021-07-23 04:15:30 +00:00
|
|
|
into:set(segment)
|
|
|
|
:scalar_mul_inplace(factor)
|
|
|
|
:vector_add_inplace(a_start)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-04-16 07:17:53 +00:00
|
|
|
segment:release()
|
2020-01-29 03:26:28 +00:00
|
|
|
return into
|
|
|
|
end
|
|
|
|
|
2021-05-13 02:24:45 +00:00
|
|
|
--internal
|
2021-07-23 04:15:30 +00:00
|
|
|
--vector from line seg origin to point
|
2020-04-16 07:17:53 +00:00
|
|
|
function intersect._line_to_point(a_start, a_end, b_pos, into)
|
2021-07-23 04:15:30 +00:00
|
|
|
return intersect.nearest_point_on_line(a_start, a_end, b_pos, into)
|
|
|
|
:vector_sub_inplace(b_pos)
|
2020-04-16 07:17:53 +00:00
|
|
|
end
|
|
|
|
|
2021-05-13 02:24:45 +00:00
|
|
|
--internal
|
2020-01-29 03:26:28 +00:00
|
|
|
--line displacement vector from separation vector
|
|
|
|
function intersect._line_displacement_to_sep(a_start, a_end, separation, total_rad)
|
2021-07-23 04:15:30 +00:00
|
|
|
local distance = separation:normalise_len_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
local sep = distance - total_rad
|
|
|
|
if sep <= 0 then
|
|
|
|
if distance <= COLLIDE_EPS then
|
|
|
|
--point intersecting the line; push out along normal
|
2021-07-23 04:15:30 +00:00
|
|
|
separation:set(a_end)
|
|
|
|
:vector_sub_inplace(a_start)
|
|
|
|
:normalise_inplace()
|
|
|
|
:rot90l_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
else
|
2021-07-23 04:15:30 +00:00
|
|
|
separation:scalar_mul_inplace(-sep)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
return separation
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-05-13 02:24:45 +00:00
|
|
|
--overlap a line segment with a circle
|
|
|
|
function intersect.line_circle_overlap(a_start, a_end, a_rad, b_pos, b_rad)
|
2021-05-13 06:13:30 +00:00
|
|
|
local nearest = intersect.nearest_point_on_line(a_start, a_end, b_pos, vec2:pooled())
|
2021-05-13 02:24:45 +00:00
|
|
|
local overlapped = intersect.circle_point_overlap(b_pos, a_rad + b_rad, nearest)
|
|
|
|
nearest:release()
|
|
|
|
return overlapped
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--collide a line segment with a circle
|
|
|
|
function intersect.line_circle_collide(a_start, a_end, a_rad, b_pos, b_rad, into)
|
|
|
|
into = intersect._line_to_point(a_start, a_end, b_pos, into)
|
|
|
|
return intersect._line_displacement_to_sep(a_start, a_end, into, a_rad + b_rad)
|
|
|
|
end
|
|
|
|
|
|
|
|
--collide 2 line segments
|
|
|
|
function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_rad, into)
|
|
|
|
--segment directions from start points
|
2021-07-23 04:34:47 +00:00
|
|
|
local a_dir = a_end
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(a_start)
|
|
|
|
local b_dir = b_end
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(b_start)
|
2020-01-29 03:26:28 +00:00
|
|
|
|
|
|
|
--detect degenerate cases
|
|
|
|
local a_degen = a_dir:length_squared() <= COLLIDE_EPS
|
2022-03-29 06:56:02 +00:00
|
|
|
local b_degen = b_dir:length_squared() <= COLLIDE_EPS
|
2021-07-23 04:34:47 +00:00
|
|
|
if a_degen or b_degen then
|
2021-07-23 04:15:30 +00:00
|
|
|
vec2.release(a_dir, b_dir)
|
2021-07-23 04:34:47 +00:00
|
|
|
if a_degen and b_degen then
|
|
|
|
--actually just circles
|
|
|
|
return intersect.circle_circle_collide(a_start, a_rad, b_start, b_rad, into)
|
|
|
|
elseif a_degen then
|
|
|
|
--a is just circle
|
|
|
|
return intersect.circle_line_collide(a_start, a_rad, b_start, b_end, b_rad, into)
|
|
|
|
elseif b_degen then
|
|
|
|
--b is just circle
|
|
|
|
return intersect.line_circle_collide(a_start, a_end, a_rad, b_start, b_rad, into)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
--otherwise we're _actually_ 2 line segs :)
|
2021-07-23 04:15:30 +00:00
|
|
|
if into == nil then into = vec2(0) end
|
2020-01-29 03:26:28 +00:00
|
|
|
|
|
|
|
--first, check intersection
|
|
|
|
|
|
|
|
--(c to lua translation of paul bourke's
|
|
|
|
-- line intersection algorithm)
|
|
|
|
local dx1 = (a_end.x - a_start.x)
|
|
|
|
local dx2 = (b_end.x - b_start.x)
|
|
|
|
local dy1 = (a_end.y - a_start.y)
|
|
|
|
local dy2 = (b_end.y - b_start.y)
|
|
|
|
local dxab = (a_start.x - b_start.x)
|
|
|
|
local dyab = (a_start.y - b_start.y)
|
|
|
|
|
|
|
|
local denom = dy2 * dx1 - dx2 * dy1
|
|
|
|
local numera = dx2 * dyab - dy2 * dxab
|
|
|
|
local numerb = dx1 * dyab - dy1 * dxab
|
|
|
|
|
|
|
|
--check coincident lines
|
2022-03-03 08:01:20 +00:00
|
|
|
local intersected
|
2020-01-29 03:26:28 +00:00
|
|
|
if
|
|
|
|
math.abs(numera) < COLLIDE_EPS and
|
|
|
|
math.abs(numerb) < COLLIDE_EPS and
|
|
|
|
math.abs(denom) < COLLIDE_EPS
|
|
|
|
then
|
|
|
|
intersected = "both"
|
|
|
|
else
|
|
|
|
--check parallel, non-coincident lines
|
|
|
|
if math.abs(denom) < COLLIDE_EPS then
|
|
|
|
intersected = "none"
|
|
|
|
else
|
|
|
|
--get interpolants along segments
|
|
|
|
local mua = numera / denom
|
|
|
|
local mub = numerb / denom
|
|
|
|
--intersection outside segment bounds?
|
|
|
|
local outside_a = mua < 0 or mua > 1
|
|
|
|
local outside_b = mub < 0 or mub > 1
|
|
|
|
if outside_a and outside_b then
|
|
|
|
intersected = "none"
|
|
|
|
elseif outside_a then
|
|
|
|
intersected = "b"
|
|
|
|
elseif outside_b then
|
|
|
|
intersected = "a"
|
|
|
|
else
|
|
|
|
intersected = "both"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-03 08:01:20 +00:00
|
|
|
assert(intersected)
|
2021-07-23 04:15:30 +00:00
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
if intersected == "both" then
|
|
|
|
--simply displace along A normal
|
2021-07-23 04:15:30 +00:00
|
|
|
into:set(a_dir)
|
|
|
|
vec2.release(a_dir, b_dir)
|
|
|
|
return into
|
|
|
|
:normalise_inplace()
|
|
|
|
:scalar_mul_inplace(a_rad + b_rad)
|
|
|
|
:rot90l_inplace()
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
vec2.release(a_dir, b_dir)
|
|
|
|
|
|
|
|
--dumb as a rock check-corners approach
|
|
|
|
--todo: pool storage
|
|
|
|
--todo proper calculus from http://geomalgorithms.com/a07-_distance.html
|
|
|
|
local search_tab = {}
|
|
|
|
--only insert corners from the non-intersected line
|
|
|
|
--since intersected line is potentially the apex
|
|
|
|
if intersected ~= "a" then
|
|
|
|
--a endpoints
|
|
|
|
table.insert(search_tab, {intersect._line_to_point(b_start, b_end, a_start), 1})
|
|
|
|
table.insert(search_tab, {intersect._line_to_point(b_start, b_end, a_end), 1})
|
|
|
|
end
|
|
|
|
if intersected ~= "b" then
|
|
|
|
--b endpoints
|
|
|
|
table.insert(search_tab, {intersect._line_to_point(a_start, a_end, b_start), -1})
|
|
|
|
table.insert(search_tab, {intersect._line_to_point(a_start, a_end, b_end), -1})
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
local best = nil
|
|
|
|
local best_len = nil
|
|
|
|
for _, v in ipairs(search_tab) do
|
|
|
|
local len = v[1]:length_squared()
|
|
|
|
if not best_len or len < best_len then
|
|
|
|
best = v
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
--fix direction
|
|
|
|
into:set(best[1])
|
|
|
|
:scalar_mul_inplace(best[2])
|
|
|
|
|
|
|
|
return intersect._line_displacement_to_sep(a_start, a_end, into, a_rad + b_rad)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- axis aligned bounding boxes
|
|
|
|
|
|
|
|
--return true on overlap, false otherwise
|
|
|
|
function intersect.aabb_point_overlap(pos, hs, v)
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta = pos
|
|
|
|
:pooled_copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
:vector_sub_inplace(v)
|
|
|
|
:abs_inplace()
|
2021-06-29 04:43:55 +00:00
|
|
|
local overlap = delta.x < hs.x and delta.y < hs.y
|
|
|
|
delta:release()
|
|
|
|
return overlap
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-05-10 15:53:40 +00:00
|
|
|
-- discrete displacement
|
|
|
|
-- return msv to push point to closest edge of aabb
|
|
|
|
function intersect.aabb_point_collide(pos, hs, v, into)
|
2021-06-08 01:51:18 +00:00
|
|
|
--separation between centres
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta_c = v
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(pos)
|
2021-06-08 01:51:18 +00:00
|
|
|
--absolute separation
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta_c_abs = delta_c
|
|
|
|
:pooled_copy()
|
|
|
|
:abs_inplace()
|
2021-06-29 04:43:55 +00:00
|
|
|
local res = false
|
|
|
|
if delta_c_abs.x < hs.x and delta_c_abs.y < hs.y then
|
2021-07-23 04:15:30 +00:00
|
|
|
res = (into or vec2(0))
|
2021-06-08 01:51:18 +00:00
|
|
|
--separating offset in both directions
|
2021-07-23 04:15:30 +00:00
|
|
|
:set(hs)
|
|
|
|
:vector_sub_inplace(delta_c_abs)
|
2021-06-08 01:51:18 +00:00
|
|
|
--minimum separating distance
|
2021-07-23 04:15:30 +00:00
|
|
|
:minor_inplace()
|
2021-06-08 01:51:18 +00:00
|
|
|
--in the right direction
|
2021-07-23 04:15:30 +00:00
|
|
|
:vector_mul_inplace(delta_c:sign_inplace())
|
2021-06-08 01:51:18 +00:00
|
|
|
--from the aabb's point of view
|
2021-07-23 04:15:30 +00:00
|
|
|
:inverse_inplace()
|
2021-05-10 15:53:40 +00:00
|
|
|
end
|
2021-06-29 04:43:55 +00:00
|
|
|
vec2.release(delta_c, delta_c_abs)
|
|
|
|
return res
|
2021-05-10 15:53:40 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--return true on overlap, false otherwise
|
2021-06-08 01:51:18 +00:00
|
|
|
function intersect.aabb_aabb_overlap(a_pos, a_hs, b_pos, b_hs)
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta = a_pos
|
|
|
|
:pooled_copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
:vector_sub_inplace(b_pos)
|
|
|
|
:abs_inplace()
|
2021-07-23 04:34:47 +00:00
|
|
|
local total_size = a_hs
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_add_inplace(b_hs)
|
2021-06-29 04:43:55 +00:00
|
|
|
local overlap = delta.x < total_size.x and delta.y < total_size.y
|
|
|
|
vec2.release(delta, total_size)
|
|
|
|
return overlap
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--discrete displacement
|
|
|
|
--return msv on collision, false otherwise
|
2021-06-08 01:51:18 +00:00
|
|
|
function intersect.aabb_aabb_collide(a_pos, a_hs, b_pos, b_hs, into)
|
2021-07-23 04:34:47 +00:00
|
|
|
local delta = a_pos
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(b_pos)
|
|
|
|
local abs_delta = delta
|
|
|
|
:pooled_copy()
|
|
|
|
:abs_inplace()
|
|
|
|
local size = a_hs
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_add_inplace(b_hs)
|
|
|
|
local abs_amount = size
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(abs_delta)
|
2021-06-29 04:43:55 +00:00
|
|
|
local res = false
|
|
|
|
if abs_amount.x > COLLIDE_EPS and abs_amount.y > COLLIDE_EPS then
|
2021-07-23 04:34:47 +00:00
|
|
|
if not into then into = vec2(0) end
|
2020-01-29 03:26:28 +00:00
|
|
|
--actually collided
|
2021-06-29 04:43:55 +00:00
|
|
|
if abs_amount.x <= abs_amount.y then
|
2020-01-29 03:26:28 +00:00
|
|
|
--x min
|
2021-10-24 23:32:14 +00:00
|
|
|
res = into:scalar_set(abs_amount.x * mathx.sign(delta.x), 0)
|
2020-01-29 03:26:28 +00:00
|
|
|
else
|
|
|
|
--y min
|
2021-10-24 23:32:14 +00:00
|
|
|
res = into:scalar_set(0, abs_amount.y * mathx.sign(delta.y))
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
2021-06-29 04:43:55 +00:00
|
|
|
return res
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-05-10 15:55:00 +00:00
|
|
|
-- helper function to clamp point to aabb
|
2021-06-08 01:51:18 +00:00
|
|
|
function intersect.aabb_point_clamp(pos, hs, v, into)
|
2021-07-23 04:34:47 +00:00
|
|
|
local v_min = pos
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_sub_inplace(hs)
|
|
|
|
local v_max = pos
|
|
|
|
:pooled_copy()
|
|
|
|
:vector_add_inplace(hs)
|
2021-07-23 04:15:30 +00:00
|
|
|
into = into or vec2(0)
|
2021-07-23 04:34:47 +00:00
|
|
|
into:set(v)
|
|
|
|
:clamp_inplace(v_min, v_max)
|
2021-06-08 01:51:18 +00:00
|
|
|
vec2.release(v_min, v_max)
|
|
|
|
return into
|
2021-05-10 15:55:00 +00:00
|
|
|
end
|
|
|
|
|
2021-06-08 01:51:18 +00:00
|
|
|
-- return true on overlap, false otherwise
|
|
|
|
function intersect.aabb_circle_overlap(a_pos, a_hs, b_pos, b_rad)
|
|
|
|
local clamped = intersect.aabb_point_clamp(a_pos, a_hs, b_pos, vec2:pooled())
|
|
|
|
local edge_distance_squared = clamped:distance_squared(b_pos)
|
|
|
|
clamped:release()
|
|
|
|
return edge_distance_squared < (b_rad * b_rad)
|
2021-05-10 15:55:51 +00:00
|
|
|
end
|
|
|
|
|
2021-05-10 16:29:37 +00:00
|
|
|
-- return msv on collision, false otherwise
|
2021-06-08 01:51:18 +00:00
|
|
|
function intersect.aabb_circle_collide(a_pos, a_hs, b_pos, b_rad, into)
|
2021-07-23 04:34:47 +00:00
|
|
|
local abs_delta = a_pos
|
|
|
|
:pooled_copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
:vector_sub_inplace(b_pos)
|
|
|
|
:abs_inplace()
|
2021-06-08 01:51:18 +00:00
|
|
|
--circle centre within aabb-like bounds, collide as an aabb
|
|
|
|
local like_aabb = abs_delta.x < a_hs.x or abs_delta.y < a_hs.y
|
|
|
|
--(clean up)
|
|
|
|
abs_delta:release()
|
|
|
|
--
|
|
|
|
local result
|
|
|
|
if like_aabb then
|
2021-07-23 04:15:30 +00:00
|
|
|
local pretend_hs = vec2:pooled(0, 0)
|
2021-06-08 01:51:18 +00:00
|
|
|
result = intersect.aabb_aabb_collide(a_pos, a_hs, b_pos, pretend_hs, into)
|
|
|
|
pretend_hs:release()
|
|
|
|
else
|
|
|
|
--outside aabb-like bounds so we need to collide with the nearest clamped corner point
|
|
|
|
local clamped = intersect.aabb_point_clamp(a_pos, a_hs, b_pos, vec2:pooled())
|
|
|
|
result = intersect.circle_circle_collide(clamped, 0, b_pos, b_rad, into)
|
|
|
|
clamped:release()
|
2021-05-10 16:29:37 +00:00
|
|
|
end
|
2021-06-08 01:51:18 +00:00
|
|
|
return result
|
2021-05-10 16:29:37 +00:00
|
|
|
end
|
|
|
|
|
2020-04-01 08:28:22 +00:00
|
|
|
--check if a point is in a polygon
|
|
|
|
--point is the point to test
|
|
|
|
--poly is a list of points in order
|
|
|
|
--based on winding number, so re-intersecting areas are counted as solid rather than inverting
|
|
|
|
function intersect.point_in_poly(point, poly)
|
|
|
|
local wn = 0
|
|
|
|
for i, a in ipairs(poly) do
|
|
|
|
local b = poly[i + 1] or poly[1]
|
|
|
|
if a.y <= point.y then
|
2021-07-23 04:15:30 +00:00
|
|
|
if
|
|
|
|
b.y > point.y
|
|
|
|
and vec2.winding_side(a, b, point) > 0
|
|
|
|
then
|
2020-04-01 08:28:22 +00:00
|
|
|
wn = wn + 1
|
|
|
|
end
|
|
|
|
else
|
2021-07-23 04:15:30 +00:00
|
|
|
if
|
|
|
|
b.y <= point.y
|
|
|
|
and vec2.winding_side(a, b, point) < 0
|
|
|
|
then
|
2020-04-01 08:28:22 +00:00
|
|
|
wn = wn - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return wn ~= 0
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:34:47 +00:00
|
|
|
--reversed versions
|
|
|
|
--it's annoying to need to flip the order of operands depending on what
|
|
|
|
--shapes you're working with
|
|
|
|
--so these functions provide the
|
|
|
|
|
|
|
|
--todo: ensure this is all of them
|
|
|
|
|
|
|
|
--(helper for reversing only if there's actually a vector, preserving false)
|
|
|
|
function intersect.reverse_msv(result)
|
|
|
|
if result then
|
|
|
|
result:inverse_inplace()
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.point_circle_overlap(a, b_pos, b_rad)
|
|
|
|
return intersect.circle_point_overlap(b_pos, b_rad, a)
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.point_circle_collide(a, b_pos, b_rad, into)
|
|
|
|
return intersect.reverse_msv(intersect.circle_circle_collide(b_pos, b_rad, a, 0, into))
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.point_aabb_overlap(a, b_pos, b_hs)
|
|
|
|
return intersect.aabb_point_overlap(b_pos, b_hs, a)
|
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.point_aabb_collide(a, b_pos, b_hs, into)
|
2021-10-19 01:04:55 +00:00
|
|
|
return intersect.reverse_msv(intersect.aabb_point_collide(b_pos, b_hs, a, into))
|
2021-07-23 04:34:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.circle_aabb_overlap(a, a_rad, b_pos, b_hs)
|
2022-03-03 07:53:11 +00:00
|
|
|
return intersect.aabb_circle_overlap(b_pos, b_hs, a, a_rad)
|
2021-07-23 04:34:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function intersect.circle_aabb_collide(a, a_rad, b_pos, b_hs, into)
|
2022-03-03 07:53:11 +00:00
|
|
|
return intersect.reverse_msv(intersect.aabb_circle_collide(b_pos, b_hs, a, a_rad, into))
|
2021-07-23 04:34:47 +00:00
|
|
|
end
|
|
|
|
|
2021-10-19 01:04:55 +00:00
|
|
|
function intersect.circle_line_collide(a, a_rad, b_start, b_end, b_rad, into)
|
2021-07-23 04:34:47 +00:00
|
|
|
return intersect.reverse_msv(intersect.line_circle_collide(b_start, b_end, b_rad, a, a_rad, into))
|
|
|
|
end
|
|
|
|
|
2021-04-13 05:39:36 +00:00
|
|
|
--resolution helpers
|
|
|
|
|
2021-04-14 03:32:14 +00:00
|
|
|
--resolve a collision between two bodies, given a (minimum) separating vector
|
|
|
|
-- from a's frame of reference, like the result of any of the _collide functions
|
|
|
|
--requires the two positions of the bodies, the msv, and a balance factor
|
|
|
|
--balance should be between 1 and 0;
|
|
|
|
-- 1 is only a_pos moving to resolve
|
|
|
|
-- 0 is only b_pos moving to resolve
|
|
|
|
-- 0.5 is balanced between both (default)
|
|
|
|
--note: this wont work as-is for line segments, which have two separate position coordinates
|
2021-07-23 04:15:30 +00:00
|
|
|
-- you will need to understand what is going on and move the both coordinates yourself
|
2021-04-14 03:32:14 +00:00
|
|
|
function intersect.resolve_msv(a_pos, b_pos, msv, balance)
|
|
|
|
balance = balance or 0.5
|
2021-07-23 04:15:30 +00:00
|
|
|
a_pos:fused_multiply_add_inplace(msv, balance)
|
|
|
|
b_pos:fused_multiply_add_inplace(msv, -(1 - balance))
|
2021-04-14 03:32:14 +00:00
|
|
|
end
|
|
|
|
|
2021-05-11 05:46:57 +00:00
|
|
|
-- gets a normalised balance factor from two mass inputs, and treats <=0 or infinite or nil masses as static bodies
|
|
|
|
-- returns false if we're colliding two static bodies, as that's invalid
|
|
|
|
function intersect.balance_from_mass(a_mass, b_mass)
|
|
|
|
--static cases
|
|
|
|
local a_static = a_mass <= 0 or a_mass == math.huge or not a_mass
|
|
|
|
local b_static = b_mass <= 0 or b_mass == math.huge or not a_mass
|
|
|
|
if a_static and b_static then
|
|
|
|
return false --colliding two static bodies
|
|
|
|
elseif a_static then
|
|
|
|
return 0.0
|
|
|
|
elseif b_static then
|
|
|
|
return 1.0
|
|
|
|
end
|
|
|
|
|
|
|
|
--get balance factor
|
|
|
|
local total = a_mass + b_mass
|
|
|
|
return a_mass / total
|
|
|
|
end
|
|
|
|
|
2021-04-13 05:39:36 +00:00
|
|
|
--bounce a velocity off of a normal (modifying velocity)
|
2021-04-14 03:32:14 +00:00
|
|
|
--essentially flips the part of the velocity in the direction of the normal
|
|
|
|
function intersect.bounce_off(velocity, normal, conservation)
|
|
|
|
--(default)
|
|
|
|
conservation = conservation or 1
|
|
|
|
--take a copy, we need it
|
2021-07-23 04:34:47 +00:00
|
|
|
local old_vel = velocity:pooled_copy()
|
2022-01-20 06:37:40 +00:00
|
|
|
--heading into the normal
|
|
|
|
if old_vel:dot(normal) < 0 then
|
|
|
|
--reject on the normal (keep velocity tangential to the normal)
|
|
|
|
velocity:vector_rejection_inplace(normal)
|
|
|
|
--add back the complement of the difference;
|
|
|
|
--basically "flip" the velocity in line with the normal.
|
|
|
|
velocity:fused_multiply_add_inplace(old_vel:vector_sub_inplace(velocity), -conservation)
|
|
|
|
end
|
2021-04-14 03:32:14 +00:00
|
|
|
--clean up
|
|
|
|
old_vel:release()
|
2021-04-13 05:39:36 +00:00
|
|
|
return velocity
|
|
|
|
end
|
|
|
|
|
2021-04-14 03:32:14 +00:00
|
|
|
--mutual bounce; two similar bodies bounce off each other, transferring energy
|
|
|
|
function intersect.mutual_bounce(velocity_a, velocity_b, normal, conservation)
|
|
|
|
--(default)
|
|
|
|
conservation = conservation or 1
|
|
|
|
--take copies, we need them
|
2021-07-23 04:15:30 +00:00
|
|
|
local old_a_vel = velocity_a:pooled_copy()
|
|
|
|
local old_b_vel = velocity_b:pooled_copy()
|
2021-04-14 03:32:14 +00:00
|
|
|
--reject on the normal
|
2021-07-23 04:15:30 +00:00
|
|
|
velocity_a:vector_rejection_inplace(normal)
|
|
|
|
velocity_b:vector_rejection_inplace(normal)
|
2021-04-14 03:32:14 +00:00
|
|
|
--calculate the amount remaining from the old velocity
|
2021-07-23 04:15:30 +00:00
|
|
|
--(transfer pool ownership)
|
|
|
|
local a_remaining = old_a_vel:vector_sub_inplace(velocity_a)
|
|
|
|
local b_remaining = old_b_vel:vector_sub_inplace(velocity_b)
|
2021-04-14 03:32:14 +00:00
|
|
|
--transfer it to the other body
|
2021-07-23 04:15:30 +00:00
|
|
|
velocity_a:fused_multiply_add_inplace(b_remaining, conservation)
|
|
|
|
velocity_b:fused_multiply_add_inplace(a_remaining, conservation)
|
2021-04-14 03:32:14 +00:00
|
|
|
--clean up
|
2021-07-20 06:20:15 +00:00
|
|
|
vec2.release(a_remaining, b_remaining)
|
2021-04-14 03:32:14 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 10:22:22 +00:00
|
|
|
return intersect
|