Skip to content

Commit

Permalink
Carts: Better wiggle prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker authored and paramat committed Oct 23, 2017
1 parent 5870fd4 commit 454e0da
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions mods/carts/cart_entity.lua
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 454e0da

Please sign in to comment.