mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2025-03-11 06:07:44 +01:00
70 lines
2 KiB
Lua
70 lines
2 KiB
Lua
local mod = vl_physics
|
|
|
|
local registered_environment_effects = {}
|
|
|
|
function mod.register_environment_effect(effect)
|
|
local list = registered_environment_effects
|
|
list[#list + 1] = effect
|
|
end
|
|
|
|
-- Get the physics effects that affect an entity
|
|
---@param pos vector.Vector Current position of the entity
|
|
---@param collision_box table The collision box of the entity used to determine if the entity
|
|
-- overlaps nodes
|
|
---@param vel Number The entity's previous velocity
|
|
---@param staticdata table
|
|
---@param mass Number
|
|
---@param entity core.LuaEntity
|
|
function mod.get_environment_effect(pos, collision_box, vel, staticdata, mass, entity)
|
|
local v = vector.zero()
|
|
local a = vector.zero()
|
|
|
|
-- Accumulate all enviornmental effects
|
|
for _,effect in ipairs(registered_environment_effects) do
|
|
local dv,da = effect(pos, collisionbox, vel, staticdata, entity)
|
|
if dv then
|
|
v = v + dv
|
|
end
|
|
if da then
|
|
a = a + da
|
|
end
|
|
end
|
|
|
|
-- Disable small effects
|
|
if vector.length(v) < 0.01 then v = nil end
|
|
if vector.length(a) < 0.01 then a = nil end
|
|
|
|
return v,a
|
|
end
|
|
|
|
local DEFAULT_ENTITY_PHYSICS = {
|
|
mass = 1,
|
|
}
|
|
function mod.apply_entity_environmental_physics(self, data)
|
|
data = data or {}
|
|
|
|
local physics = self._vl_physics or DEFAULT_ENTITY_PHYSICS
|
|
local mass = physics.mass or DEFAULT_ENTITY_PHYSICS.mass
|
|
|
|
local pos = self.object:get_pos()
|
|
local vel = self.object:get_velocity()
|
|
local new_velocity,new_acceleration = vl_physics.get_environment_effect(pos, nil, vel, data, mass, self)
|
|
|
|
--if new_velocity then print("new_velocity="..tostring(new_velocity)) end
|
|
--if new_acceleration then print("new_acceleration="..tostring(new_acceleration)) end
|
|
|
|
-- Update entity states
|
|
self._flowing = data.flowing
|
|
|
|
-- Apply environmental effects if there are any
|
|
if new_velocity or new_acceleration then
|
|
if new_acceleration then self.object:set_acceleration(new_acceleration) end
|
|
if new_velocity then self.object:set_velocity(new_velocity) end
|
|
|
|
self.physical_state = true
|
|
self._flowing = true
|
|
self.object:set_properties({
|
|
physical = true
|
|
})
|
|
end
|
|
end
|