Plasma Engine  2.0
Loading...
Searching...
No Matches
Navigation.h
1#pragma once
2
3#include <AiPlugin/Navigation/NavMesh.h>
4#include <DetourNavMeshQuery.h>
5#include <DetourPathCorridor.h>
6#include <Foundation/Math/Angle.h>
7#include <Foundation/Math/Vec3.h>
8
10
13{
14 plVec3 m_vNextWaypoint;
15 float m_fDistanceToWaypoint = 0;
16 float m_fArrivalDistance = plMath::HighValue<float>();
17 plVec2 m_vDirectionTowardsWaypoint = plVec2::MakeZero();
18 plAngle m_AbsRotationTowardsWaypoint = plAngle::MakeZero();
19 plAngle m_MaxAbsRotationAfterWaypoint = plAngle::MakeZero();
20 // float m_fWaypointCorridorWidth = plMath::HighValue<float>();
21};
22
40class PL_AIPLUGIN_DLL plAiNavigation final
41{
42public:
45
46 enum class State
47 {
48 Idle,
49 StartNewSearch,
50 InvalidCurrentPosition,
51 InvalidTargetPosition,
52 NoPathFound,
53 PartialPathFound,
54 FullPathFound,
55 Searching,
56 };
57
58 static constexpr plUInt32 MaxPathNodes = 64;
59 static constexpr plUInt32 MaxSearchNodes = MaxPathNodes * 8;
60
61 State GetState() const { return m_State; }
62
63 void Update();
64
65 void CancelNavigation();
66
67 void SetCurrentPosition(const plVec3& vPosition);
68 void SetTargetPosition(const plVec3& vPosition);
69 const plVec3& GetTargetPosition() const;
70 void SetNavmesh(plAiNavMesh* pNavmesh);
71 void SetQueryFilter(const dtQueryFilter& filter);
72
73 void ComputeAllWaypoints(plDynamicArray<plVec3>& out_waypoints) const;
74
75 void DebugDrawPathCorridor(const plDebugRendererContext& context, plColor tilesColor, float fPolyRenderOffsetZ = 0.1f);
76 void DebugDrawPathLine(const plDebugRendererContext& context, plColor straightLineColor, float fLineRenderOffsetZ = 0.2f);
77 void DebugDrawState(const plDebugRendererContext& context, const plVec3& vPosition) const;
78
79
81 float GetCurrentElevation() const;
82
83 void ComputeSteeringInfo(plAiSteeringInfo& out_info, const plVec2& vForwardDir, float fMaxLookAhead = 5.0f);
84
85 // in what radius / up / down distance navigation mesh polygons should be searched around a given position
86 // this should relate to the character size, ie at least the character radius
87 // otherwise a character that barely left the navmesh area may not know where it is, anymore
88 float m_fPolySearchRadius = 0.5f;
89 float m_fPolySearchUp = 1.5f;
90 float m_fPolySearchDown = 1.5f;
91
92 // when a path search is started, all tiles in a rectangle around the start and end point are loaded first
93 // this is the amount to increase that rectangle size, to overestimate which sectors may be needed during the path search
94 constexpr static float c_fPathSearchBoundary = 10.0f;
95
96
97private:
98 State m_State = State::Idle;
99
100 plVec3 m_vCurrentPosition = plVec3::MakeZero();
101 plVec3 m_vTargetPosition = plVec3::MakeZero();
102
103 plUInt8 m_uiCurrentPositionChangedBit : 1;
104 plUInt8 m_uiTargetPositionChangedBit : 1;
105 plUInt8 m_uiEnvironmentChangedBit : 1;
106 plUInt8 m_uiReinitQueryBit : 1;
107
108 plAiNavMesh* m_pNavmesh = nullptr;
109 dtNavMeshQuery m_Query;
110 const dtQueryFilter* m_pFilter = nullptr;
111 dtPathCorridor m_PathCorridor;
112
113 dtPolyRef m_PathSearchTargetPoly;
114 plVec3 m_vPathSearchTargetPos;
115
116 plUInt8 m_uiOptimizeTopologyCounter = 0;
117 plUInt8 m_uiOptimizeVisibilityCounter = 0;
118
119 bool UpdatePathSearch();
120};
121
122struct PL_AIPLUGIN_DLL plAiNavmeshRaycastHit
123{
124 plVec3 m_vHitPosition;
125 float m_fHitDistanceNormalized;
126 float m_fHitDistance;
127};
128
129class PL_AIPLUGIN_DLL plAiNavmeshQuery
130{
131public:
133
134 void SetNavmesh(plAiNavMesh* pNavmesh);
135 void SetQueryFilter(const dtQueryFilter& filter);
136
137 bool PrepareQueryArea(const plVec3& vCenter, float fRadius);
138 bool Raycast(const plVec3& vStart, const plVec3& vDir, float fDistance, plAiNavmeshRaycastHit& out_raycastHit);
139
140private:
141 plUInt8 m_uiReinitQueryBit : 1;
142
143 plAiNavMesh* m_pNavmesh = nullptr;
144 dtNavMeshQuery m_Query;
145 const dtQueryFilter* m_pFilter = nullptr;
146};
A navmesh generated with a specific configuration.
Definition NavMesh.h:70
Computes a path through a navigation mesh.
Definition Navigation.h:41
Definition Navigation.h:130
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeZero()
Returns a zero initialized angle. Same as a default constructed object.
Definition Angle.h:31
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
Used in plDebugRenderer to determine where debug geometry should be rendered.
Definition DebugRendererContext.h:11
Definition DynamicArray.h:81
static constexpr plVec2Template< float > MakeZero()
Definition Vec2.h:49
static plVec3Template< float > MakeZero()
Definition Vec3.h:38
constexpr TYPE HighValue()
A very large value, that is slightly smaller than sqrt(MaxValue()).
Definition Navigation.h:123
Aggregated data by plAiNavigation that should be sufficient to implement a steering behavior.
Definition Navigation.h:13