(SOLVED) Raycasting fisheye issue (multiplying by cos(a) doesn't work)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

(SOLVED) Raycasting fisheye issue (multiplying by cos(a) doesn't work)

Post by MaxGamz »

So I have been working on a raycaster and so far it is going really well. I was able to adjust it to my liking and I am satisfied with how it looks. When I came across the fisheye problem I already knew how to solve it, I simply multiply the distance by the cosin of the ray angle in respect to the player angle. However, after preforming the operation, I was still experiencing the fisheye effect. I did and checked all my math and everything seemed to be correct, but for whatever reason, it went from the distortion being unaffected to it being exaggerated. I searched all over online for an answer but after a last ditch effort, I decided to come here for answers.

Here is my raycaster function:

Code: Select all

function rays:rayCasting()
    local rayAngle = Player.rotAngle - 30*math.pi/180
    local width = love.graphics.getWidth() / 60
    for i = 0, 60 do
        --ray path incrementers
        local segx1, segy1 = self.x1, self.y1
        local rayCos, raySin = math.cos(rayAngle), math.sin(rayAngle)
        local wall = 0
        while (wall == 0) do
            segx1 = segx1 + rayCos
            segy1 = segy1 + raySin
            wall = tileMap[math.floor((segy1 + 100)/100)][math.floor((segx1 + 100)/100)]
        end

        love.graphics.line(self.x1, self.y1, segx1, segy1)
        local distance = math.sqrt((self.x1 - segx1) ^ 2 + (self.y2 - segy1) ^ 2)
        --Angle trubles :/
        local newAng = rayAngle - Player.rotAngle
        if (newAng <= -2*math.pi) or (newAng >= 2*math.pi) then
            newAng = 0
        end
        distance = distance * (math.cos(newAng))
        --Shows rays being casted
        love.graphics.line(self.x1, self.y1, distance*math.cos(newAng) + self.x1, distance*math.cos(newAng) + self.y1)
        local wallHeight = math.floor(8000 / distance)
        love.graphics.rectangle('line', i * width, love.graphics.getHeight() / 2 - wallHeight / 2, width, wallHeight)

        rayAngle = rayAngle + math.pi/180
    end
end
here is the program if you guys would like to play around with it, it is just a prototype
edit: I used Y2 instead of Y1, sorry it took me so long to figure out :?
Attachments
test.love
(829.04 KiB) Downloaded 45 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 56 guests