local function direction_snap(self)
local dir = self.dir
+ local pitch = 0
+ if dir.y == 1 then pitch = math.pi/4 end
+ if dir.y == -1 then pitch = -math.pi/4 end
local yaw = minetest.dir_to_yaw(dir)
- self.object:set_rotation(vector.new(0,yaw,0))
+ self.object:set_rotation(vector.new(pitch,yaw,0))
end
local function turn_snap(pos,self,dir,dir2)
- if dir.x ~= 0 and dir2.z ~= 0 then
- local inertia = math.abs(self.velocity.x)
- self.velocity = vector.multiply(dir2,inertia)
- self.dir = dir2
- self.axis_lock = "z"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
- elseif dir.z ~= 0 and dir2.x ~= 0 then
- local inertia = math.abs(self.velocity.z)
- self.velocity = vector.multiply(dir2,inertia)
- self.dir = dir2
- self.axis_lock = "x"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
+ if self.axis_lock == "x" then
+ if dir.x ~= 0 and dir2.z ~= 0 then
+ local inertia = math.abs(self.velocity.x)
+ self.velocity = vector.multiply(dir2,inertia)
+ self.dir = dir2
+ self.axis_lock = "z"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
+ end
+ if self.axis_lock == "z" then
+ if dir.z ~= 0 and dir2.x ~= 0 then
+ local inertia = math.abs(self.velocity.z)
+ self.velocity = vector.multiply(dir2,inertia)
+ self.dir = dir2
+ self.axis_lock = "x"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
end
+ return(false)
end
local function climb_snap(pos,self,dir,dir2)
- if dir.x == dir2.x and dir2.y ~= 0 then
- print(dump(dir2))
- print("x snapping")
- local inertia = math.abs(self.velocity.x)
- self.velocity = vector.multiply(dir2,inertia)
- self.dir = dir2
- self.axis_lock = "x"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
- elseif dir.z == dir2.z and dir2.y ~= 0 then
- local inertia = math.abs(self.velocity.z)
- self.velocity = vector.multiply(dir2,inertia)
- self.dir = dir2
- self.axis_lock = "z"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
+ if self.axis_lock == "x" then
+ if dir.x == dir2.x and dir2.y ~= 0 then
+ local inertia = math.abs(self.velocity.x)
+ self.velocity = vector.multiply(dir2,inertia)
+ self.dir = dir2
+ self.axis_lock = "x"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
end
+ if self.axis_lock == "z" then
+ if dir.z == dir2.z and dir2.y ~= 0 then
+ local inertia = math.abs(self.velocity.z)
+ self.velocity = vector.multiply(dir2,inertia)
+ self.dir = dir2
+ self.axis_lock = "z"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
+ end
+ return(false)
end
local function straight_snap(pos,self,dir)
- if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then
- self.velocity = vector.new(self.velocity.x,0,0)
- self.dir = vector.new(dir.x,0,0)
- self.axis_lock = "x"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
- elseif dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then
- self.velocity = vector.new(0,0,self.velocity.z)
- self.dir = vector.new(0,0,dir.z)
- self.axis_lock = "z"
- self.object:set_pos(pos)
- direction_snap(self)
- return(true)
+ if self.axis_lock == "x" then
+ if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then
+ self.velocity = vector.new(self.velocity.x,0,0)
+ self.dir = vector.new(dir.x,0,0)
+ self.axis_lock = "x"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
end
+ if self.axis_lock == "z" then
+ if dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then
+ self.velocity = vector.new(0,0,self.velocity.z)
+ self.dir = vector.new(0,0,dir.z)
+ self.axis_lock = "z"
+ self.object:set_pos(pos)
+ direction_snap(self)
+ return(true)
+ end
+ end
+ return(false)
end
local function rail_brain(self,pos)
local triggered = false
- if dir.x < 0 and pos2.x < pos.x then
+ if dir.x < 0 and pos2.x < pos.x then
triggered = true
elseif dir.x > 0 and pos2.x > pos.x then
triggered = true
if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
if straight_snap(pos,self,dir) then
+ print('beraking')
return
end
--stop slow down become physical, something
else
for _,dir2 in pairs(possible_dirs) do
- if turn_snap(pos,self,dir,dir2) then
+ if climb_snap(pos,self,dir,dir2) then
return
end
- if climb_snap(pos,self,dir,dir2) then
+ end
+
+ for _,dir2 in pairs(possible_dirs) do
+ if turn_snap(pos,self,dir,dir2) then
return
end
end