This is my code that I have on my calculator.
Code: map = {
width = 10,
height = 10,
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,0,0,1,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
}
meta = {}
function myindex(t,i)
return t[math.floor(i)]
end
meta.__index = myindex
setmetatable(map,meta)
for i = 1, #map do
setmetatable(map[i],meta)
end
colorRoof = zmg.makeColor(30, 70, 30)
colorSky = zmg.makeColor(30, 70, 200)
colorStripes = zmg.makeColor(70, 56, 90)
colorSide1 = zmg.makeColor(65, 58, 63)
colorSide2 = zmg.makeColor(35, 28, 33)
posx = 2
posy = 3
dirx = 1
diry = 0
planex = 0
planey = 0.66
move = 1
rotation = 0.25
direction = 1
timer = 0
oldtime = 0
screenx = 384
screeny = 216
while true do
zmg.clear()
x=0
for x = 0,screenx do
camx = 2*x/screenx - 1
rayposx = posx
rayposy = posy
raydirx = dirx + planex*camx
raydiry = diry + planey*camx
mapx = rayposx
mapy = rayposy
sidedistx = 0
sidedisty = 0
deltadistx = math.sqrt(1+(raydiry*raydiry)/(raydirx*raydirx))
deltadisty = math.sqrt(1+(raydirx*raydirx)/(raydiry*raydiry))
perp = 0
stepx = 0
stepy = 0
hit = 0
side = 0
if raydirx < 0 then
stepx = -1
sidedistx = (rayposx - mapx)*deltadistx
else stepx = 1
sidedistx = (mapx + 1 - rayposx)*deltadistx end
if raydiry < 0 then
stepy = -1
sidedisty = (rayposy - mapy)*deltadisty
else stepy = 1
sidedisty = (mapy + 1 - rayposy)*deltadisty end
while hit == 0 do
if sidedistx < sidedisty then
sidedistx = sidedistx + deltadistx
mapx = mapx + stepx
side = 0
else
sidedisty = sidedisty + deltadisty
mapy = mapy + stepy
side = 1
end
if map[mapx][mapy] > 0 then hit = 1 end
end
if side == 0 then
perp = math.abs((mapx - rayposx + (1-stepx)/2)/raydirx)
else perp = math.abs((mapy - rayposy + (1-stepy)/2)/raydiry)
end
lineheight = math.abs(screeny/perp)
draw = -lineheight/2 + screeny/2
if draw < 0 then draw = 0 end
drawE = lineheight/2 + screeny/2
if drawE >= screenx then drawE = screenx - 1 end
color = colorSide1
if side == 1 then color = colorSide2 end
bandUp = screeny/2+drawE
bandDown = screeny/2+draw
zmg.drawRectFill(x, draw, 1, drawE, color);
zmg.drawRectFill(x, drawE, 1, 272, colorRoof);
zmg.drawRectFill(x, 0, 1, draw, colorSky);
zmg.drawRectFill(x, bandUp/2, 1, 1,colorStripes)
zmg.drawRectFill(x, bandDown/2, 1, 1,colorStripes)
zmg.drawRectFill(x, screeny/2, 1, 1,colorStripes)
end
local key = zmg.keyMenu()
if key == 28 then
if map[posx + dirx * move][posy] == 0 then posx = posx + dirx * move end
if map[posx][posy + diry * move] == 0 then posy = posy + diry * move end
end
if key == 37 then
if map[posx - dirx * move][posy] == 0 then posx = posx - dirx * move end
if map[posx][posy - diry * move] == 0 then posy = posy - diry * move end
end
if key == 38 then
--both camera direction and camera plane must be rotated
oldDirX = dirx;
dirx = dirx * math.cos(-rotation) - diry * math.sin(-rotation);
diry = oldDirX * math.sin(-rotation) + diry * math.cos(-rotation);
oldPlaneX = planex;
planex = planex * math.cos(-rotation) - planey * math.sin(-rotation);
planey = oldPlaneX * math.sin(-rotation) + planey * math.cos(-rotation);
direction = direction + 1
end
if key == 27 then
--both camera direction and camera plane must be rotated
oldDirX = dirx;
dirx = dirx * math.cos(rotation) - diry * math.sin(rotation);
diry = oldDirX * math.sin(rotation) + diry * math.cos(rotation);
oldPlaneX = planex;
planex = planex * math.cos(rotation) - planey * math.sin(rotation);
planey = oldPlaneX * math.sin(rotation) + planey * math.cos(rotation);
direction = direction - 1
end
zmg.fastCopy()
end
Edit: Here is code for fixing this problem:
Code: map = {
width = 10,
height = 10,
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,0,0,1,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
}
meta = {}
function myindex(t,i)
return t[math.floor(i)]
end
meta.__index = myindex
setmetatable(map,meta)
for i = 1, #map do
setmetatable(map[i],meta)
end
colorRoof = zmg.makeColor(30, 70, 30)
colorSky = zmg.makeColor(30, 70, 200)
colorStripes = zmg.makeColor(70, 56, 90)
colorSide1 = zmg.makeColor(65, 58, 63)
colorSide2 = zmg.makeColor(35, 28, 33)
posx = 2
posy = 3
dirx = 1
diry = 0
planex = 0
planey = 0.66
move = 1
rotation = 0.25
direction = 1
timer = 0
oldtime = 0
screenx = 384
screeny = 216
while true do
zmg.clear()
x=0
for x = 0,screenx do
camx = 2*x/screenx - 1
rayposx = posx
rayposy = posy
raydirx = dirx + planex*camx
raydiry = diry + planey*camx
mapx = rayposx
mapy = rayposy
sidedistx = 0
sidedisty = 0
deltadistx = math.sqrt(1+(raydiry*raydiry)/(raydirx*raydirx))
deltadisty = math.sqrt(1+(raydirx*raydirx)/(raydiry*raydiry))
perp = 0
stepx = 0
stepy = 0
hit = 0
side = 0
if raydirx < 0 then
stepx = -1
sidedistx = (rayposx - mapx)*deltadistx
else stepx = 1
sidedistx = (mapx + 1 - rayposx)*deltadistx end
if raydiry < 0 then
stepy = -1
sidedisty = (rayposy - mapy)*deltadisty
else stepy = 1
sidedisty = (mapy + 1 - rayposy)*deltadisty end
while hit == 0 do
if sidedistx < sidedisty then
sidedistx = sidedistx + deltadistx
mapx = mapx + stepx
side = 0
else
sidedisty = sidedisty + deltadisty
mapy = mapy + stepy
side = 1
end
if map[mapx][mapy] > 0 then hit = 1 end
end
if side == 0 then
perp = math.abs((mapx - rayposx + (1-stepx)/2)/raydirx)
else perp = math.abs((mapy - rayposy + (1-stepy)/2)/raydiry)
end
lineheight = math.abs(screeny/perp)
draw = -lineheight/2 + screeny/2
if draw < 0 then draw = 0 end
drawE = lineheight/2 + screeny/2
if drawE >= screenx then drawE = screenx - 1 end
color = colorSide1
if side == 1 then color = colorSide2 end
bandUp = screeny/2+drawE
bandDown = screeny/2+draw
zmg.drawRectFill(x, draw, 1, drawE, color);
zmg.drawRectFill(x, drawE, 1, 272, colorRoof);
zmg.drawRectFill(x, 0, 1, draw, colorSky);
zmg.drawRectFill(x, bandUp/2, 1, 1,colorStripes)
zmg.drawRectFill(x, bandDown/2, 1, 1,colorStripes)
zmg.drawRectFill(x, screeny/2, 1, 1,colorStripes)
end
zmg.fastCopy()
local key = zmg.keyMenu()
if key == 28 then
if map[posx + dirx * move][posy] == 0 then posx = posx + dirx * move end
if map[posx][posy + diry * move] == 0 then posy = posy + diry * move end
end
if key == 37 then
if map[posx - dirx * move][posy] == 0 then posx = posx - dirx * move end
if map[posx][posy - diry * move] == 0 then posy = posy - diry * move end
end
if key == 38 then
--both camera direction and camera plane must be rotated
oldDirX = dirx;
dirx = dirx * math.cos(-rotation) - diry * math.sin(-rotation);
diry = oldDirX * math.sin(-rotation) + diry * math.cos(-rotation);
oldPlaneX = planex;
planex = planex * math.cos(-rotation) - planey * math.sin(-rotation);
planey = oldPlaneX * math.sin(-rotation) + planey * math.cos(-rotation);
direction = direction + 1
end
if key == 27 then
--both camera direction and camera plane must be rotated
oldDirX = dirx;
dirx = dirx * math.cos(rotation) - diry * math.sin(rotation);
diry = oldDirX * math.sin(rotation) + diry * math.cos(rotation);
oldPlaneX = planex;
planex = planex * math.cos(rotation) - planey * math.sin(rotation);
planey = oldPlaneX * math.sin(rotation) + planey * math.cos(rotation);
direction = direction - 1
end
end