NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavMeshFunnel.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <vector>
7
8namespace NoJob
9{
10 namespace
11 {
12 constexpr float FunnelEpsilon = 0.00001f;
13 }
14
16 const NavMesh& navMesh,
17 const NavPath& corridor,
18 const glm::vec3& startPosition,
19 const glm::vec3& goalPosition)
20 {
21 NavPointPath result;
22
23 if (!corridor.IsValid())
24 return result;
25
26 if (corridor.Polygons.size() == 1)
27 {
28 result.Points.push_back(startPosition);
29
30 if (!NearlyEqual(
31 startPosition,
32 goalPosition))
33 {
34 result.Points.push_back(
35 goalPosition);
36 }
37
38 return result;
39 }
40
41 std::vector<Portal> portals;
42
43 if (!BuildPortals(
44 navMesh,
45 corridor,
46 startPosition,
47 goalPosition,
48 portals))
49 {
50 return result;
51 }
52
53 if (portals.size() < 2)
54 return result;
55
56 result.Points.push_back(
57 startPosition);
58
59 glm::vec3 portalApex =
60 portals[0].Left;
61
62 glm::vec3 portalLeft =
63 portals[0].Left;
64
65 glm::vec3 portalRight =
66 portals[0].Right;
67
68 std::size_t apexIndex = 0;
69 std::size_t leftIndex = 0;
70 std::size_t rightIndex = 0;
71
72 for (std::size_t i = 1;
73 i < portals.size();
74 ++i)
75 {
76 const glm::vec3 newLeft =
77 portals[i].Left;
78
79 const glm::vec3 newRight =
80 portals[i].Right;
81
82 // --------------------------------------------------------
83 // Tighten the right side of the funnel.
84 // --------------------------------------------------------
85
86 if (SignedAreaXZ(
87 portalApex,
88 portalRight,
89 newRight) <= 0.0f)
90 {
91 if (NearlyEqual(
92 portalApex,
93 portalRight) ||
94 SignedAreaXZ(
95 portalApex,
96 portalLeft,
97 newRight) > 0.0f)
98 {
99 portalRight =
100 newRight;
101
102 rightIndex = i;
103 }
104 else
105 {
106 if (result.Points.empty() ||
107 !NearlyEqual(
108 result.Points.back(),
109 portalLeft))
110 {
111 result.Points.push_back(
112 portalLeft);
113 }
114
115 portalApex =
116 portalLeft;
117
118 apexIndex =
119 leftIndex;
120
121 portalLeft =
122 portalApex;
123
124 portalRight =
125 portalApex;
126
127 leftIndex =
128 apexIndex;
129
130 rightIndex =
131 apexIndex;
132
133 i = apexIndex;
134 continue;
135 }
136 }
137
138 // --------------------------------------------------------
139 // Tighten the left side of the funnel.
140 // --------------------------------------------------------
141
142 if (SignedAreaXZ(
143 portalApex,
144 portalLeft,
145 newLeft) >= 0.0f)
146 {
147 if (NearlyEqual(
148 portalApex,
149 portalLeft) ||
150 SignedAreaXZ(
151 portalApex,
152 portalRight,
153 newLeft) < 0.0f)
154 {
155 portalLeft =
156 newLeft;
157
158 leftIndex = i;
159 }
160 else
161 {
162 if (result.Points.empty() ||
163 !NearlyEqual(
164 result.Points.back(),
165 portalRight))
166 {
167 result.Points.push_back(
168 portalRight);
169 }
170
171 portalApex =
172 portalRight;
173
174 apexIndex =
175 rightIndex;
176
177 portalLeft =
178 portalApex;
179
180 portalRight =
181 portalApex;
182
183 leftIndex =
184 apexIndex;
185
186 rightIndex =
187 apexIndex;
188
189 i = apexIndex;
190 continue;
191 }
192 }
193 }
194
195 if (result.Points.empty() ||
196 !NearlyEqual(
197 result.Points.back(),
198 goalPosition))
199 {
200 result.Points.push_back(
201 goalPosition);
202 }
203
204 return result;
205 }
206
207 bool NavMeshFunnel::BuildPortals(
208 const NavMesh& navMesh,
209 const NavPath& corridor,
210 const glm::vec3& startPosition,
211 const glm::vec3& goalPosition,
212 std::vector<Portal>& portals)
213 {
214 portals.clear();
215
216 if (corridor.Polygons.empty())
217 return false;
218
219 // Degenerate start portal.
220 portals.push_back(
221 {
222 startPosition,
223 startPosition
224 });
225
226 for (std::size_t i = 0;
227 i + 1 < corridor.Polygons.size();
228 ++i)
229 {
230 Portal portal;
231
232 if (!FindSharedPortal(
233 navMesh,
234 corridor.Polygons[i],
235 corridor.Polygons[i + 1],
236 portal))
237 {
238 portals.clear();
239 return false;
240 }
241
242 // Keep waypoints away from the exact portal vertices.
243 // This reduces corner grazing on voxelized NavMeshes.
244 // Never invert a narrow portal.
245 glm::vec3 portalWidth = portal.Right - portal.Left;
246 portalWidth.y = 0.0f;
247 const float width = glm::length(portalWidth);
248 if (width > 0.0001f)
249 {
250 constexpr float desiredInset = 0.12f;
251 const float inset = std::min(desiredInset, width * 0.20f);
252 const glm::vec3 along = (portal.Right - portal.Left) / width;
253 portal.Left += along * inset;
254 portal.Right -= along * inset;
255 }
256
257 portals.push_back(
258 portal);
259 }
260
261 // Degenerate goal portal.
262 portals.push_back(
263 {
264 goalPosition,
265 goalPosition
266 });
267
268 return true;
269 }
270
271 bool NavMeshFunnel::FindSharedPortal(
272 const NavMesh& navMesh,
273 NavPolygonID fromPolygon,
274 NavPolygonID toPolygon,
275 Portal& portal)
276 {
277 const NavPolygon* from =
278 navMesh.GetPolygon(
279 fromPolygon);
280
281 const NavPolygon* to =
282 navMesh.GetPolygon(
283 toPolygon);
284
285 if (!from || !to)
286 return false;
287
288 for (const NavEdge& edge :
289 from->Edges)
290 {
291 if (!edge.Traversable)
292 continue;
293
294 if (edge.NeighborPolygon !=
295 toPolygon)
296 {
297 continue;
298 }
299
300 const NavVertex* start =
301 navMesh.GetVertex(
302 edge.StartVertex);
303
304 const NavVertex* end =
305 navMesh.GetVertex(
306 edge.EndVertex);
307
308 if (!start || !end)
309 return false;
310
311 /*
312 * Our polygons use consistent winding and adjacency
313 * stores the shared edge from the perspective of the
314 * current polygon.
315 *
316 * With the current navigation winding, traversing from
317 * one polygon to its neighbour means:
318 *
319 * EndVertex -> left side
320 * StartVertex -> right side
321 */
322 portal.Left =
323 end->Position;
324
325 portal.Right =
326 start->Position;
327
328 return true;
329 }
330
331 return false;
332 }
333
334 float NavMeshFunnel::SignedAreaXZ(
335 const glm::vec3& a,
336 const glm::vec3& b,
337 const glm::vec3& c)
338 {
339 const float abX =
340 b.x - a.x;
341
342 const float abZ =
343 b.z - a.z;
344
345 const float acX =
346 c.x - a.x;
347
348 const float acZ =
349 c.z - a.z;
350
351 return
352 abX * acZ -
353 abZ * acX;
354 }
355
356 bool NavMeshFunnel::NearlyEqual(
357 const glm::vec3& a,
358 const glm::vec3& b)
359 {
360 const glm::vec3 delta =
361 a - b;
362
363 return
364 glm::dot(delta, delta) <=
365 FunnelEpsilon *
366 FunnelEpsilon;
367 }
368}
NavMeshFunnel declarations for the Navigation subsystem.
static NavPointPath BuildPath(const NavMesh &navMesh, const NavPath &corridor, const glm::vec3 &startPosition, const glm::vec3 &goalPosition)
uint32_t NavPolygonID
Definition NavMesh.h:18
bool IsValid() const
std::vector< NavPolygonID > Polygons
std::vector< glm::vec3 > Points