mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-19 21:04:13 +02:00
Use vector utils in MT utils, fix load order
This commit is contained in:
parent
8ad7c3de7c
commit
3df17ae43f
45
init.lua
45
init.lua
@ -52,33 +52,31 @@ local function loadfile_exports(filename)
|
|||||||
return env
|
return env
|
||||||
end
|
end
|
||||||
|
|
||||||
local components = {
|
|
||||||
mod = {},
|
|
||||||
conf = {},
|
|
||||||
schema = {},
|
|
||||||
data = {},
|
|
||||||
file = {},
|
|
||||||
func = {},
|
|
||||||
log = {},
|
|
||||||
minetest = {},
|
|
||||||
math = {"number"},
|
|
||||||
player = {},
|
|
||||||
table = {},
|
|
||||||
text = {"string"},
|
|
||||||
vector = {},
|
|
||||||
trie = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
modlib = {}
|
modlib = {}
|
||||||
|
|
||||||
for component, aliases in pairs(components) do
|
for _, component in ipairs{
|
||||||
local comp = loadfile_exports(get_resource(component .. ".lua"))
|
"mod",
|
||||||
modlib[component] = comp
|
"conf",
|
||||||
for _, alias in pairs(aliases) do
|
"schema",
|
||||||
modlib[alias] = comp
|
"data",
|
||||||
end
|
"file",
|
||||||
|
"func",
|
||||||
|
"log",
|
||||||
|
"math",
|
||||||
|
"player",
|
||||||
|
"table",
|
||||||
|
"text",
|
||||||
|
"vector",
|
||||||
|
"minetest",
|
||||||
|
"trie"
|
||||||
|
} do
|
||||||
|
modlib[component] = loadfile_exports(get_resource(component .. ".lua"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Aliases
|
||||||
|
modlib.string = modlib.text
|
||||||
|
modlib.number = modlib.math
|
||||||
|
|
||||||
modlib.conf.build_setting_tree()
|
modlib.conf.build_setting_tree()
|
||||||
|
|
||||||
modlib.mod.get_resource = get_resource
|
modlib.mod.get_resource = get_resource
|
||||||
@ -86,6 +84,7 @@ modlib.mod.loadfile_exports = loadfile_exports
|
|||||||
|
|
||||||
_ml = modlib
|
_ml = modlib
|
||||||
|
|
||||||
|
modlib.mod.include("test.lua")
|
||||||
--[[
|
--[[
|
||||||
--modlib.mod.include("test.lua")
|
--modlib.mod.include("test.lua")
|
||||||
]]
|
]]
|
121
minetest.lua
121
minetest.lua
@ -148,7 +148,7 @@ end
|
|||||||
liquid_level_max = 8
|
liquid_level_max = 8
|
||||||
--+ Calculates the flow direction of a flowingliquid node
|
--+ Calculates the flow direction of a flowingliquid node
|
||||||
--# as returned by `minetest.get_node`
|
--# as returned by `minetest.get_node`
|
||||||
--> 4 corner levels from -0.5 to 0.5 as list
|
--> 4 corner levels from -0.5 to 0.5 as list of `modlib.vector`
|
||||||
function get_liquid_corner_levels(pos)
|
function get_liquid_corner_levels(pos)
|
||||||
local node = minetest.get_node(pos)
|
local node = minetest.get_node(pos)
|
||||||
local def = minetest.registered_nodes[node.name]
|
local def = minetest.registered_nodes[node.name]
|
||||||
@ -207,56 +207,56 @@ function get_liquid_corner_levels(pos)
|
|||||||
return levels / neighbor_count
|
return levels / neighbor_count
|
||||||
end
|
end
|
||||||
local corner_levels = {
|
local corner_levels = {
|
||||||
{x = 0, z = 0},
|
{0, nil, 0},
|
||||||
{x = 1, z = 0},
|
{1, nil, 0},
|
||||||
{x = 1, z = 1},
|
{1, nil, 1},
|
||||||
{x = 0, z = 1}
|
{0, nil, 1}
|
||||||
}
|
}
|
||||||
for _, corner_level in pairs(corner_levels) do
|
for index, corner_level in pairs(corner_levels) do
|
||||||
corner_level.y = get_corner_level(corner_level.x, corner_level.z) - 0.5
|
corner_level[2] = get_corner_level(corner_level[1], corner_level[3])
|
||||||
corner_level.x, corner_level.z = corner_level.x - 0.5, corner_level.z - 0.5
|
corner_levels[index] = modlib.vector.subtract_scalar(modlib.vector.new(corner_level), 0.5)
|
||||||
end
|
end
|
||||||
return corner_levels
|
return corner_levels
|
||||||
end
|
end
|
||||||
|
|
||||||
flowing_downwards = vector.new(0, -1, 0)
|
flowing_downwards = modlib.vector.new{0, -1, 0}
|
||||||
--+ Calculates the flow direction of a flowingliquid node
|
--+ Calculates the flow direction of a flowingliquid node
|
||||||
--# as returned by `minetest.get_node`
|
--# as returned by `minetest.get_node`
|
||||||
--> `modlib.minetest.flowing_downwards = vector.new(0, -1, 0)` if only flowing downwards
|
--> `modlib.minetest.flowing_downwards = modlib.vector.new{0, -1, 0}` if only flowing downwards
|
||||||
--> surface direction as `vector` else
|
--> surface direction as `modlib.vector` else
|
||||||
function get_liquid_flow_direction(pos)
|
function get_liquid_flow_direction(pos)
|
||||||
local corner_levels = get_liquid_corner_levels(pos)
|
local corner_levels = get_liquid_corner_levels(pos)
|
||||||
local max_level = corner_levels[1].y
|
local max_level = corner_levels[1][2]
|
||||||
for index = 2, 4 do
|
for index = 2, 4 do
|
||||||
local level = corner_levels[index].y
|
local level = corner_levels[index][2]
|
||||||
if level > max_level then
|
if level > max_level then
|
||||||
max_level = level
|
max_level = level
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local dir = vector.new(0, 0, 0)
|
local dir = modlib.vector.new{0, 0, 0}
|
||||||
local count = 0
|
local count = 0
|
||||||
for max_level_index, corner_level in pairs(corner_levels) do
|
for max_level_index, corner_level in pairs(corner_levels) do
|
||||||
if corner_level.y == max_level then
|
if corner_level[2] == max_level then
|
||||||
for offset = 1, 3 do
|
for offset = 1, 3 do
|
||||||
local index = (max_level_index + offset - 1) % 4 + 1
|
local index = (max_level_index + offset - 1) % 4 + 1
|
||||||
local diff = vector.subtract(corner_level, corner_levels[index])
|
local diff = corner_level - corner_levels[index]
|
||||||
if diff.y ~= 0 then
|
if diff[2] ~= 0 then
|
||||||
diff.x = diff.x * diff.y
|
diff[1] = diff[1] * diff[2]
|
||||||
diff.z = diff.z * diff.y
|
diff[3] = diff[3] * diff[2]
|
||||||
if offset == 3 then
|
if offset == 3 then
|
||||||
diff = vector.divide(diff, math.sqrt(2))
|
diff = modlib.vector.divide_scalar(diff, math.sqrt(2))
|
||||||
end
|
end
|
||||||
dir = vector.add(dir, diff)
|
dir = dir + diff
|
||||||
count = count + 1
|
count = count + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if count ~= 0 then
|
if count ~= 0 then
|
||||||
dir = vector.divide(dir, count)
|
dir = modlib.vector.divide_scalar(dir, count)
|
||||||
end
|
end
|
||||||
if vector.equals(dir, vector.new(0, 0, 0)) then
|
if dir == modlib.vector.new{0, 0, 0} then
|
||||||
if node.param2 % 32 > 7 then
|
if minetest.get_node(pos).param2 % 32 > 7 then
|
||||||
return flowing_downwards
|
return flowing_downwards
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -264,26 +264,29 @@ function get_liquid_flow_direction(pos)
|
|||||||
end
|
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)
|
||||||
if not liquids then
|
if not liquids then
|
||||||
return raycast
|
return raycast
|
||||||
end
|
end
|
||||||
local direction = vector.direction(pos1, pos2)
|
local pos1 = modlib.vector.from_minetest(_pos1)
|
||||||
local length = vector.distance(pos1, pos2)
|
local _direction = vector.direction(_pos1, _pos2)
|
||||||
|
local direction = modlib.vector.from_minetest(_direction)
|
||||||
|
local length = vector.distance(_pos1, _pos2)
|
||||||
local function next()
|
local function next()
|
||||||
for pointed_thing in raycast do
|
for pointed_thing in raycast do
|
||||||
if pointed_thing.type ~= "node" then
|
if pointed_thing.type ~= "node" then
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
local pos = pointed_thing.under
|
local _pos = pointed_thing.under
|
||||||
local node = minetest.get_node(pos)
|
local pos = modlib.vector.from_minetest(_pos)
|
||||||
|
local node = minetest.get_node(_pos)
|
||||||
local def = minetest.registered_nodes[node.name]
|
local def = minetest.registered_nodes[node.name]
|
||||||
if not (def and def.drawtype == "flowingliquid") then return pointed_thing end
|
if not (def and def.drawtype == "flowingliquid") then return pointed_thing end
|
||||||
local corner_levels = get_liquid_corner_levels(pos)
|
local corner_levels = get_liquid_corner_levels(_pos)
|
||||||
local full_corner_levels = true
|
local full_corner_levels = true
|
||||||
for _, corner_level in pairs(corner_levels) do
|
for _, corner_level in pairs(corner_levels) do
|
||||||
if corner_level.y < 0.5 then
|
if corner_level[2] < 0.5 then
|
||||||
full_corner_levels = false
|
full_corner_levels = false
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -291,8 +294,7 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
if full_corner_levels then
|
if full_corner_levels then
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
-- origin = pos
|
local relative = pos1 - pos
|
||||||
local relative = vector.subtract(pos1, pos)
|
|
||||||
local inside = true
|
local inside = true
|
||||||
for _, prop in pairs(relative) do
|
for _, prop in pairs(relative) do
|
||||||
if prop <= -0.5 or prop >= 0.5 then
|
if prop <= -0.5 or prop >= 0.5 then
|
||||||
@ -302,7 +304,7 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
end
|
end
|
||||||
local function level(x, z)
|
local function level(x, z)
|
||||||
local function distance_squared(corner)
|
local function distance_squared(corner)
|
||||||
return (x - corner.x) ^ 2 + (z - corner.z) ^ 2
|
return (x - corner[1]) ^ 2 + (z - corner[3]) ^ 2
|
||||||
end
|
end
|
||||||
local irrelevant_corner, distance = 1, distance_squared(corner_levels[1])
|
local irrelevant_corner, distance = 1, distance_squared(corner_levels[1])
|
||||||
for index = 2, 4 do
|
for index = 2, 4 do
|
||||||
@ -315,19 +317,20 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
return corner_levels[((irrelevant_corner + off) % 4) + 1]
|
return corner_levels[((irrelevant_corner + off) % 4) + 1]
|
||||||
end
|
end
|
||||||
local base = corner(2)
|
local base = corner(2)
|
||||||
local edge_1, edge_2 = vector.subtract(corner(1), base), vector.subtract(corner(3), base)
|
local edge_1, edge_2 = corner(1) - base, corner(3) - base
|
||||||
assert(math.abs(edge_1.x + edge_1.z) + math.abs(edge_2.x + edge_2.z) == 2)
|
-- Properly selected edges will have a total length of 2
|
||||||
if edge_1.x == 0 then
|
assert(math.abs(edge_1[1] + edge_1[3]) + math.abs(edge_2[1] + edge_2[3]) == 2)
|
||||||
|
if edge_1[1] == 0 then
|
||||||
edge_1, edge_2 = edge_2, edge_1
|
edge_1, edge_2 = edge_2, edge_1
|
||||||
end
|
end
|
||||||
local level = base.y + (edge_1.y * ((x - base.x) / edge_1.x)) + (edge_2.y * ((z - base.z) / edge_2.z))
|
local level = base[2] + (edge_1[2] * ((x - base[1]) / edge_1[1])) + (edge_2[2] * ((z - base[3]) / edge_2[3]))
|
||||||
assert(level >= -0.5 and level <= 0.5)
|
assert(level >= -0.5 and level <= 0.5)
|
||||||
return level
|
return level
|
||||||
end
|
end
|
||||||
inside = inside and (relative.y < level(relative.x, relative.z))
|
inside = inside and (relative[2] < level(relative[1], relative[3]))
|
||||||
if inside then
|
if inside then
|
||||||
-- pos1 is inside the liquid node
|
-- pos1 is inside the liquid node
|
||||||
pointed_thing.intersection_point = pos1
|
pointed_thing.intersection_point = _pos1
|
||||||
pointed_thing.intersection_normal = vector.new(0, 0, 0)
|
pointed_thing.intersection_normal = vector.new(0, 0, 0)
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
@ -338,25 +341,27 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
local offset = dir * 0.5
|
local offset = dir * 0.5
|
||||||
local diff_axis = (relative[axis] - offset) / -direction[axis]
|
local diff_axis = (relative[axis] - offset) / -direction[axis]
|
||||||
local intersection_point = {}
|
local intersection_point = {}
|
||||||
for plane_axis in pairs{x = true, y = true, z = true, [axis] = nil} do
|
for plane_axis = 1, 3 do
|
||||||
local value = direction[plane_axis] * diff_axis + relative[plane_axis]
|
if plane_axis ~= axis then
|
||||||
if value < -0.5 or value > 0.5 then
|
local value = direction[plane_axis] * diff_axis + relative[plane_axis]
|
||||||
return
|
if value < -0.5 or value > 0.5 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
intersection_point[plane_axis] = value
|
||||||
end
|
end
|
||||||
intersection_point[plane_axis] = value
|
|
||||||
end
|
end
|
||||||
intersection_point[axis] = offset
|
intersection_point[axis] = offset
|
||||||
return intersection_point
|
return intersection_point
|
||||||
end
|
end
|
||||||
if direction.y > 0 then
|
if direction[2] > 0 then
|
||||||
local intersection_point = plane("y", -1)
|
local intersection_point = plane(2, -1)
|
||||||
if intersection_point then
|
if intersection_point then
|
||||||
pointed_thing.intersection_point = vector.add(intersection_point, pos)
|
pointed_thing.intersection_point = (intersection_point + pos):to_minetest()
|
||||||
pointed_thing.intersection_normal = intersection_normal("y", -1)
|
pointed_thing.intersection_normal = intersection_normal("y", -1)
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for coord, other in pairs{x = "z", z = "x"} do
|
for coord, other in pairs{[1] = 3, [3] = 1} do
|
||||||
if direction[coord] ~= 0 then
|
if direction[coord] ~= 0 then
|
||||||
local dir = direction[coord] > 0 and -1 or 1
|
local dir = direction[coord] > 0 and -1 or 1
|
||||||
local intersection_point = plane(coord, dir)
|
local intersection_point = plane(coord, dir)
|
||||||
@ -364,25 +369,25 @@ function raycast(pos1, pos2, objects, liquids)
|
|||||||
local height = 0
|
local height = 0
|
||||||
for _, corner in pairs(corner_levels) do
|
for _, corner in pairs(corner_levels) do
|
||||||
if corner[coord] == dir * 0.5 then
|
if corner[coord] == dir * 0.5 then
|
||||||
height = height + (math.abs(intersection_point[other] + corner[other])) * corner.y
|
height = height + (math.abs(intersection_point[other] + corner[other])) * corner[2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if intersection_point.y <= height then
|
if intersection_point[2] <= height then
|
||||||
pointed_thing.intersection_point = vector.add(intersection_point, pos)
|
pointed_thing.intersection_point = (intersection_point + pos):to_minetest()
|
||||||
pointed_thing.intersection_normal = intersection_normal(coord, dir)
|
pointed_thing.intersection_normal = intersection_normal(modlib.vector.index_aliases[coord], dir)
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for _, triangle in pairs{
|
for _, triangle in pairs{
|
||||||
{corner_levels[1], corner_levels[2], corner_levels[3]},
|
{corner_levels[3], corner_levels[2], corner_levels[1]},
|
||||||
{corner_levels[1], corner_levels[3], corner_levels[4]}
|
{corner_levels[4], corner_levels[3], corner_levels[1]}
|
||||||
} do
|
} do
|
||||||
local pos_on_ray = modlib.vector.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 = (pos1 + modlib.vector.multiply_scalar(direction, pos_on_ray)):to_minetest()
|
||||||
pointed_thing.intersection_normal = vector.multiply(modlib.vector.triangle_normal(triangle), -1)
|
pointed_thing.intersection_normal = modlib.vector.triangle_normal(triangle):to_minetest()
|
||||||
return pointed_thing
|
return pointed_thing
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
7
test.lua
7
test.lua
@ -27,16 +27,15 @@ if tests.liquid_dir then
|
|||||||
local direction = modlib.minetest.get_liquid_flow_direction(pos, node)
|
local direction = modlib.minetest.get_liquid_flow_direction(pos, node)
|
||||||
local start_pos = pos
|
local start_pos = pos
|
||||||
start_pos.y = start_pos.y + 1
|
start_pos.y = start_pos.y + 1
|
||||||
for i = 0, 20 do
|
for i = 0, 5 do
|
||||||
minetest.add_particle{
|
minetest.add_particle{
|
||||||
pos = vector.add(start_pos, vector.multiply(direction, i/20)),
|
pos = vector.add(start_pos, vector.multiply(direction, i/5)),
|
||||||
size = i/10,
|
size = i/2.5,
|
||||||
texture = "logo.png"
|
texture = "logo.png"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
end
|
end
|
||||||
if tests.liquid_raycast then
|
if tests.liquid_raycast then
|
||||||
minetest.register_globalstep(function()
|
minetest.register_globalstep(function()
|
||||||
|
99
vector.lua
99
vector.lua
@ -1,8 +1,33 @@
|
|||||||
local mt_vector = vector
|
local mt_vector = vector
|
||||||
local vector = getfenv(1)
|
local vector = getfenv(1)
|
||||||
|
|
||||||
|
index_aliases = {
|
||||||
|
x = 1,
|
||||||
|
y = 2,
|
||||||
|
z = 3,
|
||||||
|
w = 4
|
||||||
|
}
|
||||||
|
|
||||||
|
modlib.table.add_all(index_aliases, modlib.table.flip(index_aliases))
|
||||||
|
|
||||||
|
metatable = {
|
||||||
|
__index = function(table, key)
|
||||||
|
local index = index_aliases[key]
|
||||||
|
if index ~= nil then
|
||||||
|
return table[index]
|
||||||
|
end
|
||||||
|
return vector[key]
|
||||||
|
end,
|
||||||
|
__newindex = function(table, key, value)
|
||||||
|
local index = letters[key]
|
||||||
|
if index ~= nil then
|
||||||
|
return rawset(table, index, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
function new(v)
|
function new(v)
|
||||||
return setmetatable(v, vector)
|
return setmetatable(v, metatable)
|
||||||
end
|
end
|
||||||
|
|
||||||
function from_xyzw(v)
|
function from_xyzw(v)
|
||||||
@ -21,6 +46,33 @@ function to_minetest(v)
|
|||||||
return mt_vector.new(unpack(v))
|
return mt_vector.new(unpack(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function equals(v, other_v)
|
||||||
|
for k, v in pairs(v) do
|
||||||
|
if v ~= other_v[k] then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
metatable.__eq = equals
|
||||||
|
|
||||||
|
function less_than(v, other_v)
|
||||||
|
for k, v in pairs(v) do
|
||||||
|
if v >= other_v[k] then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
metatable.__lt = less_than
|
||||||
|
|
||||||
|
function less_or_equal(v, other_v)
|
||||||
|
for k, v in pairs(v) do
|
||||||
|
if v > other_v[k] then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
metatable.__le = less_or_equal
|
||||||
|
|
||||||
function combine(v1, v2, f)
|
function combine(v1, v2, f)
|
||||||
local new_vector = {}
|
local new_vector = {}
|
||||||
for key, value in pairs(v1) do
|
for key, value in pairs(v1) do
|
||||||
@ -50,6 +102,11 @@ subtract, subtract_scalar = combinator(function(a, b) return a - b end)
|
|||||||
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
||||||
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
||||||
|
|
||||||
|
metatable.__add = add
|
||||||
|
metatable.__sub = subtract
|
||||||
|
metatable.__mul = multiply
|
||||||
|
metatable.__div = divide
|
||||||
|
|
||||||
function norm(v)
|
function norm(v)
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for _, c in pairs(v) do
|
for _, c in pairs(v) do
|
||||||
@ -78,6 +135,22 @@ function clamp(v, min, max)
|
|||||||
return apply(apply(v, math.max, min), math.min, max)
|
return apply(apply(v, math.max, min), math.min, max)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function cross3(v, other_v)
|
||||||
|
return new{
|
||||||
|
v[2] * other_v[3] - v[3] * other_v[2],
|
||||||
|
v[3] * other_v[1] - v[1] * other_v[3],
|
||||||
|
v[1] * other_v[2] - v[2] * other_v[1]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function dot(v, other_v)
|
||||||
|
local sum = 0
|
||||||
|
for i, c in pairs(v) do
|
||||||
|
sum = sum + c * other_v[i]
|
||||||
|
end
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
function box_box_collision(diff, box, other_box)
|
function box_box_collision(diff, box, other_box)
|
||||||
for index, diff in pairs(diff) do
|
for index, diff in pairs(diff) do
|
||||||
if box[index] + diff > other_box[index + 3] or other_box[index] > box[index + 3] + diff then
|
if box[index] + diff > other_box[index + 3] or other_box[index] > box[index + 3] + diff then
|
||||||
@ -90,24 +163,24 @@ end
|
|||||||
--+ Möller-Trumbore
|
--+ Möller-Trumbore
|
||||||
function ray_triangle_intersection(origin, direction, triangle)
|
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 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
|
||||||
local h = vector.cross(direction, edge_2)
|
local h = cross3(direction, edge_2)
|
||||||
local a = vector.dot(edge_1, h)
|
local a = dot(edge_1, h)
|
||||||
if math.abs(a) < 1e-9 then
|
if math.abs(a) < 1e-9 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local f = 1 / a
|
local f = 1 / a
|
||||||
local diff = vector.subtract(origin, point_1)
|
local diff = subtract(origin, point_1)
|
||||||
local u = f * vector.dot(diff, h)
|
local u = f * dot(diff, h)
|
||||||
if u < 0 or u > 1 then
|
if u < 0 or u > 1 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local q = vector.cross(diff, edge_1)
|
local q = cross3(diff, edge_1)
|
||||||
local v = f * vector.dot(direction, q)
|
local v = f * dot(direction, q)
|
||||||
if v < 0 or u + v > 1 then
|
if v < 0 or u + v > 1 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local pos_on_line = f * vector.dot(edge_2, q);
|
local pos_on_line = f * dot(edge_2, q)
|
||||||
if pos_on_line >= 0 then
|
if pos_on_line >= 0 then
|
||||||
return pos_on_line
|
return pos_on_line
|
||||||
end
|
end
|
||||||
@ -115,10 +188,6 @@ 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 = vector.subtract(point_2, point_1), vector.subtract(point_3, point_1)
|
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
|
||||||
return vector.normalize{
|
return normalize(cross3(edge_1, edge_2))
|
||||||
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
|
end
|
Loading…
Reference in New Issue
Block a user