mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-20 05:14:10 +02:00
Move triangle utils from "minetest" to "vector"
This commit is contained in:
parent
7fde587dab
commit
3631b1c4ca
40
minetest.lua
40
minetest.lua
@ -263,42 +263,6 @@ function get_liquid_flow_direction(pos)
|
|||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
|
|
||||||
--+ Möller-Trumbore
|
|
||||||
function ray_triangle_intersection(origin, direction, triangle)
|
|
||||||
local point_1, point_2, point_3 = unpack(triangle)
|
|
||||||
local edge_1, edge_2 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
|
||||||
local h = vector.cross(direction, edge_2)
|
|
||||||
local a = vector.dot(edge_1, h)
|
|
||||||
if math.abs(a) < 1e-9 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local f = 1 / a
|
|
||||||
local diff = vector.subtract(origin, point_1)
|
|
||||||
local u = f * vector.dot(diff, h)
|
|
||||||
if u < 0 or u > 1 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local q = vector.cross(diff, edge_1)
|
|
||||||
local v = f * vector.dot(direction, q)
|
|
||||||
if v < 0 or u + v > 1 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local pos_on_line = f * vector.dot(edge_2, q);
|
|
||||||
if pos_on_line >= 0 then
|
|
||||||
return pos_on_line
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function triangle_normal(triangle)
|
|
||||||
local point_1, point_2, point_3 = unpack(triangle)
|
|
||||||
local edge_1, edge_2 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
|
||||||
return vector.normalize{
|
|
||||||
x = edge_1.y * edge_2.z - edge_1.z * edge_2.y,
|
|
||||||
y = edge_1.z * edge_2.x - edge_1.x * edge_2.z,
|
|
||||||
z = edge_1.x * edge_2.y - edge_1.y * edge_2.x
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
--+ Raycast wrapper with proper flowingliquid intersections
|
--+ Raycast wrapper with proper flowingliquid intersections
|
||||||
function raycast(pos1, pos2, objects, liquids)
|
function raycast(pos1, pos2, objects, liquids)
|
||||||
local raycast = minetest.raycast(pos1, pos2, objects, liquids)
|
local raycast = minetest.raycast(pos1, pos2, objects, liquids)
|
||||||
@ -415,10 +379,10 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
{corner_levels[1], corner_levels[2], corner_levels[3]},
|
{corner_levels[1], corner_levels[2], corner_levels[3]},
|
||||||
{corner_levels[1], corner_levels[3], corner_levels[4]}
|
{corner_levels[1], corner_levels[3], corner_levels[4]}
|
||||||
} do
|
} do
|
||||||
local pos_on_ray = ray_triangle_intersection(relative, direction, triangle)
|
local pos_on_ray = modlib.vector.ray_triangle_intersection(relative, direction, triangle)
|
||||||
if pos_on_ray and pos_on_ray <= length then
|
if pos_on_ray and pos_on_ray <= length then
|
||||||
pointed_thing.intersection_point = vector.add(pos1, vector.multiply(direction, pos_on_ray))
|
pointed_thing.intersection_point = vector.add(pos1, vector.multiply(direction, pos_on_ray))
|
||||||
pointed_thing.intersection_normal = vector.multiply(triangle_normal(triangle), -1)
|
pointed_thing.intersection_normal = vector.multiply(modlib.vector.triangle_normal(triangle), -1)
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
36
vector.lua
36
vector.lua
@ -86,3 +86,39 @@ function box_box_collision(diff, box, other_box)
|
|||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--+ Möller-Trumbore
|
||||||
|
function ray_triangle_intersection(origin, direction, triangle)
|
||||||
|
local point_1, point_2, point_3 = unpack(triangle)
|
||||||
|
local edge_1, edge_2 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
||||||
|
local h = vector.cross(direction, edge_2)
|
||||||
|
local a = vector.dot(edge_1, h)
|
||||||
|
if math.abs(a) < 1e-9 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local f = 1 / a
|
||||||
|
local diff = vector.subtract(origin, point_1)
|
||||||
|
local u = f * vector.dot(diff, h)
|
||||||
|
if u < 0 or u > 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local q = vector.cross(diff, edge_1)
|
||||||
|
local v = f * vector.dot(direction, q)
|
||||||
|
if v < 0 or u + v > 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local pos_on_line = f * vector.dot(edge_2, q);
|
||||||
|
if pos_on_line >= 0 then
|
||||||
|
return pos_on_line
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function triangle_normal(triangle)
|
||||||
|
local point_1, point_2, point_3 = unpack(triangle)
|
||||||
|
local edge_1, edge_2 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
||||||
|
return vector.normalize{
|
||||||
|
x = edge_1.y * edge_2.z - edge_1.z * edge_2.y,
|
||||||
|
y = edge_1.z * edge_2.x - edge_1.x * edge_2.z,
|
||||||
|
z = edge_1.x * edge_2.y - edge_1.y * edge_2.x
|
||||||
|
}
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user