mirror of
https://github.com/minetest/minetest_game.git
synced 2025-01-09 23:07:36 +01:00
Carts: Better wiggle prevention
This commit is contained in:
parent
5870fd48f6
commit
454e0da531
@ -43,16 +43,12 @@ function cart_entity:on_activate(staticdata, dtime_s)
|
|||||||
if data.old_dir then
|
if data.old_dir then
|
||||||
self.old_dir = data.old_dir
|
self.old_dir = data.old_dir
|
||||||
end
|
end
|
||||||
if data.old_vel then
|
|
||||||
self.old_vel = data.old_vel
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function cart_entity:get_staticdata()
|
function cart_entity:get_staticdata()
|
||||||
return minetest.serialize({
|
return minetest.serialize({
|
||||||
railtype = self.railtype,
|
railtype = self.railtype,
|
||||||
old_dir = self.old_dir,
|
old_dir = self.old_dir
|
||||||
old_vel = self.old_vel
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -180,21 +176,11 @@ local function rail_on_step(self, dtime)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local pos = self.object:get_pos()
|
local pos = self.object:get_pos()
|
||||||
|
local cart_dir = carts:velocity_to_dir(vel)
|
||||||
|
local same_dir = vector.equals(cart_dir, self.old_dir)
|
||||||
local update = {}
|
local update = {}
|
||||||
|
|
||||||
-- stop cart if velocity vector flips
|
if self.old_pos and not self.punched and same_dir then
|
||||||
if self.old_vel and self.old_vel.y == 0 and
|
|
||||||
(self.old_vel.x * vel.x < 0 or self.old_vel.z * vel.z < 0) then
|
|
||||||
self.old_vel = {x = 0, y = 0, z = 0}
|
|
||||||
self.old_pos = pos
|
|
||||||
self.object:set_velocity(vector.new())
|
|
||||||
self.object:set_acceleration(vector.new())
|
|
||||||
rail_on_step_event(get_railparams(pos).on_step, self, dtime)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self.old_vel = vector.new(vel)
|
|
||||||
|
|
||||||
if self.old_pos and not self.punched then
|
|
||||||
local flo_pos = vector.round(pos)
|
local flo_pos = vector.round(pos)
|
||||||
local flo_old = vector.round(self.old_pos)
|
local flo_old = vector.round(self.old_pos)
|
||||||
if vector.equals(flo_pos, flo_old) then
|
if vector.equals(flo_pos, flo_old) then
|
||||||
@ -213,7 +199,8 @@ local function rail_on_step(self, dtime)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.old_pos then
|
local stop_wiggle = false
|
||||||
|
if self.old_pos and same_dir then
|
||||||
-- Detection for "skipping" nodes
|
-- Detection for "skipping" nodes
|
||||||
local found_path = carts:pathfinder(
|
local found_path = carts:pathfinder(
|
||||||
pos, self.old_pos, self.old_dir, ctrl, self.old_switch, self.railtype
|
pos, self.old_pos, self.old_dir, ctrl, self.old_switch, self.railtype
|
||||||
@ -224,9 +211,11 @@ local function rail_on_step(self, dtime)
|
|||||||
pos = vector.new(self.old_pos)
|
pos = vector.new(self.old_pos)
|
||||||
update.pos = true
|
update.pos = true
|
||||||
end
|
end
|
||||||
|
elseif self.old_pos and cart_dir.y ~= -1 and not self.punched then
|
||||||
|
-- Stop wiggle
|
||||||
|
stop_wiggle = true
|
||||||
end
|
end
|
||||||
|
|
||||||
local cart_dir = carts:velocity_to_dir(vel)
|
|
||||||
local railparams
|
local railparams
|
||||||
|
|
||||||
-- dir: New moving direction of the cart
|
-- dir: New moving direction of the cart
|
||||||
@ -236,9 +225,16 @@ local function rail_on_step(self, dtime)
|
|||||||
)
|
)
|
||||||
|
|
||||||
local new_acc = {x=0, y=0, z=0}
|
local new_acc = {x=0, y=0, z=0}
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
vel = {x = 0, y = 0, z = 0}
|
vel = {x = 0, y = 0, z = 0}
|
||||||
pos = vector.round(pos)
|
local pos_r = vector.round(pos)
|
||||||
|
if not carts:is_rail(pos_r, self.railtype) then
|
||||||
|
pos = self.old_pos
|
||||||
|
elseif not stop_wiggle then
|
||||||
|
pos = pos_r
|
||||||
|
else
|
||||||
|
pos.y = math.floor(pos.y + 0.5)
|
||||||
|
end
|
||||||
update.pos = true
|
update.pos = true
|
||||||
update.vel = true
|
update.vel = true
|
||||||
else
|
else
|
||||||
@ -296,7 +292,7 @@ local function rail_on_step(self, dtime)
|
|||||||
|
|
||||||
self.object:set_acceleration(new_acc)
|
self.object:set_acceleration(new_acc)
|
||||||
self.old_pos = vector.new(pos)
|
self.old_pos = vector.new(pos)
|
||||||
if not vector.equals(dir, {x=0, y=0, z=0}) then
|
if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then
|
||||||
self.old_dir = vector.new(dir)
|
self.old_dir = vector.new(dir)
|
||||||
end
|
end
|
||||||
self.old_switch = switch_keys
|
self.old_switch = switch_keys
|
||||||
@ -332,7 +328,7 @@ local function rail_on_step(self, dtime)
|
|||||||
elseif self.old_dir.z < 0 then
|
elseif self.old_dir.z < 0 then
|
||||||
yaw = 1
|
yaw = 1
|
||||||
end
|
end
|
||||||
self.object:setyaw(yaw * math.pi)
|
self.object:set_yaw(yaw * math.pi)
|
||||||
|
|
||||||
local anim = {x=0, y=0}
|
local anim = {x=0, y=0}
|
||||||
if dir.y == -1 then
|
if dir.y == -1 then
|
||||||
|
Loading…
Reference in New Issue
Block a user