Problèmes de calculs de rayons pour un moteur raycasting

flinterswap - Modifié le 16 juin 2023 à 14:33

Bonjour à tous et toutes,

J'aimerais programmer un "moteur" de raycasting comme DOOM ou Duke Nukem. un youtubeur (3Dsage) a fait trois vidéos pour expliquer comment en faire un.

Le souci c'est qu'il code en C++ avec openGL, et moi j'utilise LUA et le framework Love2D. J'ai essayé de décrypter tant bien que mal son code, et j'ai réussi à aller assez loin dans le tutoriel, mais au moment de calculer les deux premiers rayons, des bugs surviennent.

Par exemple, les deux premiers rayons ont deux angles différents, alors qu'ils devraient tout les deux être à la meme position (j'espère que j'arrive à me faire comprendre).

Voici ma fonction, si par miracle, vous voyez ce qui cloche je vous en serais très reconnaissant :) 

function raycast()
    rayAngle = playerAngle
    rayX = 0
    rayY = 0
    mx = 0
    my = 0
    mp = 0
    offsetX = 0
    offsetY = 0
    
    for i=0,1 do

        dof = 0

        aTan = -1/math.tan(rayAngle)

        if rayAngle > pi then
            rayY = math.floor(playerY/50)*50
            rayX = (playerY - rayY)*aTan + playerX
            offsetY = -50
            offsetX = -offsetY*aTan
            
        end

        if rayAngle < pi then
           rayY = math.floor(playerY/50)*50
           rayX = (playerY - rayY)*aTan + playerX
           offsetY = 50
           offsetX = -offsetY*aTan
        end

        if rayAngle == 0 or rayAngle == pi then
            rayX = playerX
            rayY = playerY
            dof = 20
        end

        while dof<20 do
            mx = math.floor(rayX/50)
            my = math.floor(rayY/50)
            mp = my*20+mx
            if mp > 0 and mp < 20*20 and carte [mp] == 1 then
                dof = 20
            else
                rayX = rayX+offsetX
                rayY = rayY+offsetY
                dof = dof+1
            end
        end

        love.graphics.setColor(1, 0, 0)
        love.graphics.line(playerX - 25, playerY - 25, rayX - 50, rayY)
        love.graphics.reset()
        love.graphics.print(mp, 1000, 100)

        ---------------------------------------------------------------

        dof = 0

        nTan = -math.tan(rayAngle)
        
        if rayAngle > pi/2 and rayAngle < 3*pi/2 then
            rayX = math.floor(playerX/50)*50
            rayY = (playerX - rayX)*nTan + playerY
            offsetX = -50
            offsetY = -offsetX*nTan
            
        end

        if rayAngle < pi/2 or rayAngle > 3*pi/2 then
           rayX = math.floor(playerX/50)*50
           rayY = (playerX - rayX)*nTan + playerY
           offsetX = 50
           offsetY = -offsetX*nTan
        end

        if rayAngle == pi*2 or rayAngle == 3*pi/2 then
            rayX = playerX
            rayY = playerY
            dof = 20
        end

        while dof<20 do
            mx = math.floor(rayX/50)
            my = math.floor(rayY/50)
            mp = my*20+mx
            if mp > 0 and mp < 20*20 and carte [mp] == 1 then
                dof = 20
            else
                rayX = rayX+offsetX
                rayY = rayY+offsetY
                dof = dof+1
            end
        end

        love.graphics.setColor(0, 1, 0)
        love.graphics.line(playerX - 25, playerY - 25, rayX, rayY)
        love.graphics.reset()
    end    
end


Windows / Chrome 113.0.0.0

A voir également: