Pathfinding A* Améliorations
Bonsoir,
j'ai fais un pathfinding, mais il ne marche pas très bien. Enfin il ne marche pas toujours et des fois il me fais des trucs bizard. Pourriez-vous m'aider ? :ccool:
Conseil ? Amélioration ?
Voilà mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
Public Class Pathfinding
Public movepath As New List(Of UInteger)
Public Cells As New List(Of CellInfo)
Public cellactuel As UInteger
Public cellarrivée As UInteger
Public IsPath As Boolean = False
Public CanMove As Boolean = True
Public Sub FindPath(ByVal listcellopen As List(Of UInteger), ByVal actuell As UInteger, ByVal arrive As UInteger)
'--Variables--
cellactuel = actuell
cellarrivée = arrive
movepath.Add(cellactuel)
'--Vérification--
If cellactuel = cellarrivée Then
Exit Sub
End If
'--Cellules--
For i = 0 To 559
Dim obstacle As Boolean = False
If listcellopen.Contains(i) Then
obstacle = False
Else
obstacle = True
End If
Cells.Add(New CellInfo(i, obstacle))
Next
'--FindPath--
While Not cellactuel = cellarrivée
'--Variables--
Dim casewalk As New List(Of Integer)
Dim x = Cells(cellactuel).X
Dim y = Cells(cellactuel).Y
'--Calcul H--
For i = 0 To 559
Cells(i).Calcul_H(Cells(cellarrivée).X, Cells(cellarrivée).Y)
Next
'--Calcul algo--
For i = 0 To Cells.Count - 1
If Cells(i).X = x And Cells(i).Y = y + 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x And Cells(i).Y = y - 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x - 1 And Cells(i).Y = y And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x + 1 And Cells(i).Y = y And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x + 1 And Cells(i).Y = y + 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x - 1 And Cells(i).Y = y - 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x - 1 And Cells(i).Y = y + 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
If Cells(i).X = x + 1 And Cells(i).Y = y - 1 And Cells(i).obstacle = False Then
casewalk.Add(i)
End If
Next
'Une fois qu'on a nos 8 case ainsi que leur cout F rien de plus simple que le comparer
Dim H As Integer = 99999
Dim index As Integer
For i = 0 To casewalk.Count - 1
'F est inférieur il devient la meilleur case
If h > Cells(casewalk(i)).H Then
H = Cells(casewalk(i)).H
index = casewalk(i)
End If
Next
'On a la cell avec le cout F le plus bas :P
movepath.Add(index)
cellactuel = index
Cells(index).obstacle = True
End While
End Sub
End Class |
Mes autres class:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Public Class MapPosition
'X gauche à droite
'Y bas en haut
Public X As Integer = 0
Public Y As Integer = 0
Public H As Integer
Public Id_cell As UInteger
Public Function CalculXY(ByVal cellid As UInteger)
X = cellid Mod 14
Y = cellid \ 14
Return X
Return Y
End Function
Public Function CalculCellid(ByVal X As Integer, ByVal Y As Integer)
Id_cell = Y * 14 + X
Return Id_cell
End Function
Public Function calculh(ByVal xstart As Integer, ByVal ystart As Integer, ByVal xend As Integer, ByVal yend As Integer)
h = (Math.Abs(xend - xstart) + Math.Abs(yend - ystart))
Return h
End Function
End Class |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Public Class CellInfo
Public X As Integer
Public Y As Integer
Public H As Integer
Public index As Integer
Public obstacle As Boolean = False
Sub New(ByVal indexx As Integer, ByVal obstaclee As Boolean)
index = indexx
Dim Map = New MapPosition
Map.CalculXY(index)
X = Map.X
Y = Map.Y
obstacle = obstaclee
End Sub
Public Sub Calcul_H(ByVal Xarrivé As Integer, ByVal Yarrivé As Integer)
H = 10 * (Math.Abs(Xarrivé - X) + Math.Abs(Yarrivé - Y)) 'la distance à l'arrivé est égale à al diff en abscisse + la diff en ordonnée)
End Sub
End Class |
Cordialement :zoubi: