Ir ao conteúdo
  • Cadastre-se

Visual Basic VB6 - Raycasting: porque não termina quando o quadrado é azul?


Posts recomendados

tenho esta função para fazer o RayCasting usando a Trignometria(estou a testar os pontos Horizontais:
 

Private Sub DrawRays2()
    Dim AY As Double
    Dim AX As Double
    Dim StepX As Double
    Dim StepY As Double
    Dim VertX As Double
    Dim VertY As Double
    Dim HorizX As Double
    Dim HorizY As Double
    Dim MapX As Long
    Dim MapY As Long
    Dim HorizDist As Double
    Dim VertDist As Double
    Dim WallDistance As Double
    Dim RayHeight As Double
    
    MapX = Player.MapX
    MapY = Player.MapY
    On Error Resume Next
    
    'Check for horizontal intersections:
    If ((Player.Radians >= 0 And Player.Radians <= PI)) Then 'Facing down
        AY = (Int(Player.PosY / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
        StepY = ObjectSize
    Else 'Facing Up
        AY = (Int(Player.PosY / ObjectSize) * ObjectSize) - 1
        StepY = -ObjectSize
    End If
    
    AX = Player.PosX + (AY - Player.PosY) / Tan(Player.Radians)
    HorizX = AX
    HorizY = AY
    StepX = StepY / Tan(Player.Radians)
    MapX = Fix((HorizX) / ObjectSize)
    MapY = Fix((HorizY) / ObjectSize)
    A.SetPixel (Fix(HorizX)), (Fix(HorizY)), ColorConstants.vbCyan
    
    Do
            
        HorizX = HorizX + StepX
        HorizY = HorizY + StepY
        
        MapX = Fix((HorizX + 1) / ObjectSize)
        MapY = Fix((HorizY + 1) / ObjectSize)
        A.SetPixel (Fix(HorizX)), (Fix(HorizY)), ColorConstants.vbCyan
        If LevelMap0(MapY, MapX) = vbBlue Then
            Exit Do
        End If
        DoEvents
    Loop
    
    HorizDist = Sqr(((HorizX - Player.PosX) * (HorizX - Player.PosX)) + ((HorizY - Player.PosY) * (HorizY - Player.PosY)))
   
end sub

porque não termina quando chega á parede?(dependendo do Radiano)

Link para o comentário
Compartilhar em outros sites

vejam este print errado 😞
eis o código actual:
  

Private Sub DrawRays2()
    Dim AY As Double
    Dim AX As Double
    Dim StepX As Double
    Dim StepY As Double
    Dim VertX As Double
    Dim VertY As Double
    Dim HorizX As Double
    Dim HorizY As Double
    Dim MapX As Long
    Dim MapY As Long
    Dim HorizDist As Double
    Dim VertDist As Double
    Dim WallDistance As Double
    Dim RayHeight As Double
    
    MapX = Player.MapX
    MapY = Player.MapY
    On Error Resume Next
    
'    'Check for horizontal intersections:
    If ((Player.Radians > 0 And Player.Radians < PI)) Then 'Facing down
        AY = (Int(Player.PosY / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
        AX = Player.PosX + (AY - Player.PosY) / Tan(Player.Radians)
        StepY = ObjectSize
    ElseIf ((Player.Radians = 0 And Player.Radians = PI)) Then
        AY = Player.PosY
        AX = Player.PosX
    Else 'Facing Up
        AY = (Int(Player.PosY / ObjectSize) * ObjectSize) - 1
        AX = Player.PosX + (AY - Player.PosY) / Tan(Player.Radians)
        StepY = -ObjectSize
    End If

    
    HorizX = AX
    HorizY = AY
    StepX = StepY / Tan(Player.Radians)
    MapX = Fix((HorizX) / ObjectSize)
    MapY = Fix((HorizY) / ObjectSize)
    A.SetPixel (Fix(HorizX)), (Fix(HorizY)), ColorConstants.vbCyan
    If LevelMap0(MapY, MapX) <> vbBlue Then

        Do

            HorizX = HorizX + StepX
            HorizY = HorizY + StepY

            MapX = Fix((HorizX) / ObjectSize)
            MapY = Fix((HorizY) / ObjectSize)
            A.SetPixel (Fix(HorizX)), (Fix(HorizY)), ColorConstants.vbCyan
            If LevelMap0(MapY, MapX) = vbBlue Then
                Exit Do
            End If
            DoEvents
        Loop
    End If


    HorizDist = Sqr(((HorizX - Player.PosX) * (HorizX - Player.PosX)) + ((HorizY - Player.PosY) * (HorizY - Player.PosY)))
    
    
    'Check for vertical intersections:
    If ((Player.Radians < PI / 2 Or Player.Radians > 3 * PI / 2)) Then 'Facing right
        AX = (Int(Player.PosX / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
        AY = Player.PosY + (Player.PosX - AX) * -Tan(Player.Radians)
        StepX = ObjectSize
    ElseIf ((Player.Radians = PI / 2 Or Player.Radians = 3 * PI / 2)) Then
        AY = Player.PosY
        AX = Player.PosX
    Else 'Facing left
        AX = (Int(Player.PosX / ObjectSize) * ObjectSize) - 1
        AY = Player.PosY + (Player.PosX - AX) * -Tan(Player.Radians)
        StepX = -ObjectSize
    End If
    
    
    VertX = AX
    VertY = AY
    StepY = StepX * Tan(Player.Radians)
    MapX = Fix((VertX) / ObjectSize)
    MapY = Fix((VertY) / ObjectSize)
    A.SetPixel (Fix(VertX)), (Fix(VertY)), vbYellow
    If LevelMap0(MapY, MapX) <> vbBlue Then
        Do
                
            VertX = VertX + StepX
            VertY = VertY + StepY
            
            
            MapX = Fix((VertX) / ObjectSize)
            MapY = Fix((VertY) / ObjectSize)
            A.SetPixel (Fix(VertX)), (Fix(VertY)), vbYellow
            If LevelMap0(MapY, MapX) = vbBlue Then
                Exit Do
            End If
            
            DoEvents
        Loop
    End If
    
    
    VertDist = Sqr(((VertX - Player.PosX)) * ((VertX - Player.PosX))) + (((VertY - Player.PosY)) * ((VertY - Player.PosY)))
    A.ForeColor vbRed
    
    'obter o a linha mais curta(horizontal ou vertical):
    If VertDist < HorizDist Then
        ' Draw the vertical ray:
        
        A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(VertX), Fix(VertY)
        WallDistance = VertDist
        Debug.Print VertDist & vbTab & "Draw Vertical" & vbTab & HorizDist & vbTab & "Horizontal line"
    Else
        ' Draw the horzontal ray:
        
        A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(HorizX), Fix(HorizY)
        WallDistance = HorizDist
        Debug.Print HorizDist & vbTab & " Draw Horizontal" & vbTab & VertDist & vbTab & "vertical line"
    End If
    
    WallDistance = WallDistance * Cos(Player.Radians)
    RayHeight = ObjectSize / WallDistance * 320
    A.ForeColor vbBlue
    A.DrawLine 475 + 50, 200 / 2 - RayHeight / 2, 475 + 50, 200 / 2 + RayHeight / 2
   
End Sub

printed:"334,308881610545     Draw Horizontal    1816,56277706662    vertical line"
porque a linha vertical é 1816? se o limite é 9*33= 297 e sim noto mais bugs, mas tudo na interceção vertical 😞
PS: o numero de frames é aproximadamente 1500.... mas  o 'Debug.Print' usa imensos recursos... mas é para testar os valores, nada mais 😉

image.png

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!