Implement gravity, move orientation update to own function, fix cart stopping in process_acceleration

This commit is contained in:
teknomunk 2024-03-06 08:07:31 +00:00
parent 8c6b011c91
commit a0463b564a

View file

@ -363,20 +363,28 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
local passenger_attach_position = vector.new(0, -1.75, 0) local passenger_attach_position = vector.new(0, -1.75, 0)
local function distance_to_next_block( dir, pos ) local function update_cart_orientation(self,staticdata)
if dir.x == 1 then local rot = self.object:get_rotation()
return 1 - ( pos.x - math.floor(pos.x) ) local dir = staticdata.dir
elseif dir.x == -1 then if dir.x < 0 then
return pos.x - math.floor(pos.x) rot.y = 0.5 * math.pi
elseif dir.z == 1 then elseif dir.x > 0 then
return 1 - ( pos.z - math.floor(pos.z) ) rot.y = 1.5 * math.pi
elseif dir.z == -1 then elseif dir.z < 0 then
return pos.z - math.floor(pos.z) rot.y = 1 * math.pi
elseif dir.y == 1 then
return 1 - ( pos.y - math.floor(pos.z) )
else else
return pos.y - math.floor(pos.y) rot.y = 0
end end
-- Forward/backwards tilt (pitch)
if dir.y < 0 then
rot.x = -0.25 * math.pi
elseif dir.y > 0 then
rot.x = 0.25 * math.pi
else
rot.x = 0
end
self.object:set_rotation(rot)
end end
local function do_movement_step(self, remaining_distance) local function do_movement_step(self, remaining_distance)
@ -419,20 +427,23 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
local under_pos = pos - vector.new(0,1,0) local under_pos = pos - vector.new(0,1,0)
local under_node_name = minetest.get_node(under_pos).name local under_node_name = minetest.get_node(under_pos).name
local under_node_def = minetest.registered_nodes[under_node_name] local under_node_def = minetest.registered_nodes[under_node_name]
print( "under_node_name="..under_node_name..", hopper="..tostring(under_node_def.groups.hopper)) if DEBUG then print( "under_node_name="..under_node_name..", hopper="..tostring(under_node_def.groups.hopper)) end
if under_node_def and under_node_def.groups.hopper ~= 0 then if under_node_def and under_node_def.groups.hopper ~= 0 then
print( "Attempt pull_from_minecart" ) if DEBUG then print( "Attempt pull_from_minecart" ) end
if mcl_hoppers.pull_from_minecart( self, under_pos, self._inv_size or 0 ) then if mcl_hoppers.pull_from_minecart( self, under_pos, self._inv_size or 0 ) then
staticdata.delay = 1.5 staticdata.delay = 1.5
end end
end end
-- Get the next direction -- Get the next direction
next_dir,last_switch = mcl_minecarts:get_rail_direction(pos, staticdata.dir, nil, nil, staticdata.railtype) local next_dir,_ = mcl_minecarts:get_rail_direction(pos, staticdata.dir, nil, nil, staticdata.railtype)
if DEBUG and next_dir ~= staticdata.dir then
print( "Changing direction from "..tostring(staticdata.dir).." to "..tostring(next_dir))
end
-- Handle end of track -- Handle end of track
if next_dir == staticdata.dir * -1 then if next_dir == staticdata.dir * -1 then
print("Stopping cart at "..tostring(pos)) if DEBUG then print("Stopping cart at "..tostring(pos)) end
staticdata.velocity = 0 staticdata.velocity = 0
distence = remaining_distance distence = remaining_distance
end end
@ -444,16 +455,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
self.object:move_to(pos) self.object:move_to(pos)
-- Update cart orientation -- Update cart orientation
local yaw = 0 update_cart_orientation(self,staticdata)
local dir = staticdata.dir
if dir.x < 0 then
yaw = 0.5
elseif dir.x > 0 then
yaw = 1.5
elseif dir.z < 0 then
yaw = 1
end
self.object:set_yaw( yaw * math.pi )
-- Report distance traveled -- Report distance traveled
return distance return distance
@ -463,33 +465,39 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
local staticdata = self._staticdata local staticdata = self._staticdata
if not staticdata.connected_at then return end if not staticdata.connected_at then return end
local acceleration = 0
local friction = 0.1
if DEBUG and staticdata.velocity > 0 then
print( " acceleration="..tostring(acceleration)..",velocity="..tostring(staticdata.velocity)..
",timestep="..tostring(timestep)..
",dir="..tostring(staticdata.dir))
end
local pos = staticdata.connected_at local pos = staticdata.connected_at
local node_name = minetest.get_node(pos).name local node_name = minetest.get_node(pos).name
local node_def = minetest.registered_nodes[node_name] local node_def = minetest.registered_nodes[node_name]
local max_vel = mcl_minecarts.speed_max local max_vel = mcl_minecarts.speed_max
if self._go_forward then if self._go_forward then
self._acceleration = 2 acceleration = 2
elseif self._brake then elseif self._brake then
self._acceleration = -1.5 acceleration = -1.5
elseif self._punched then elseif self._punched then
self._acceleration = 2 if statcdata.velocity < 1 then
staticdata.velocity = 1
end
acceleration = 2
elseif self._fueltime and self._fueltime > 0 then elseif self._fueltime and self._fueltime > 0 then
self._acceleration = 0.6 acceleration = 0.6
elseif staticdata.velocity >= ( node_def._max_acceleration_velocity or max_vel ) then
acceleration = 0
else else
if staticdata.velocity >= ( node_def._max_acceleration_velocity or max_vel ) then acceleration = node_def._rail_acceleration or -friction
self._acceleration = 0
else
self._acceleration = node_def._rail_acceleration or -0.4
end
end end
if self._acceleration > 0 and (staticdata.velocity or 0) < 0.1 then if math.abs(acceleration) > (friction / 5) then
staticdata.velocity = 0.1 staticdata.velocity = ( staticdata.velocity or 0 ) + acceleration * timestep
end
if math.abs(self._acceleration) > 0.1 then
staticdata.velocity = ( staticdata.velocity or 0 ) + self._acceleration * timestep
if staticdata.velocity > max_vel then if staticdata.velocity > max_vel then
staticdata.velocity = max_vel staticdata.velocity = max_vel
elseif staticdata.velocity < 0.1 then elseif staticdata.velocity < 0.1 then
@ -497,12 +505,47 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
end end
end end
if DEBUG and staticdata.velocity > 0 then -- Factor in gravity after everything else
print( " acceleration="..tostring(self._acceleration)..",velocity="..tostring(staticdata.velocity).. local gravity_accel = 0
",timestep="..tostring(timestep)) local gravity_strength = friction + 0.2
if staticdata.dir.y < 0 then
gravity_accel = gravity_strength
elseif staticdata.dir.y > 0 then
gravity_accel = -gravity_strength
end
if DEBUG and gravity_accel ~= 0 then
print("gravity_accel="..tostring(gravity_accel))
end
if gravity_accel ~= 0 then
staticdata.velocity = (staticdata.velocity or 0) + gravity_accel
if staticdata.velocity < 0 then
if DEBUG then
print("Gravity flipped direction")
end
staticdata.velocity = staticdata.velocity * -1
-- Update direction
local next_dir,_ = mcl_minecarts:get_rail_direction(pos + staticdata.dir, staticdata.dir * -1, nil, nil, staticdata.railtype)
if DEBUG and next_dir ~= staticdata.dir then
print( "Changing direction from "..tostring(staticdata.dir).." to "..tostring(next_dir))
end
staticdata.dir = next_dir
update_cart_orientation(self,staticdata)
end end
end end
-- Force the cart to stop if moving slowly enough
if (staticdata.velocity or 0) < 0.1 then
staticdata.velocity = 0
end
if DEBUG and staticdata.velocity > 0 then
print( " acceleration="..tostring(acceleration)..",velocity="..tostring(staticdata.velocity)..
",timestep="..tostring(timestep)..
",dir="..tostring(staticdata.dir))
end
end
local function do_movement( self, dtime ) local function do_movement( self, dtime )
local staticdata = self._staticdata local staticdata = self._staticdata
@ -523,6 +566,11 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
process_acceleration(self,dtime * max_step_distance / total_distance) process_acceleration(self,dtime * max_step_distance / total_distance)
-- Skip processing stopped railcarts
if not staticdata.velocity or math.abs(staticdata.velocity) < 0.05 then
return
end
while remaining_distance > 0.1 do while remaining_distance > 0.1 do
local step_distance = do_movement_step(self, remaining_distance) local step_distance = do_movement_step(self, remaining_distance)
if step_distance > 0.1 then if step_distance > 0.1 then