-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoadPathfindingComponent.cpp
More file actions
190 lines (160 loc) · 6.01 KB
/
RoadPathfindingComponent.cpp
File metadata and controls
190 lines (160 loc) · 6.01 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "RoadPathfindingComponent.h"
#include "Containers/Queue.h"
#include "Algo/Reverse.h"
URoadPathfindingComponent::URoadPathfindingComponent()
{
PrimaryComponentTick.bCanEverTick = false;
}
TArray<TSharedPtr<FPathNode>> URoadPathfindingComponent::FindAllNodes(const TArray<USplineComponent*>& SplineComponents)
{
TMap<FVector, TSharedPtr<FPathNode>> NodeMap; // Use smart pointers for NodeMap to manage neighbors correctly
// Create nodes and link neighbors
for (USplineComponent* Spline : SplineComponents)
{
FVector StartLocation = Spline->GetLocationAtSplinePoint(0, ESplineCoordinateSpace::World);
FVector EndLocation = Spline->GetLocationAtSplinePoint(Spline->GetNumberOfSplinePoints() - 1, ESplineCoordinateSpace::World);
// Check or add StartNode
TSharedPtr<FPathNode>* StartNodePtr = NodeMap.Find(StartLocation);
if (StartNodePtr == nullptr)
{
TSharedPtr<FPathNode> NewNode = MakeShared<FPathNode>(StartLocation);
NodeMap.Add(StartLocation, NewNode);
StartNodePtr = &NewNode;
}
// Check or add EndNode
TSharedPtr<FPathNode>* EndNodePtr = NodeMap.Find(EndLocation);
if (EndNodePtr == nullptr)
{
TSharedPtr<FPathNode> NewNode = MakeShared<FPathNode>(EndLocation);
NodeMap.Add(EndLocation, NewNode);
EndNodePtr = &NewNode;
}
// Link nodes as neighbors if they are different
if (*StartNodePtr != *EndNodePtr)
{
if (!(*StartNodePtr)->Neighbors.Contains(*EndNodePtr))
{
(*StartNodePtr)->Neighbors.Add(*EndNodePtr);
}
if (!(*EndNodePtr)->Neighbors.Contains(*StartNodePtr))
{
(*EndNodePtr)->Neighbors.Add(*StartNodePtr);
}
}
}
// Convert NodeMap values to TArray
TArray<TSharedPtr<FPathNode>> PathNodes;
NodeMap.GenerateValueArray(PathNodes);
return PathNodes;
}
TArray<TSharedPtr<FPathNode>> URoadPathfindingComponent::AStarPathfinding(TSharedPtr<FPathNode> StartNode, TSharedPtr<FPathNode> GoalNode, const TArray<TSharedPtr<FPathNode>>& AllNodes)
{
if (!StartNode.IsValid() || !GoalNode.IsValid())
{
return TArray<TSharedPtr<FPathNode>>();
}
TMap<FVector, TSharedPtr<FPathNode>> NodeMap;
for (TSharedPtr<FPathNode> Node : AllNodes)
{
NodeMap.Add(Node->Location, Node);
}
// Open set (nodes to be evaluated) and closed set (nodes already evaluated)
TSet<TSharedPtr<FPathNode>> OpenSet;
TSet<TSharedPtr<FPathNode>> ClosedSet;
TMap<TSharedPtr<FPathNode>, TSharedPtr<FPathNode>> CameFrom; // For path reconstruction
TMap<TSharedPtr<FPathNode>, float> GScore; // Cost from start node
TMap<TSharedPtr<FPathNode>, float> FScore; // Total cost (GScore + heuristic)
// Initialize scores
OpenSet.Add(StartNode);
GScore.Add(StartNode, 0.0f);
FScore.Add(StartNode, FVector::Distance(StartNode->Location, GoalNode->Location));
// Heuristic function (Euclidean distance)
auto Heuristic = [](const FVector& A, const FVector& B) {
return FVector::Distance(A, B);
};
while (OpenSet.Num() > 0)
{
// Get node with the lowest FScore
TSharedPtr<FPathNode> CurrentNode = nullptr;
float LowestScore = FLT_MAX;
for (TSharedPtr<FPathNode> Node : OpenSet)
{
float Score = FScore[Node];
if (Score < LowestScore)
{
LowestScore = Score;
CurrentNode = Node;
}
}
if (CurrentNode == GoalNode)
{
// Reconstruct path
TArray<TSharedPtr<FPathNode>> Path;
while (CameFrom.Contains(CurrentNode))
{
Path.Add(CurrentNode);
CurrentNode = CameFrom[CurrentNode];
}
Path.Add(StartNode);
Algo::Reverse(Path);
return Path;
}
OpenSet.Remove(CurrentNode);
ClosedSet.Add(CurrentNode);
for (TSharedPtr<FPathNode> Neighbor : CurrentNode->Neighbors)
{
TSharedPtr<FPathNode>* ActualNeighborPtr = NodeMap.Find(Neighbor->Location);
if (ActualNeighborPtr == nullptr || ClosedSet.Contains(*ActualNeighborPtr))
{
continue;
}
TSharedPtr<FPathNode> ActualNeighbor = *ActualNeighborPtr;
float TentativeGScore = GScore[CurrentNode] + FVector::Distance(CurrentNode->Location, ActualNeighbor->Location);
if (!OpenSet.Contains(ActualNeighbor))
{
OpenSet.Add(ActualNeighbor);
}
else if (TentativeGScore >= GScore[ActualNeighbor])
{
continue;
}
CameFrom.Add(ActualNeighbor, CurrentNode);
GScore.Add(ActualNeighbor, TentativeGScore);
FScore.Add(ActualNeighbor, TentativeGScore + Heuristic(ActualNeighbor->Location, GoalNode->Location));
}
}
// No path found
return TArray<TSharedPtr<FPathNode>>();
}
TSharedPtr<FPathNode> URoadPathfindingComponent::FindNearestNodeByLocation(const FVector& Location, const TArray<TSharedPtr<FPathNode>>& AllNodes)
{
if (AllNodes.Num() == 0)
{
return nullptr;
}
TSharedPtr<FPathNode> NearestNode = nullptr;
float MinDistance = FLT_MAX;
for (TSharedPtr<FPathNode> Node : AllNodes)
{
float Distance = FVector::Dist(Location, Node->Location);
if (Distance < MinDistance)
{
MinDistance = Distance;
NearestNode = Node;
}
}
return NearestNode;
}
TArray<FVector> URoadPathfindingComponent::GetLocationsFromPathNodes(const TArray<TSharedPtr<FPathNode>>& PathNodes)
{
TArray<FVector> Locations;
// Iterate through the path nodes and collect their locations
for (TSharedPtr<FPathNode> Node : PathNodes)
{
if (Node.IsValid())
{
Locations.Add(Node->Location);
}
}
return Locations;
}