vebveb wrote:
oh yes, you are right.
However a issue may remain if Spenceboy98 didn't consider that the index begin at 1 and not 0.
Well, he's porting a raycaster that someone else wrote for the Nspire, so hopefully it uses the same 1-indexing for its Lua. Smile In fact, I might just try our fixes on my own LuaZM/Prizm and see how if they work.
KermMartian wrote:
vebveb wrote:
oh yes, you are right.
However a issue may remain if Spenceboy98 didn't consider that the index begin at 1 and not 0.
Well, he's porting a raycaster that someone else wrote for the Nspire, so hopefully it uses the same 1-indexing for its Lua. Smile In fact, I might just try our fixes on my own LuaZM/Prizm and see how if they work.


Actually Kerm, it wasn't originally on the Nspire. I just found it online while I was looking for things to port.

Were you saying I should add this?:

Code:
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

Or this?:

Code:
if map[math.floor(posx - dirx * move)][math.floor(posy)] == 0 then posx = posx - dirx * move end
Forget all the xposi and yposi stuff, change those back to posx and posy, and add the first, longer chunk of code that vebveb and I worked out directly after you declare map. Sorry, my mistake about the source of the raycaster. Smile
KermMartian wrote:
Forget all the xposi and yposi stuff, change those back to posx and posy, and add the first, longer chunk of code that vebveb and I worked out directly after you declare map. Sorry, my mistake about the source of the raycaster. Smile


Okay, it works, but it is incredibly slow. Do you want me to upload it Kerm?
Spenceboy98 wrote:
KermMartian wrote:
Forget all the xposi and yposi stuff, change those back to posx and posy, and add the first, longer chunk of code that vebveb and I worked out directly after you declare map. Sorry, my mistake about the source of the raycaster. Smile


Okay, it works, but it is incredibly slow. Do you want me to upload it Kerm?
Sure, I'll take a crack at it. I'm sure there are plenty of things that can be done to make it faster. What's currently so slow about it? Try it with the latest LuaZM v0.1.1 if you want to try screenshotting it.
So, you want me to upload, or give code? Or Both?

And how do I get a screenshot(I already have 0.1.1)?
Spenceboy98 wrote:
So, you want me to upload, or give code? Or Both?

And how do I get a screenshot(I already have 0.1.1)?
Just switch to keyMenu instead of keyMenuFast, if you haven't already, and use the ScreenReceiver software. If you don't have it, I can take the screenshot myself.
The keyMenu thing doesn't really make much of a difference. I don't currently have the screen reciever on this computer, so it would be nice if you could do it.
Maybe it is the multiple call of math.floor. KermM: you could optimise it more (with bit operations for example), because without an fpu, the conversion from int to float is slow (and float to int can be slow too if not optimised), and your code use these conversions (if you are actually using the code you gave me)
Also, when you press a button, it doesn't move your character until another key is pressed.
Spenceboy98 wrote:
Also, when you press a button, it doesn't move your character until another key is pressed.
Yeah, I noticed that too and was a bad confused by it. Did you call zmg.fastCopy() after drawing the map, and before the key call?
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
I think I see why this is so slow: it seems to be scanning down the screen in order to draw the parallelogram shapes, if I'm reading the code correctly? Maybe I should add a parallelogram routine. Very Happy
KermMartian wrote:
I think I see why this is so slow: it seems to be scanning down the screen in order to draw the parallelogram shapes, if I'm reading the code correctly? Maybe I should add a parallelogram routine. Very Happy


Maybe you should. Very Happy
KermMartian wrote:
I think I see why this is so slow: it seems to be scanning down the screen in order to draw the parallelogram shapes, if I'm reading the code correctly? Maybe I should add a parallelogram routine. Very Happy


That and a triangle (filled && outline versions) routine as well, that's something super useful for a lot of things. Smile
Well, if I added a triangle outline/filled routine, the parallelogram would be sort of extraneous, I think. Smile So maybe I'll go for that.
By the way, parallelograms is just two trigs...
Eiyeron wrote:
By the way, parallelograms is just two trigs...
Yeah, I meant trapezoid there; my brain mis-fired. Smile
^^ me too
Eiyeron wrote:
^^ me too
Yeah, you're right, a trapezoid is indeed two triangles. I was thinking of it as two triangles and a rectangle, but the rectangle is extraneous.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement