NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
NavigationSystem.cpp
Go to the documentation of this file.
1
#include "
Engine/AI/Navigation/NavigationSystem.h
"
2
3
#include "
Engine/AI/Navigation/NavMeshGeometryExtractor.h
"
4
#include "
Engine/AI/Navigation/NavMeshVoxelizer.h
"
5
#include "
Engine/AI/Navigation/NavMeshPathfinder.h
"
6
#include "
Engine/Scene/Scene.h
"
7
8
#include <cmath>
9
#include <utility>
10
11
namespace
NoJob
12
{
13
namespace
14
{
15
constexpr
float
PointInsideEpsilon =
16
0.00001f;
17
}
18
19
bool
NavigationSystem::Bake
(
Scene
& scene)
20
{
21
NavMeshSourceGeometry
geometry =
NavMeshVoxelizer::Rasterize
(
22
scene,
NavMeshGeometryExtractor::Extract
(scene),
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
51
void
NavigationSystem::Clear
()
52
{
53
m_NavMesh =
NavMesh
{};
54
m_HasNavMesh =
false
;
55
++m_NavMeshVersion;
56
}
57
58
NavPolygonID
NavigationSystem::FindPolygon
(
59
const
glm::vec3& position)
const
60
{
61
if
(!m_HasNavMesh)
62
return
InvalidNavPolygonID
;
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
88
return
InvalidNavPolygonID
;
89
}
90
91
NavPointPath
NavigationSystem::CalculatePath
(
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 ==
107
InvalidNavPolygonID
||
108
goalPolygon ==
109
InvalidNavPolygonID
)
110
{
111
return
emptyPath;
112
}
113
114
const
NavPath
corridor =
115
NavMeshPathfinder::FindPath
(
116
m_NavMesh,
117
startPolygon,
118
goalPolygon);
119
120
if
(!corridor.
IsValid
())
121
return
emptyPath;
122
123
return
NavMeshFunnel::BuildPath
(
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.h
NavMeshGeometryExtractor declarations for the Navigation subsystem.
NavMeshPathfinder.h
NavMeshPathfinder declarations for the Navigation subsystem.
NavMeshVoxelizer.h
NavMeshVoxelizer declarations for the Navigation subsystem.
NavigationSystem.h
NavigationSystem declarations for the Navigation subsystem.
Scene.h
Scene declarations for the Scenes and Native Scripting subsystem.
NoJob::NavMeshFunnel::BuildPath
static NavPointPath BuildPath(const NavMesh &navMesh, const NavPath &corridor, const glm::vec3 &startPosition, const glm::vec3 &goalPosition)
Definition
NavMeshFunnel.cpp:15
NoJob::NavMeshGenerator::Generate
static NavMesh Generate(const NavMeshSourceGeometry &geometry, const NavMeshGenerationSettings &settings={})
Definition
NavMeshGenerator.cpp:13
NoJob::NavMeshGeometryExtractor::Extract
static NavMeshSourceGeometry Extract(Scene &scene)
Definition
NavMeshGeometryExtractor.cpp:19
NoJob::NavMesh
Definition
NavMesh.h:53
NoJob::NavMesh::GetVertex
const NavVertex * GetVertex(NavVertexID id) const
Definition
NavMesh.cpp:127
NoJob::NavMesh::GetPolygonCount
size_t GetPolygonCount() const
Definition
NavMesh.h:69
NoJob::NavMeshPathfinder::FindPath
static NavPath FindPath(const NavMesh &navMesh, NavPolygonID startPolygon, NavPolygonID goalPolygon)
Definition
NavMeshPathfinder.cpp:35
NoJob::NavMeshVoxelizer::Rasterize
static NavMeshSourceGeometry Rasterize(Scene &scene, const NavMeshSourceGeometry &source, float cellSize, float agentRadius, float agentHeight, float maxSlopeAngle)
Definition
NavMeshVoxelizer.cpp:24
NoJob::NavigationSystem::Bake
bool Bake(Scene &scene)
Definition
NavigationSystem.cpp:19
NoJob::NavigationSystem::FindPolygon
NavPolygonID FindPolygon(const glm::vec3 &position) const
Definition
NavigationSystem.cpp:58
NoJob::NavigationSystem::CalculatePath
NavPointPath CalculatePath(const glm::vec3 &startPosition, const glm::vec3 &destination) const
Definition
NavigationSystem.cpp:91
NoJob::NavigationSystem::Clear
void Clear()
Definition
NavigationSystem.cpp:51
NoJob::Scene
Definition
Scene.h:31
NoJob
Definition
BTActionNode.h:13
NoJob::InvalidNavPolygonID
constexpr NavPolygonID InvalidNavPolygonID
Definition
NavMesh.h:23
NoJob::NavVertexID
uint32_t NavVertexID
Definition
NavMesh.h:17
NoJob::NavPolygonID
uint32_t NavPolygonID
Definition
NavMesh.h:18
NoJob::NavMeshGenerationSettings
Definition
NavMeshGenerator.h:18
NoJob::NavMeshGenerationSettings::MergePolygons
bool MergePolygons
Definition
NavMeshGenerator.h:20
NoJob::NavMeshGenerationSettings::VertexWeldTolerance
float VertexWeldTolerance
Definition
NavMeshGenerator.h:24
NoJob::NavMeshSourceGeometry
Definition
NavMeshGenerator.h:32
NoJob::NavMeshSourceGeometry::Vertices
std::vector< glm::vec3 > Vertices
Definition
NavMeshGenerator.h:33
NoJob::NavMeshSourceGeometry::Indices
std::vector< uint32_t > Indices
Definition
NavMeshGenerator.h:34
NoJob::NavPath
Definition
NavMeshPathfinder.h:17
NoJob::NavPath::IsValid
bool IsValid() const
Definition
NavMeshPathfinder.h:20
NoJob::NavPointPath
Definition
NavMeshFunnel.h:18
NoJob::NavPolygon
Definition
NavMesh.h:43
NoJob::NavPolygon::Vertices
std::vector< NavVertexID > Vertices
Definition
NavMesh.h:44
NoJob::NavVertex
Definition
NavMesh.h:27
NoJob::NavVertex::Position
glm::vec3 Position
Definition
NavMesh.h:28
Engine
AI
Navigation
NavigationSystem.cpp
Generated by
1.18.0