0

I am trying to make a Wolfenstein3D-like game, using pico-8 (that's a 2d engine with many limitations) and the world just bends very weirdly.

Gif of running around

My code: (Warning LUA! Confusing language. starts counting at 1...! )

function ray_cast()
  points = {}
  for i=1,64 do
    points[i] = -1
  end


  for o = -31,32 do
    local angle = player.lvec - o/256
    
    
    for i=0,96 do
      local x,y
      x = i * cos(angle)
      y = i * sin(angle)
    
      if mget((x+player.x)/8,(y+player.y)/8) == 1 then
            
        local tx = i-1 * cos(angle)
        local ty = i-1 * sin(angle)
            
        local dis = sqrt((tx^2+ty^2)) 
            
        points[o+32] = dis*cos(angle-player.lvec)
        break
      end
    end
  end
end

I asked for help in the PICO-8 discord and someone said they could help me, but after a lot of messaging, it still did not result in the solution I wanted.

Edit: New GIF Distortion of things

Nizart
  • 1
  • 1
  • people providing answers on Lua problems usually know Lua very well, so I'm not sure why you put a warning about Lua being a "confusing language" into your post. starting at 1 does not even matter in your code. ;) – Piglet Jan 12 '21 at 14:00
  • @Piglet Well my problem is not a Lua problem but a Raycast problem and lua isn't hard to understand if you know the whole quirks and edges it has. – Nizart Jan 12 '21 at 14:03
  • Replace `i-1` with `(i-1)` – Egor Skriptunoff Jan 13 '21 at 08:30

1 Answers1

0

I don't know the language of Lua, however, I do understand the basics of ray-casting. From what I see, the issue could be a too large FOV size or the angles not being fixed (subtracting 360 degrees/2PI radians when above 360 degrees/2PI radians, or adding 360 degrees/2PI radians when below 0). If you don't "fix" the angles, you may get incorrect ray hit positions or even in some cases, crash the GUI. If you have a large FOV, the world will appear to be warped.

FOV OF 64 DEGREES:

FOV OF 128 DEGREES: enter image description here

  • I am not using degrees, but a number between 0 and 1. I changed my code so that the FOV is the equivalent of 64 degrees. though being a bit better, it still bends, so I'd ask you to clarify a bit on the other option since I didn't quite understand. My Player Look Vector is always between 1 and 0, so I would not get above "360 degrees" in most cases. There, of course, is a small number of cases where this happens so i would try and fix that. Edit: I fixed that too, but it still results in bending, so I might have not got what you said, so it would be nice if yu could clarify it. – Nizart Jan 12 '21 at 16:43
  • Okay so you are not using radians or degrees but rather a value between 0 ad 1? Because ray-casting uses trigonometry, you need to use radians or degrees (for most languages that I know of, the trig functions take radian values as parameters) because radians/degrees represents a circle in which an angle can form on the circumference. You need to change your code to implement them as clamped radians, which range from 0 to 2PI (~6.28), or degrees, which range from 0 to 360. – liam-dev2021 Jan 12 '21 at 16:49
  • Pico-8 does not have many built-in functions and nothing i also think it uses a number between 0 and 1. also I added a new gif to the question since the last one didn't show the biggest distortion problem. – Nizart Jan 12 '21 at 16:55
  • What might help is to rind some way to render the rays being cast in the mini map you created so that you can get visual feedback as to where the rays are hitting and why. You could also have the program toggle between 2D perspective and 3D perspective so that you can easily see how the rays are being translated to the 3D environment. You should also have all of the tiles be different colors or make left/right walls be darker so that you can see the true effect of the distortion or if the distortion is really an optical illusion due it being all the same color. – liam-dev2021 Jan 12 '21 at 18:58