NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavAgent.cpp
Go to the documentation of this file.
2
4
5#include <algorithm>
6#include <utility>
7
8namespace NoJob
9{
11 const NavigationSystem& navigationSystem,
12 const glm::vec3& currentPosition,
13 const glm::vec3& destination)
14 {
15 NavPointPath newPath =
16 navigationSystem.CalculatePath(
17 currentPosition,
18 destination);
19
20 if (!newPath.IsValid())
21 {
22 Stop();
23 return false;
24 }
25
26 m_Path = std::move(newPath);
27 m_Destination = destination;
28
29 m_CurrentWaypoint =
30 m_Path.Points.size() > 1 ? 1 : 0;
31
32 m_ReachedDestination = false;
33
34 return true;
35 }
36
38 const NavigationSystem& navigationSystem,
39 const glm::vec3& currentPosition)
40 {
41 if (m_ReachedDestination || !HasPath())
42 return false;
43
44 NavPointPath newPath =
45 navigationSystem.CalculatePath(
46 currentPosition,
47 m_Destination);
48
49 if (!newPath.IsValid())
50 return false;
51
52 // Commit only after successful path calculation.
53 m_Path = std::move(newPath);
54
55 m_CurrentWaypoint =
56 m_Path.Points.size() > 1 ? 1 : 0;
57
58 return true;
59 }
61 const glm::vec3& currentPosition,
62 float deltaTime)
63 {
64 if (!HasPath() ||
65 deltaTime <= 0.0f)
66 {
67 return glm::vec3(0.0f);
68 }
69
70 while (m_CurrentWaypoint <
71 m_Path.Points.size())
72 {
73 const glm::vec3 target =
74 m_Path.Points[
75 m_CurrentWaypoint];
76
77 glm::vec3 toTarget =
78 target -
79 currentPosition;
80
81 toTarget.y = 0.0f;
82
83 const float distance =
84 glm::length(toTarget);
85
86 const bool isLastWaypoint =
87 m_CurrentWaypoint + 1 >=
88 m_Path.Points.size();
89
90 // Intermediate waypoints must be reached accurately.
91 // Using the final stopping distance here makes agents
92 // cut corners and potentially collide with walls.
93 constexpr float WaypointTolerance = 0.10f;
94 const float tolerance = isLastWaypoint
95 ? std::max(m_StoppingDistance, 0.0f)
96 : WaypointTolerance;
97
98 if (distance <= tolerance)
99 {
100 if (isLastWaypoint)
101 {
102 m_CurrentWaypoint =
103 m_Path.Points.size();
104
105 m_ReachedDestination =
106 true;
107
108 return glm::vec3(0.0f);
109 }
110
111 ++m_CurrentWaypoint;
112 continue;
113 }
114
115 const glm::vec3 direction =
116 toTarget /
117 distance;
118
119 const float maxMovement =
120 std::max(m_Speed, 0.0f) *
121 deltaTime;
122
123 const float movementDistance =
124 std::min(
125 maxMovement,
126 distance);
127
128 return
129 direction *
130 movementDistance;
131 }
132
133 m_ReachedDestination = true;
134
135 return glm::vec3(0.0f);
136 }
137
139 {
140 m_Path.Clear();
141
142 m_CurrentWaypoint = 0;
143 m_ReachedDestination = true;
144 }
145}
NavAgent declarations for the Navigation subsystem.
NavigationSystem declarations for the Navigation subsystem.
glm::vec3 Update(const glm::vec3 &currentPosition, float deltaTime)
Definition NavAgent.cpp:60
bool SetDestination(const NavigationSystem &navigationSystem, const glm::vec3 &currentPosition, const glm::vec3 &destination)
Definition NavAgent.cpp:10
bool RecalculatePath(const NavigationSystem &navigationSystem, const glm::vec3 &currentPosition)
Definition NavAgent.cpp:37
bool HasPath() const
Definition NavAgent.h:36
NavPointPath CalculatePath(const glm::vec3 &startPosition, const glm::vec3 &destination) const
bool IsValid() const