From 454e0da5313971d9eb8e94491ba148e0bb26db82 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 15 Oct 2017 12:05:31 +0200 Subject: [PATCH] Carts: Better wiggle prevention --- mods/carts/cart_entity.lua | 44 +++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/mods/carts/cart_entity.lua b/mods/carts/cart_entity.lua index 4384a039..88fd2be6 100644 --- a/mods/carts/cart_entity.lua +++ b/mods/carts/cart_entity.lua @@ -43,16 +43,12 @@ function cart_entity:on_activate(staticdata, dtime_s) if data.old_dir then self.old_dir = data.old_dir end - if data.old_vel then - self.old_vel = data.old_vel - end end function cart_entity:get_staticdata() return minetest.serialize({ railtype = self.railtype, - old_dir = self.old_dir, - old_vel = self.old_vel + old_dir = self.old_dir }) end @@ -180,21 +176,11 @@ local function rail_on_step(self, dtime) end 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 = {} - -- stop cart if velocity vector flips - 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 + if self.old_pos and not self.punched and same_dir then local flo_pos = vector.round(pos) local flo_old = vector.round(self.old_pos) if vector.equals(flo_pos, flo_old) then @@ -213,7 +199,8 @@ local function rail_on_step(self, dtime) end end - if self.old_pos then + local stop_wiggle = false + if self.old_pos and same_dir then -- Detection for "skipping" nodes local found_path = carts:pathfinder( 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) update.pos = true end + elseif self.old_pos and cart_dir.y ~= -1 and not self.punched then + -- Stop wiggle + stop_wiggle = true end - local cart_dir = carts:velocity_to_dir(vel) local railparams -- 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} - 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} - 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.vel = true else @@ -296,7 +292,7 @@ local function rail_on_step(self, dtime) self.object:set_acceleration(new_acc) 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) end self.old_switch = switch_keys @@ -332,7 +328,7 @@ local function rail_on_step(self, dtime) elseif self.old_dir.z < 0 then yaw = 1 end - self.object:setyaw(yaw * math.pi) + self.object:set_yaw(yaw * math.pi) local anim = {x=0, y=0} if dir.y == -1 then -- 2.25.1