heres my RayCasting function:
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
output:
35,4424985851603 Draw Horizontal 4548,40096311184 vertical line
the Horizontal intersection works fine.. but why the Vertical intersection give me so big values(4548,40096311184)?
why the horizontal works fine but not the vertical?