NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavigationSystem.cpp
Go to the documentation of this file.
2
7
8#include <cmath>
9#include <utility>
10
11namespace NoJob
12{
13 namespace
14 {
15 constexpr float PointInsideEpsilon =
16 0.00001f;
17 }
18
20 {
23 0.35f, 0.75f, 1.8f, m_GenerationSettings.MaxSlopeAngle);
24
25 if (geometry.Vertices.empty() ||
26 geometry.Indices.size() < 3)
27 {
28 Clear();
29 return false;
30 }
31
32 NavMeshGenerationSettings voxelSettings = m_GenerationSettings;
33 voxelSettings.MergePolygons = false;
34 voxelSettings.VertexWeldTolerance = 0.0f;
35 NavMesh generatedNavMesh = NavMeshGenerator::Generate(geometry, voxelSettings);
36
37 if (generatedNavMesh.GetPolygonCount() == 0)
38 {
39 Clear();
40 return false;
41 }
42
43 m_NavMesh =
44 std::move(generatedNavMesh);
45
46 m_HasNavMesh = true;
47 ++m_NavMeshVersion;
48 return true;
49 }
50
52 {
53 m_NavMesh = NavMesh{};
54 m_HasNavMesh = false;
55 ++m_NavMeshVersion;
56 }
57
59 const glm::vec3& position) const
60 {
61 if (!m_HasNavMesh)
63
64 for (std::size_t polygonIndex = 0;
65 polygonIndex < m_NavMesh.GetPolygonCount();
66 ++polygonIndex)
67 {
68 const NavPolygonID polygonID =
69 static_cast<NavPolygonID>(
70 polygonIndex);
71
72 const NavPolygon* polygon =
73 m_NavMesh.GetPolygon(
74 polygonID);
75
76 if (!polygon)
77 continue;
78
79 if (IsPointInsidePolygonXZ(
80 m_NavMesh,
81 *polygon,
82 position))
83 {
84 return polygonID;
85 }
86 }
87
89 }
90
92 const glm::vec3& startPosition,
93 const glm::vec3& destination) const
94 {
95 NavPointPath emptyPath;
96
97 if (!m_HasNavMesh)
98 return emptyPath;
99
100 const NavPolygonID startPolygon =
101 FindPolygon(startPosition);
102
103 const NavPolygonID goalPolygon =
104 FindPolygon(destination);
105
106 if (startPolygon ==
108 goalPolygon ==
110 {
111 return emptyPath;
112 }
113
114 const NavPath corridor =
116 m_NavMesh,
117 startPolygon,
118 goalPolygon);
119
120 if (!corridor.IsValid())
121 return emptyPath;
122
124 m_NavMesh,
125 corridor,
126 startPosition,
127 destination);
128 }
129
130 bool NavigationSystem::IsPointInsidePolygonXZ(
131 const NavMesh& navMesh,
132 const NavPolygon& polygon,
133 const glm::vec3& position)
134 {
135 if (polygon.Vertices.size() < 3)
136 return false;
137
138 float referenceSign = 0.0f;
139
140 for (std::size_t i = 0;
141 i < polygon.Vertices.size();
142 ++i)
143 {
144 const NavVertexID startID =
145 polygon.Vertices[i];
146
147 const NavVertexID endID =
148 polygon.Vertices[
149 (i + 1) %
150 polygon.Vertices.size()];
151
152 const NavVertex* start =
153 navMesh.GetVertex(startID);
154
155 const NavVertex* end =
156 navMesh.GetVertex(endID);
157
158 if (!start || !end)
159 return false;
160
161 const float edgeX =
162 end->Position.x -
163 start->Position.x;
164
165 const float edgeZ =
166 end->Position.z -
167 start->Position.z;
168
169 const float pointX =
170 position.x -
171 start->Position.x;
172
173 const float pointZ =
174 position.z -
175 start->Position.z;
176
177 const float cross =
178 edgeX * pointZ -
179 edgeZ * pointX;
180
181 if (std::abs(cross) <=
182 PointInsideEpsilon)
183 {
184 continue;
185 }
186
187 if (referenceSign == 0.0f)
188 {
189 referenceSign = cross;
190 continue;
191 }
192
193 if ((referenceSign > 0.0f &&
194 cross < -PointInsideEpsilon) ||
195 (referenceSign < 0.0f &&
196 cross > PointInsideEpsilon))
197 {
198 return false;
199 }
200 }
201
202 return true;
203 }
204}
NavMeshGeometryExtractor declarations for the Navigation subsystem.
NavMeshPathfinder declarations for the Navigation subsystem.
NavMeshVoxelizer declarations for the Navigation subsystem.
NavigationSystem declarations for the Navigation subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
static NavPointPath BuildPath(const NavMesh &navMesh, const NavPath &corridor, const glm::vec3 &startPosition, const glm::vec3 &goalPosition)
static NavMesh Generate(const NavMeshSourceGeometry &geometry, const NavMeshGenerationSettings &settings={})
static NavMeshSourceGeometry Extract(Scene &scene)
const NavVertex * GetVertex(NavVertexID id) const
Definition NavMesh.cpp:127
size_t GetPolygonCount() const
Definition NavMesh.h:69
static NavPath FindPath(const NavMesh &navMesh, NavPolygonID startPolygon, NavPolygonID goalPolygon)
static NavMeshSourceGeometry Rasterize(Scene &scene, const NavMeshSourceGeometry &source, float cellSize, float agentRadius, float agentHeight, float maxSlopeAngle)
NavPolygonID FindPolygon(const glm::vec3 &position) const
NavPointPath CalculatePath(const glm::vec3 &startPosition, const glm::vec3 &destination) const
constexpr NavPolygonID InvalidNavPolygonID
Definition NavMesh.h:23
uint32_t NavVertexID
Definition NavMesh.h:17
uint32_t NavPolygonID
Definition NavMesh.h:18
std::vector< glm::vec3 > Vertices
std::vector< uint32_t > Indices
bool IsValid() const
std::vector< NavVertexID > Vertices
Definition NavMesh.h:44
glm::vec3 Position
Definition NavMesh.h:28