Move fiction constant to top of file, suppress cart flips when direction reverses due to gravity or end of track

This commit is contained in:
teknomunk 2024-03-09 18:45:01 +00:00
parent b51496ad8e
commit f57d202a9d

View file

@ -8,6 +8,7 @@ mcl_minecarts.modpath = minetest.get_modpath(modname)
mcl_minecarts.speed_max = 10 mcl_minecarts.speed_max = 10
mcl_minecarts.check_float_time = 15 mcl_minecarts.check_float_time = 15
local max_step_distance = 0.5 local max_step_distance = 0.5
local friction = 0.1
dofile(mcl_minecarts.modpath.."/functions.lua") dofile(mcl_minecarts.modpath.."/functions.lua")
dofile(mcl_minecarts.modpath.."/rails.lua") dofile(mcl_minecarts.modpath.."/rails.lua")
@ -371,26 +372,41 @@ 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 update_cart_orientation(self,staticdata) local function update_cart_orientation(self,staticdata)
local rot = self.object:get_rotation() -- constants
local _2_pi = math.pi * 2
local pi = math.pi
local dir = staticdata.dir local dir = staticdata.dir
if dir.x < 0 then
rot.y = 0.5 * math.pi -- Calculate an angle from the x,z direction components
elseif dir.x > 0 then local rot_y = math.atan2( dir.x, dir.z ) + ( staticdata.rot_adjust or 0 )
rot.y = 1.5 * math.pi if rot_y < 0 then
elseif dir.z < 0 then rot_y = rot_y + _2_pi
rot.y = 1 * math.pi end
-- Check if the rotation is a 180 flip and don't change if so
local rot = self.object:get_rotation()
local diff = math.abs((rot_y - ( rot.y + pi ) % _2_pi) )
if diff < 0.001 or diff > _2_pi - 0.001 then
-- Update rotation adjust and recalculate the rotation
staticdata.rot_adjust = ( ( staticdata.rot_adjust or 0 ) + pi ) % _2_pi
rot.y = math.atan2( dir.x, dir.z ) + ( staticdata.rot_adjust or 0 )
else else
rot.y = 0 rot.y = rot_y
end end
-- Forward/backwards tilt (pitch) -- Forward/backwards tilt (pitch)
if dir.y < 0 then if dir.y < 0 then
rot.x = -0.25 * math.pi rot.x = -0.25 * pi
elseif dir.y > 0 then elseif dir.y > 0 then
rot.x = 0.25 * math.pi rot.x = 0.25 * pi
else else
rot.x = 0 rot.x = 0
end end
if ( staticdata.rot_adjust or 0 ) < 0.01 then
rot.x = -rot.x
end
self.object:set_rotation(rot) self.object:set_rotation(rot)
end end
@ -473,7 +489,6 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
if not staticdata.connected_at then return end if not staticdata.connected_at then return end
local acceleration = 0 local acceleration = 0
local friction = 0.1
if DEBUG and staticdata.velocity > 0 then if DEBUG and staticdata.velocity > 0 then
print( " acceleration="..tostring(acceleration)..",velocity="..tostring(staticdata.velocity).. print( " acceleration="..tostring(acceleration)..",velocity="..tostring(staticdata.velocity)..
@ -491,9 +506,6 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
elseif self._brake then elseif self._brake then
acceleration = -1.5 acceleration = -1.5
elseif self._punched then elseif self._punched then
if statcdata.velocity < 1 then
staticdata.velocity = 1
end
acceleration = 2 acceleration = 2
elseif self._fueltime and self._fueltime > 0 then elseif self._fueltime and self._fueltime > 0 then
acceleration = 0.6 acceleration = 0.6
@ -567,6 +579,11 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
staticdata.delay = 0 staticdata.delay = 0
end end
local initial_velocity = 1
if self._punched and statcdata.velocity < initial_velocity then
staticdata.velocity = initial_velocity
end
-- Break long movements into fixed-size steps so that -- Break long movements into fixed-size steps so that
-- it is impossible to jump across gaps due to server lag -- it is impossible to jump across gaps due to server lag
-- causing large timesteps -- causing large timesteps