Code: Select all
local p = player
local mx, my, hit = 0, 0, false
local speed = kshift and 0.1 or 0.05 -- kw, ka, ks, kd, and kshift are keyboard buttons
if kw then mx = mx-speed my = my-speed end
if ks then mx = mx+speed my = my+speed end
if ka then my = my-speed mx = mx+speed end
if kd then my = my+speed mx = mx-speed end
if (kw or ks) and (ka or kd) then mx, my = mx/2, my/2 end -- Diagonal movement isn't faster
p.z, p.x, p.y = trace(p.z,p.z, p.x,p.x+mx, p.y,p.y+my)
function trace(sz,dz, sx,dx, sy,dy)
local dist = math.dist(sx,dx, sy,dy, sz,dz)
if dist <= 0 then return sz, sx, sy end
local ltz, ltx, lty = sz, sx, sy
for i=math.min(1, dist), dist, math.min(1, dist) do
local tz = math.round((sz*(dist-i) + dz*i)/dist)
local tx = math.round((sx*(dist-i) + dx*i)/dist)
local ty = math.round((sy*(dist-i) + dy*i)/dist)
if tiles[tz] then if tiles[tz][tx] then if tiles[tz][tx][ty] then
if dist < 1 then return sz, sx, sy, true end
return ltz, ltx, lty, true
end end end
ltz, ltx, lty = tz, tx, ty
end
return dz, dx, dy
end
1) If you have two tiles which share a corner, the player can, if perfectly aligned, walk between them.
2) You can't "slide" along walls like you can in other games, which makes the walls "feel sticky".
I figure the way to solve #1 is to make the player have a collision radius (whereas it's currently just a point). Is anyone good enough with math to know how I can implement this?