Monday, April 3, 2017

lua - How can I correct an unwanted fisheye effect when drawing a scene with raycasting?


Hello I have a game using raycasting for the TI Nspire in Lua, and when the player faces a wall so that their degree they are facing is perpendicular to the length of the wall, I am having what is described as the fishbowl/fisheye effect.


How can I fix this? I have already looked here, and none of that makes sense to me. Here is my code:


map = {{0, 0, 50, 0},{50, 0, 50, 50},{50, 50, 0, 50},{0, 50, 0, 0}}

facing = 45 --"player rotation"

rays = {}
x, y = 25, 25

function raycast()
for i,v in pairs(rays) do
rays[i] = nil
end

rayf = facing + 30


for i = 1, 320, 1 do --64, 5
x_ray, y_ray = x, y
ray()
table.insert(rays, radius)
rayf = rayf - (60/320)
end
platform.window:invalidate()
end

function ray()

radius = 1
for n = 1, 50, 1 do -- increase of testdot
x_ray = math.floor(x + (radius * math.cos(rayf * 3.141592653/180)))
y_ray = math.floor(y - (radius * math.sin(rayf * 3.141592653/180)))
for k,v in pairs(map) do
if (
math.min(v[1],v[3]) <= x_ray and x_ray <= math.max(v[1],v[3])
) and (
math.min(v[2],v[4]) <= y_ray and y_ray <= math.max(v[2],v[4])
) then

--print("Collision")
return true
end
end
radius = n
end
end

function on.paint(gc)
gc:setColorRGB(0,200,255)

gc:fillRect(0, 0, 320, 108)
gc:setColorRGB(100,100,100)
gc:fillRect(0, 109, 320, 109)
gc:setColorRGB(100,0,100)
for i,v in ipairs(rays) do
gc:drawLine(i, 108.5 - ((100/v * 20)/2), i, 108.5 + ((100/v * 20)/2))
end
end

function on.arrowKey(key)

if key == 'left' then
facing = facing + 3
elseif key == 'right' then
facing = facing - 3
end
if key == 'up' then
y = y - 1
elseif key == 'down' then
y = y + 1
end

raycast()
end

I have also tried it like this:


map = {{0, 0, 50, 0},{50, 0, 50, 50},{50, 50, 0, 50},{0, 50, 0, 0}}

facing = 45 --"player rotation"
rays = {}
x, y = 25, 25


function raycast()
for i,v in pairs(rays) do
rays[i] = nil
end

rayf = facing + 30

for i = 1, 320, 1 do --64, 5
x_ray, y_ray = x, y
ray()

----------------
fisheyeradius = radius / (math.cos(rayf - facing))
table.insert(rays, fisheyeradius)
----------------
rayf = rayf - (60/320)
end
platform.window:invalidate()
end

function ray()

radius = 1
for n = 1, 50, 1 do -- increase of testdot
x_ray = math.floor(x + (radius * math.cos(rayf * 3.141592653/180)))
y_ray = math.floor(y - (radius * math.sin(rayf * 3.141592653/180)))
for k,v in pairs(map) do
if (
math.min(v[1],v[3]) <= x_ray and x_ray <= math.max(v[1],v[3])
) and (
math.min(v[2],v[4]) <= y_ray and y_ray <= math.max(v[2],v[4])
) then

--print("Collision")
return true
end
end
radius = n
end
end

function on.paint(gc)
gc:setColorRGB(0,200,255)

gc:fillRect(0, 0, 320, 106)
gc:setColorRGB(100,100,100)
gc:fillRect(0, 106, 320, 106)
gc:setColorRGB(100,0,100)
for i,v in ipairs(rays) do
gc:drawLine(i, 108.5 - ((100/v * 20)/2), i, 108.5 + ((100/v * 20)/2))
end
end

function on.arrowKey(key)

if key == 'left' then
facing = facing + 3
elseif key == 'right' then
facing = facing - 3
end
if key == 'up' then
y = y - 1
elseif key == 'down' then
y = y + 1
end

raycast()
end

The wall originally looked like this:


wall


But I have made changes around the ---------------, and now the wall looks like this:


img


I this supposed to occur?



Answer



I actually had to solve this some time ago. The easiest fix is to simply divide by cos(angle of ray - viewAngle) and use that as the distance



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...