optimised line line collision a little, could still use some more love :)

This commit is contained in:
Max Cahill 2023-08-10 16:26:17 +10:00
parent fb3d58f417
commit 8c1e51b057

View File

@ -144,6 +144,12 @@ function intersect.line_circle_collide(a_start, a_end, a_rad, b_pos, b_rad, into
end end
--collide 2 line segments --collide 2 line segments
local _line_line_search_tab = {
{vec2(), 1},
{vec2(), 1},
{vec2(), -1},
{vec2(), -1},
}
function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_rad, into) function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_rad, into)
--segment directions from start points --segment directions from start points
local a_dir = a_end local a_dir = a_end
@ -234,30 +240,37 @@ function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_ra
vec2.release(a_dir, b_dir) vec2.release(a_dir, b_dir)
--dumb as a rocks check-corners approach --dumb as a rocks check-corners approach
--todo: pool storage
--todo proper calculus from http://geomalgorithms.com/a07-_distance.html --todo proper calculus from http://geomalgorithms.com/a07-_distance.html
local search_tab = {} local search_tab = _line_line_search_tab
for i = 1, 4 do
search_tab[i][1]:sset(math.huge)
end
--only insert corners from the non-intersected line --only insert corners from the non-intersected line
--since intersected line is potentially the apex --since intersected line is potentially the apex
if intersected ~= "a" then if intersected ~= "a" then
--a endpoints --a endpoints
table.insert(search_tab, {intersect._line_to_point(b_start, b_end, a_start), 1}) intersect._line_to_point(b_start, b_end, a_start, search_tab[1][1])
table.insert(search_tab, {intersect._line_to_point(b_start, b_end, a_end), 1}) intersect._line_to_point(b_start, b_end, a_end, search_tab[2][1])
end end
if intersected ~= "b" then if intersected ~= "b" then
--b endpoints --b endpoints
table.insert(search_tab, {intersect._line_to_point(a_start, a_end, b_start), -1}) intersect._line_to_point(a_start, a_end, b_start, search_tab[3][1])
table.insert(search_tab, {intersect._line_to_point(a_start, a_end, b_end), -1}) intersect._line_to_point(a_start, a_end, b_end, search_tab[4][1])
end end
local best = nil local best = nil
local best_len = nil local best_len = nil
for _, v in ipairs(search_tab) do for _, v in ipairs(search_tab) do
local len = v[1]:length_squared() local delta = v[1]
if not best_len or len < best_len then if delta.x == math.huge then
--skip
else
local len = delta:length_squared()
if len < (best_len or math.huge) then
best = v best = v
end end
end end
end
--fix direction --fix direction
into:set(best[1]) into:set(best[1])