Add vector.ray_parallelogram_intersection

This commit is contained in:
Lars Mueller 2022-01-09 19:31:41 +01:00
parent c87effd9a5
commit bcda0511b1

@ -206,8 +206,7 @@ function box_box_collision(diff, box, other_box)
return true return true
end end
--+ Möller-Trumbore local function moeller_trumbore(origin, direction, triangle, is_tri)
function ray_triangle_intersection(origin, direction, triangle)
local point_1, point_2, point_3 = unpack(triangle) local point_1, point_2, point_3 = unpack(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1) local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
local h = cross3(direction, edge_2) local h = cross3(direction, edge_2)
@ -223,7 +222,7 @@ function ray_triangle_intersection(origin, direction, triangle)
end end
local q = cross3(diff, edge_1) local q = cross3(diff, edge_1)
local v = f * dot(direction, q) local v = f * dot(direction, q)
if v < 0 or u + v > 1 then if v < 0 or (is_tri and u or 0) + v > 1 then
return return
end end
local pos_on_line = f * dot(edge_2, q) local pos_on_line = f * dot(edge_2, q)
@ -232,6 +231,14 @@ function ray_triangle_intersection(origin, direction, triangle)
end end
end end
function ray_triangle_intersection(origin, direction, triangle)
return moeller_trumbore(origin, direction, triangle, true)
end
function ray_parallelogram_intersection(origin, direction, parallelogram)
return moeller_trumbore(origin, direction, parallelogram)
end
function triangle_normal(triangle) function triangle_normal(triangle)
local point_1, point_2, point_3 = unpack(triangle) local point_1, point_2, point_3 = unpack(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1) local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)