NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavMeshPathfinder.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <limits>
6#include <queue>
7#include <vector>
8
9namespace NoJob
10{
11 namespace
12 {
13 constexpr float Infinity =
14 std::numeric_limits<float>::infinity();
15
16 struct OpenNode
17 {
18 NavPolygonID Polygon =
20
21 float FScore = Infinity;
22 };
23
24 struct OpenNodeCompare
25 {
26 bool operator()(
27 const OpenNode& lhs,
28 const OpenNode& rhs) const
29 {
30 return lhs.FScore > rhs.FScore;
31 }
32 };
33 }
34
36 const NavMesh& navMesh,
37 NavPolygonID startPolygon,
38 NavPolygonID goalPolygon)
39 {
40 NavPath emptyPath;
41
42 const NavPolygon* start =
43 navMesh.GetPolygon(startPolygon);
44
45 const NavPolygon* goal =
46 navMesh.GetPolygon(goalPolygon);
47
48 if (!start || !goal)
49 return emptyPath;
50
51 if (startPolygon == goalPolygon)
52 {
53 NavPath path;
54 path.Polygons.push_back(startPolygon);
55 return path;
56 }
57
58 const std::size_t polygonCount =
59 navMesh.GetPolygonCount();
60
61 std::vector<float> gScore(
62 polygonCount,
63 Infinity);
64
65 std::vector<NavPolygonID> cameFrom(
66 polygonCount,
68
69 std::vector<bool> closed(
70 polygonCount,
71 false);
72
73 std::priority_queue<
74 OpenNode,
75 std::vector<OpenNode>,
76 OpenNodeCompare>
77 openSet;
78
79 gScore[startPolygon] = 0.0f;
80
81 openSet.push(
82 {
83 startPolygon,
84 Heuristic(
85 navMesh,
86 startPolygon,
87 goalPolygon)
88 });
89
90 while (!openSet.empty())
91 {
92 const OpenNode currentNode =
93 openSet.top();
94
95 openSet.pop();
96
97 const NavPolygonID currentID =
98 currentNode.Polygon;
99
100 if (currentID >= polygonCount)
101 continue;
102
103 if (closed[currentID])
104 continue;
105
106 if (currentID == goalPolygon)
107 {
108 return ReconstructPath(
109 startPolygon,
110 goalPolygon,
111 cameFrom);
112 }
113
114 closed[currentID] = true;
115
116 const NavPolygon* currentPolygon =
117 navMesh.GetPolygon(currentID);
118
119 if (!currentPolygon)
120 continue;
121
122 for (const NavEdge& edge :
123 currentPolygon->Edges)
124 {
125 if (!edge.Traversable)
126 continue;
127
128 const NavPolygonID neighborID =
129 edge.NeighborPolygon;
130
131 if (neighborID ==
133 {
134 continue;
135 }
136
137 if (neighborID >= polygonCount)
138 continue;
139
140 if (closed[neighborID])
141 continue;
142
143 const NavPolygon* neighbor =
144 navMesh.GetPolygon(
145 neighborID);
146
147 if (!neighbor)
148 continue;
149
150 const float transitionCost =
151 CalculateTransitionCost(
152 navMesh,
153 currentID,
154 neighborID,
155 edge);
156
157 if (!std::isfinite(
158 transitionCost))
159 {
160 continue;
161 }
162
163 const float tentativeGScore =
164 gScore[currentID] +
165 transitionCost;
166
167 if (tentativeGScore >=
168 gScore[neighborID])
169 {
170 continue;
171 }
172
173 cameFrom[neighborID] =
174 currentID;
175
176 gScore[neighborID] =
177 tentativeGScore;
178
179 const float fScore =
180 tentativeGScore +
181 Heuristic(
182 navMesh,
183 neighborID,
184 goalPolygon);
185
186 openSet.push(
187 {
188 neighborID,
189 fScore
190 });
191 }
192 }
193
194 return emptyPath;
195 }
196
197 float NavMeshPathfinder::Heuristic(
198 const NavMesh& navMesh,
199 NavPolygonID from,
200 NavPolygonID to)
201 {
202 const NavPolygon* fromPolygon =
203 navMesh.GetPolygon(from);
204
205 const NavPolygon* toPolygon =
206 navMesh.GetPolygon(to);
207
208 if (!fromPolygon ||
209 !toPolygon)
210 {
211 return 0.0f;
212 }
213
214 return glm::distance(
215 fromPolygon->Center,
216 toPolygon->Center);
217 }
218
219 float NavMeshPathfinder::
220 CalculateTransitionCost(
221 const NavMesh& navMesh,
222 NavPolygonID from,
223 NavPolygonID to,
224 const NavEdge& edge)
225 {
226 if (!edge.Traversable)
227 return Infinity;
228
229 const NavPolygon* fromPolygon =
230 navMesh.GetPolygon(from);
231
232 const NavPolygon* toPolygon =
233 navMesh.GetPolygon(to);
234
235 if (!fromPolygon ||
236 !toPolygon)
237 {
238 return Infinity;
239 }
240
241 if (edge.Cost < 0.0f ||
242 toPolygon->Cost < 0.0f)
243 {
244 return Infinity;
245 }
246
247 const float movementCost =
248 glm::distance(
249 fromPolygon->Center,
250 toPolygon->Center);
251
252 return
253 movementCost +
254 edge.Cost +
255 toPolygon->Cost;
256 }
257
258 NavPath NavMeshPathfinder::ReconstructPath(
259 NavPolygonID startPolygon,
260 NavPolygonID goalPolygon,
261 const std::vector<NavPolygonID>& cameFrom)
262 {
263 NavPath path;
264
265 NavPolygonID current =
266 goalPolygon;
267
268 while (current !=
270 {
271 path.Polygons.push_back(
272 current);
273
274 if (current ==
275 startPolygon)
276 {
277 break;
278 }
279
280 if (current >=
281 cameFrom.size())
282 {
283 path.Clear();
284 return path;
285 }
286
287 current =
288 cameFrom[current];
289 }
290
291 if (path.Polygons.empty() ||
292 path.Polygons.back() !=
293 startPolygon)
294 {
295 path.Clear();
296 return path;
297 }
298
299 std::reverse(
300 path.Polygons.begin(),
301 path.Polygons.end());
302
303 return path;
304 }
305}
NavMeshPathfinder declarations for the Navigation subsystem.
const NavPolygon * GetPolygon(NavPolygonID id) const
Definition NavMesh.cpp:137
size_t GetPolygonCount() const
Definition NavMesh.h:69
static NavPath FindPath(const NavMesh &navMesh, NavPolygonID startPolygon, NavPolygonID goalPolygon)
constexpr NavPolygonID InvalidNavPolygonID
Definition NavMesh.h:23
uint32_t NavPolygonID
Definition NavMesh.h:18
NavPolygonID NeighborPolygon
Definition NavMesh.h:36
bool Traversable
Definition NavMesh.h:38
std::vector< NavPolygonID > Polygons
std::vector< NavEdge > Edges
Definition NavMesh.h:45
glm::vec3 Center
Definition NavMesh.h:47