-
Apr 20th, 2024, 02:52 PM
#1
Thread Starter
PowerPoster
VB2010 - how avoid the '1.#INF'?
see these functions for RayCasting(yes recreating again):
Code:
Private Function GetMapPosition(ByVal Position As Double) As Integer
Return Math.Floor(Position / ObjectSize)
End Function
Private Sub DrawRays()
Dim RayCount As Integer = 1
Dim RayIndex As Integer = 0
Dim RayX As Double = Player.PosX
Dim RayY As Double = Player.PosY
Dim RayStepX As Double = 0.0
Dim RayStepY As Double = 0.0
Dim RayRadians As Double = Player.Radians '- (DegreeToRadians * 30)
Dim RayRadiansStep As Double = 0.0
Dim GridCount As Integer = 0
Dim aTan As Double = 0.0
Dim MapX As Integer = 0
Dim MapY As Integer = 0
'On Error Resume Next
For RayIndex = 0 To RayCount
RayRadians = Player.Radians
GridCount = 0
'Check Horizontal Lines:
aTan = -1 / Math.Tan(RayRadians)
If (RayRadians > Math.PI) Then
'Get next Intersection:
RayY = ((Player.PosY >> 6) << 6) - 0.0001
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = -ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians < Math.PI) Then
'Get next Intersection:
RayY = ((Player.PosY >> 6) << 6) + ObjectSize
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians = 0 Or RayRadians = Math.PI) Then
RayY = Player.PosY
RayX = Player.PosX
Continue For
End If
'On Error Resume Next
Do While (GridCount < 8)
MapX = GetMapPosition(RayX)
MapY = GetMapPosition(RayY)
If (MapLevel0(MapX, MapY) <> Color.Black) Then
RayX += RayStepX
RayY += RayStepY
GridCount += 1
Else
GridCount = 8
End If
Loop
imgLevelMap0Graphics.DrawLine(Pens.Yellow, New Point(Player.PosX, Player.PosY), New Point(Math.Floor(RayX), Math.Floor(RayY)))
Next
'Debug.Print(CStr(RayStepX) & vbTab & CStr(RayStepY))
End Sub
these line:
Code:
Return Math.Floor(Position / ObjectSize)
the 'Position' give me '1.#INF'.. it's an error..: "A first chance exception of type 'System.OverflowException' occurred in RayCasting1.exe"
how can i avoid these type of error?
the GetMapPosition() is for give me the array map position(an integer value).
-
Apr 20th, 2024, 03:29 PM
#2
Re: VB2010 - how avoid the '1.#INF'?
When you get this exception what are the values for Position and ObjectSize?
-
Apr 20th, 2024, 03:31 PM
#3
Re: VB2010 - how avoid the '1.#INF'?
At no place in your code are you declaring or setting a value of ObjectSize, so we have no idea what type it is or what value(s) it may have when your code runs.
Assuming it is something like a Double, as ObjectSize approaches 0, Position / ObjectSize will approach Infinity. So you will likely need to have some sort of lower bounds check of what the value of ObjectSize is, and if it is too close to 0, you'll need to have different code to handle that situation.
-
Apr 20th, 2024, 03:34 PM
#4
Thread Starter
PowerPoster
Re: VB2010 - how avoid the '1.#INF'?
the 'ObjectSize' is 64.... when the 'RayRadians' is zero..
i can get here:
Code:
aTan = -1 / Math.Tan(RayRadians)
because if the 'Math.Tan(RayRadians)' is zero, it will be error\infinite...
other times i can get a much big number 
i'm testing more the values and maybe avoiding more errors 
to be honest, i'm new with VB2010... so i'm trying understand the errors.. thanks for all
the problem isn't resolved... but i'm trying
-
Apr 20th, 2024, 04:04 PM
#5
Re: VB2010 - how avoid the '1.#INF'?
Looks like you are calculating a perpendicular slope there. And you have to remember that in the world of the x, y axis, the slope (dy/dx) of a vertical line is infinity, since the x value is constant. So if the original line is horizontal, you need extra code to handle calculations related to the line perpendicular to it, since the "true" slope of that line is infinity.
-
Apr 20th, 2024, 04:36 PM
#6
Thread Starter
PowerPoster
Re: VB2010 - how avoid the '1.#INF'?
is there another way for avoid these several errors\bugs:
Code:
Private Function GetMapPosition(ByVal Position As Double) As Integer
Return Math.Floor(Position / ObjectSize)
End Function
Private Sub DrawRays()
Dim RayCount As Integer = 1
Dim RayIndex As Integer = 0
Dim RayX As Double = Player.PosX
Dim RayY As Double = Player.PosY
Dim RayStepX As Double = 0.0
Dim RayStepY As Double = 0.0
Dim RayRadians As Double = Player.Radians '- (DegreeToRadians * 30)
Dim RayRadiansStep As Double = 0.0
Dim GridCount As Integer = 0
Dim aTan As Double = 0.0
Dim MapX As Integer = 0
Dim MapY As Integer = 0
For RayIndex = 0 To RayCount
RayRadians = Player.Radians
GridCount = 0
'Check Horizontal Lines:
aTan = -1 / Math.Tan(RayRadians)
'Get next Intersection:
If (RayRadians > PI) Then
RayY = ((Player.PosY >> 6) << 6) - 0.0001
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = -ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians < PI) Then
RayY = ((Player.PosY >> 6) << 6) + ObjectSize
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians = 0 Or RayRadians = PI) Then 'no ray
RayY = Player.PosY
RayX = Player.PosX
RayStepY = 0
RayStepX = 0
GridCount = 8
End If
'Get next Intersections:
'enter on 'while' if the step is different than zero... and only count 8 steps\adds:
Do While (RayStepX <> 0 And RayStepY <> 0 And GridCount < 8)
'avoiding the Ray position be more the map\array size and draw the array:
If (RayX > (9 * ObjectSize) Or RayY > (9 * ObjectSize)) Then Continue For
'Get Map Position:
MapX = GetMapPosition(RayX)
MapY = GetMapPosition(RayY)
'Avoid negative and more than 9 index array:
If (MapX > 9 Or MapY > 9) Then Exit Do
If (MapX < 0 Or MapY < 0) Then Exit Do
If (MapLevel0(MapX, MapY) <> Color.Black) Then
RayX += RayStepX
RayY += RayStepY
GridCount += 1
Else
GridCount = 8
End If
Loop
imgLevelMap0Graphics.DrawLine(Pens.Yellow, New Point(Player.PosX, Player.PosY), New Point(Math.Floor(RayX), Math.Floor(RayY)))
Next
End Sub
????
even controlling the 'Mapx' and 'Mapy' isn't easy 
'9' is the count squares on map\array. by width(9*objectsize) and height((9*objectsize) sizes...
Last edited by joaquim; Apr 20th, 2024 at 04:44 PM.
-
Apr 20th, 2024, 05:29 PM
#7
Re: VB2010 - how avoid the '1.#INF'?
You can't blindly perform a division when the denominator can potentially be 0, and you still are doing that in that code. Your existing aTan calculation must be inside of logic that verifies that the result of Math.Tan(RayRadians) isn't 0. And in the case that it IS 0, I would imagine you would need entirely new logic below that that accommodates a purely "vertical" tangent line.
-
Apr 21st, 2024, 01:03 PM
#8
Thread Starter
PowerPoster
Re: VB2010 - how avoid the '1.#INF'?
updated:
Code:
Private Function GetMapPosition(ByVal Position As Double) As Integer
Return Math.Floor(Position / ObjectSize)
End Function
Private Sub DrawRays()
Dim RayCount As Integer = 1
Dim RayIndex As Integer = 0
Dim RayX As Double = Player.PosX
Dim RayY As Double = Player.PosY
Dim RayStepX As Double = 0.0
Dim RayStepY As Double = 0.0
Dim RayRadians As Double = Player.Radians '- (DegreeToRadians * 30)
Dim RayRadiansStep As Double = 0.0
Dim GridCount As Integer = 0
Dim aTan As Double = 0.0
Dim MapX As Integer = 0
Dim MapY As Integer = 0
For RayIndex = 0 To RayCount
RayRadians = Player.Radians
GridCount = 0
'Check Horizontal Lines:
'avoiding division by zero:
Dim tan As Double = Math.Tan(RayRadians)
If (tan = 0) Then tan = 1
aTan = -1 / tan
'Get next Intersection:
If (RayRadians > PI) Then
RayY = ((Player.PosY >> 6) << 6) - 0.0001
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = -ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians < PI) Then
RayY = ((Player.PosY >> 6) << 6) + ObjectSize
RayX = (Player.PosY - RayY) * aTan + Player.PosX
RayStepY = ObjectSize
RayStepX = -RayStepY * aTan
ElseIf (RayRadians = 0 Or RayRadians = PI) Then 'no ray
RayY = Player.PosY
RayX = Player.PosX
RayStepY = 0
RayStepX = 0
GridCount = 8
Continue For
End If
'Get next Intersections:
'enter on 'while' if the step is different than zero... and only count 8 steps\adds:
Do While (RayStepX <> 0 And RayStepY <> 0 And GridCount < 8)
'avoiding the Ray position be more the map\array size and draw the array:
If (RayX > (9 * ObjectSize) Or RayY > (9 * ObjectSize)) Then Continue For
'Get Map Position:
MapX = GetMapPosition(RayX)
MapY = GetMapPosition(RayY)
'Avoid negative and more than 9 index array:
If (MapX > 9 Or MapY > 9) Then Exit Do
If (MapX < 0 Or MapY < 0) Then Exit Do
If (MapLevel0(MapX, MapY) <> Color.Black) Then
RayX += RayStepX
RayY += RayStepY
GridCount += 1
Else
GridCount = 8
End If
Loop
If (RayX <> Player.PosX Or RayY <> Player.PosY) Then imgLevelMap0Graphics.DrawLine(Pens.Yellow, New Point(Player.PosX, Player.PosY), New Point(Math.Floor(RayX), Math.Floor(RayY)))
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|