Okay, I am trying to port this raycaster to the Prizm Lua for LuaZM. Here is my code so far:
Code:
It works until I turn left or right. After that, when I press the forward button, it gives me this:
Code:
Any suggestions?
Code:
--Raycasting script
--Based On James Carmack Method
--Thx to http://student.kuleuven.be/~m0216922/CG/raycasting.html Tutorial
--Thx to Jdourlens (http://jdourlens.fr.nf)
function makecolor(r,g,b)
return bit32.lshift(bit32.rshift(r,3),11)+bit32.lshift(bit32.rshift(g,2),5)+bit32.rshift(b,3)
end
--Define first Map Features in a table
map = {
width = 10,
height = 10,
--mapping decor : 1 = Wall, 0 = No Wall
{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},
}
--Player's Starting Position
posx = 2
posy = 3
--Initial Directior Vector, Plane Setting and Time Settings
dirx = 1
diry = 0
planex = 0
planey = 0.66
move = 1
rotation = 0.1
direction = 1
timer = 0
oldtime = 0
--screen Dimensions
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
a = 65
b = 58
c = 63
colorRoof = makecolor(30, 70, 30)
colorSky = makecolor(30, 70, 200)
colorStripes = makecolor(70, 56, 90)
if side == 1 then
a = a-30
b = b-30
c = c-30
end
color = makecolor(a, b, c)
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
if zmg.keyMenuFast() == 28 then
if timer >= oldtime 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
timer = 0
end
end
if zmg.keyMenuFast() == 37 then
if timer >= oldtime 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
timer = 0
end
end
if zmg.keyMenuFast() == 38 then
if timer >= oldtime 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
timer = 0
end
end
if zmg.keyMenuFast() == 27 then
if timer >= oldtime 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
timer = 0
end
end
zmg.fastCopy()
end
It works until I turn left or right. After that, when I press the forward button, it gives me this:
Code:
RaycastLua.lua:138: attempt to index field '?' (a nil value) stack traceback:
RaycastLua.lua:138: in main chunk
[C]: in function 'run'
stdin:1: in main chunk
[C]: in ?
Any suggestions?