Results 1 to 3 of 3

Thread: VB6 - raycasting: how get the image line?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,843

    VB6 - raycasting: how get the image line?

    i have the line height and i can draw it:
    Code:
    a.drawline(x0,y0,x1,y1) A.DrawLine 475 + 50 + RayCounts, 200 / 2 - RayHeight / 2, 475 + 50 + RayCounts, 200 / 2 + RayHeight / 2
    'a' is my image class instance.
    now i want get the image line:
    Code:
    If VertDist < HorizDist Then
                ' Draw the vertical ray:
                A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(VertX), Fix(VertY)
                WallDistance = VertDist
                'getting the image line position:
                GridX = VertX Mod ObjectSize
                GridY = VertY Mod ObjectSize
            Else
                ' Draw the horizontal ray:
                A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(HorizX), Fix(HorizY)
                
                WallDistance = HorizDist
                'getting the image line position:
                GridX = HorizX Mod ObjectSize
                GridY = HorizY Mod ObjectSize
            End If
            'Debug.Print GridX & "  " & GridY
            WallDistance = WallDistance * Cos(RayRadians - Player.Radians) 'avoiding the Fish Effect
            RayHeight = (ObjectSize / WallDistance) * 200 ' is the height screen\
            'a.drawline(x0,y0,x1,y1) A.DrawLine 475 + 50 + RayCounts, 200 / 2 - RayHeight / 2, 475 + 50 + RayCounts, 200 / 2 + RayHeight / 2
            If (GridX = 0) Then
                StretchBlt A.MemoryHDC, 475 + 50 + RayCounts, 200 / 2 - RayHeight / 2, 475 + 50 + RayCounts, 200 / 2 + RayHeight / 2, picWall1.hdc, 0, GridY, 1, picWall1.Height, SRCCOPY
            Else
                StretchBlt A.MemoryHDC, 475 + 50 + RayCounts, 200 / 2 - RayHeight / 2, 475 + 50 + RayCounts, 200 / 2 + RayHeight / 2, picWall1.hdc, GridX, 0, picWall1.Width, 1, SRCCOPY
            End If
    the image line is drawed... but incorrectly
    heres the result: https://imgur.com/W6NbjQp
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,843

    Re: VB6 - raycasting: how get the image line?

    finally i get more correct values:
    Code:
    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
        Dim RayRadians As Double
        Dim RadiansSteps As Double
        Dim RayCount As Integer
        Dim RayCounts As Integer
        Dim blnSomething As Boolean
        Dim OffSetGrid As Long
    
        
        RayCount = 320
    
        MapX = Player.MapX
        MapY = Player.MapY
        RadiansSteps = Radian60 / 320
        
        'On Error Resume Next
        RayRadians = (Player.Radians - Radian30)
        RayCounts = 0
        Do While RayCounts < RayCount
            If (RayRadians > (2 * pi)) Then RayRadians = 0.001
            'If (RayRadians < 0) Then RayRadians = (2 * pi)
            'Check for horizontal intersections:
            If RayRadians > 0 And RayRadians < pi Then 'Facing down
                AY = (Int(Player.PosY / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
                AX = Player.PosX + (AY - Player.PosY) / Tan(RayRadians)
                StepY = ObjectSize
            ElseIf RayRadians = 0 Or RayRadians = 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(RayRadians)
                StepY = -ObjectSize
            End If
    
    
            HorizX = AX
            HorizY = AY
            StepX = StepY / Tan(RayRadians)
            If HorizX < 0 Then
                MapX = 0
            ElseIf HorizX > 9 * ObjectSize Then
                MapX = 9
            Else
                MapX = Fix(HorizX / ObjectSize)
            End If
            
            If HorizY < 0 Then
                MapY = 0
            ElseIf HorizY > 9 * ObjectSize Then
                MapY = 9
            Else
                MapY = Fix(HorizY / ObjectSize)
            End If
           
            
            blnSomething = True
     
            Do While blnSomething = True
                If MapX < 0 Or MapX > 9 Or MapY < 0 Or MapY > 9 Then blnSomething = False
                If blnSomething = True Then
                  If LevelMap0(MapY, MapX) = vbBlue Then blnSomething = False
                End If
                If blnSomething = True Then
    
                    HorizX = HorizX + StepX
                    HorizY = HorizY + StepY
    
                    MapX = Fix((HorizX) / ObjectSize)
                    MapY = Fix((HorizY) / ObjectSize)
                End If
            Loop
            
    
            HorizDist = Abs((Player.PosX - HorizX) / Cos(RayRadians))
    
            'Check for vertical intersections:
            If RayRadians < pi / 2 Or RayRadians > 3 * pi / 2 Then 'Facing right
                AX = (Int(Player.PosX / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
                AY = Player.PosY + (Player.PosX - AX) * -Tan(RayRadians)
                StepX = ObjectSize
            ElseIf RayRadians = pi / 2 Or RayRadians = 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(RayRadians)
                StepX = -ObjectSize
            End If
    
    
            VertX = AX
            VertY = AY
            StepY = StepX * Tan(RayRadians)
            If VertX < 0 Then
                MapX = 0
            ElseIf VertX > 9 * ObjectSize Then
                MapX = 9
            Else
                MapX = Fix(VertX / ObjectSize)
            End If
            
            If VertY < 0 Then
                MapY = 0
            ElseIf VertY > 9 * ObjectSize Then
                MapY = 9
            Else
                MapY = Fix(VertY / ObjectSize)
            End If
            
            blnSomething = True
           Do While blnSomething = True
                If MapX < 0 Or MapX > 9 Or MapY < 0 Or MapY > 9 Then blnSomething = False
                If blnSomething = True Then
                  If LevelMap0(MapY, MapX) = vbBlue Then blnSomething = False
                End If
                If blnSomething = True Then
    
                    VertX = VertX + StepX
                    VertY = VertY + StepY
        
                    MapX = Fix((VertX) / ObjectSize)
                    MapY = Fix((VertY) / ObjectSize)
        
                End If
            Loop
    
            VertDist = Abs((Player.PosX - VertX) / Cos(RayRadians))
            
            
            '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
                
                
            Else
                ' Draw the horizontal ray:
                A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(HorizX), Fix(HorizY)
                
                WallDistance = HorizDist
                
                
            End If
            OffSetGrid = VertY Mod ObjectSize
            If (OffSetGrid = 0) Then OffSetGrid = VertX Mod ObjectSize
            WallDistance = WallDistance * Cos(RayRadians - Player.Radians) 'avoiding the Fish Effect
            RayHeight = (ObjectSize / WallDistance) * 200 ' is the height screen\
            picWall1.DrawTextureVerticalLine A.MemoryHDC, OffSetGrid, CLng(RayHeight), 475 + 50 + RayCounts, 60
            OffSetGrid = 0
            RayRadians = RayRadians + RadiansSteps
            
            RayCounts = RayCounts + 1
            
        Loop
    End Sub
    but i need ask: why i lose some pixels when i'm loking up?
    https://imgur.com/33mUDTb
    i'm testing and i found the problem: how we calculate, correctly the 'OffSetGrid'?
    Last edited by joaquim; Feb 3rd, 2024 at 05:56 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,843

    Re: VB6 - raycasting: how get the image line?

    finally i fix that problem:
    Code:
    Private Sub DrawRays()
         
        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
        Dim RayRadians As Double
        Dim RadiansSteps As Double
        Dim RayCount As Integer
        Dim RayCounts As Integer
        Dim blnSomething As Boolean
        Dim OffSetGrid As Long
    
        
        RayCount = 320
    
        MapX = Player.MapX
        MapY = Player.MapY
        RadiansSteps = Radian60 / 320
        
        'On Error Resume Next
        RayRadians = (Player.Radians - Radian30)
        RayCounts = 0
        Do While RayCounts < RayCount
            If (RayRadians > (2 * pi)) Then RayRadians = 0.001
            'Check for horizontal intersections:
            
            If RayRadians >= 0 And RayRadians <= pi Then 'Facing down
                AY = (Int(Player.PosY / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
                AX = Player.PosX + (AY - Player.PosY) / Tan(RayRadians)
                StepY = ObjectSize
            ElseIf RayRadians = 0 Or RayRadians = 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(RayRadians)
                StepY = -ObjectSize
            End If
    
    
            HorizX = AX
            HorizY = AY
            StepX = StepY / Tan(RayRadians)
            If HorizX < 0 Then
                MapX = 0
            ElseIf HorizX > 9 * ObjectSize Then
                MapX = 9
            Else
                MapX = Fix(HorizX / ObjectSize)
            End If
            
            If HorizY < 0 Then
                MapY = 0
            ElseIf HorizY > 9 * ObjectSize Then
                MapY = 9
            Else
                MapY = Fix(HorizY / ObjectSize)
            End If
           
            
            blnSomething = True
     
            Do While blnSomething = True
                If MapX < 0 Or MapX > 9 Or MapY < 0 Or MapY > 9 Then blnSomething = False
                If blnSomething = True Then
                  If LevelMap0(MapY, MapX) = vbBlue Then blnSomething = False
                End If
                If blnSomething = True Then
    
                    HorizX = HorizX + StepX
                    HorizY = HorizY + StepY
                    If ((HorizX < 0 And HorizX > 9 * ObjectSize) Or (HorizY < 0 And HorizY > 9 * ObjectSize)) Then blnSomething = False
                    MapX = Fix((HorizX) / ObjectSize)
                    MapY = Fix((HorizY) / ObjectSize)
                End If
            Loop
            
    
            HorizDist = Abs((Player.PosX - HorizX) / Cos(RayRadians))
    
            'Check for vertical intersections:
            If RayRadians < pi / 2 Or RayRadians > 3 * pi / 2 Then 'Facing right
                AX = (Int(Player.PosX / ObjectSize) * ObjectSize) + ObjectSize ' Calculate grid position
                AY = Player.PosY + (Player.PosX - AX) * -Tan(RayRadians)
                StepX = ObjectSize
            ElseIf RayRadians = pi / 2 Or RayRadians = 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(RayRadians)
                StepX = -ObjectSize
            End If
    
    
            VertX = AX
            VertY = AY
            StepY = StepX * Tan(RayRadians)
            If VertX < 0 Then
                MapX = 0
            ElseIf VertX > 9 * ObjectSize Then
                MapX = 9
            Else
                MapX = Fix(VertX / ObjectSize)
            End If
            
            If VertY < 0 Then
                MapY = 0
            ElseIf VertY > 9 * ObjectSize Then
                MapY = 9
            Else
                MapY = Fix(VertY / ObjectSize)
            End If
            
            blnSomething = True
           Do While blnSomething = True
                If MapX < 0 Or MapX > 9 Or MapY < 0 Or MapY > 9 Then blnSomething = False
                If blnSomething = True Then
                  If LevelMap0(MapY, MapX) = vbBlue Then blnSomething = False
                End If
                If blnSomething = True Then
                    
                    VertX = VertX + StepX
                    VertY = VertY + StepY
                    If ((VertY < 0 And VertY > 9 * ObjectSize) Or (VertX < 0 Or VertX > 9 * ObjectSize)) Then blnSomething = False
                    MapX = Fix((VertX) / ObjectSize)
                    MapY = Fix((VertY) / ObjectSize)
        
                End If
            Loop
    
            VertDist = Abs((Player.PosX - VertX) / Cos(RayRadians))
            
            
            '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
                OffSetGrid = VertY - (Int(VertY / ObjectSize) * ObjectSize)
                
            Else
                ' Draw the horizontal ray:
                A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(HorizX), Fix(HorizY)
                OffSetGrid = HorizX - (Int(HorizX / ObjectSize) * ObjectSize)
                WallDistance = HorizDist
                
            End If
            
            WallDistance = WallDistance * Cos(RayRadians - Player.Radians) 'avoiding the Fish Effect
            RayHeight = (ObjectSize / WallDistance) * 200 ' is the height screen\
            If (OffSetGrid < 0 Or OffSetGrid >= picWall1.Width) Then OffSetGrid = 0
            picWall1.DrawTextureVerticalLine A.MemoryHDC, OffSetGrid, CLng(RayHeight), 475 + 50 + RayCounts, 60
            OffSetGrid = 0
            RayRadians = RayRadians + RadiansSteps
            
            RayCounts = RayCounts + 1
            
        Loop
    End Sub
    the problem was here:
    Code:
    '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
                OffSetGrid = VertY - (Int(VertY / ObjectSize) * ObjectSize) 'here fixed
                
            Else
                ' Draw the horizontal ray:
                A.DrawLine Fix(Player.PosX), Fix(Player.PosY), Fix(HorizX), Fix(HorizY)
                OffSetGrid = HorizX - (Int(HorizX / ObjectSize) * ObjectSize) 'here fixed
                WallDistance = HorizDist
                
            End If
    i getted the AI help
    now i must fix a bug, because some vertical lines(on square limites) are printed in a different angle\position:
    https://imgur.com/SPP20UD
    here i notice:
    1 - on Horizontal Intersection, between PI and zero, these bug happens;
    2 - on Vertical Intersection, between PI/2 and 3*PI/2, these bug happens...
    how can i fix it?
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width