-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteeringController.h
More file actions
96 lines (83 loc) · 2.86 KB
/
Copy pathSteeringController.h
File metadata and controls
96 lines (83 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <Engine/Source/Utility/Global.h>
#include <Engine/Source/Navigation/Pathfinder.h>
#include "Kinematic.h"
#include "Face.h"
#include "Separation.h"
#include "Arrive.h"
namespace KE_EDITOR
{
class AIMovement;
}
/*
[Description]
| SteeringController handles the Movement of an entity thru steering behaviors and with help of the Pathfinder class to naviate thru the world.
| It encapsulates all the movement calculations and a Enemy class for example can simply assign targets to this controller and it will handle the rest.
| friend class AIMovement is used to allow the editor to access the kinematic data of the entity, for example max acceleration or rotation speed.
*/
class SteeringController
{
friend class KE_EDITOR::AIMovement;
public:
SteeringController(Transform& aTransform, SharedKinematicData& aData) :
myKinematic(aTransform, aData),
myFace(this->myKinematic),
myArrive(this->myKinematic)
{
}
inline const Vector3f& GetVelocity() const { return myKinematic.velocity; }
inline void AssignPathfinder(KE::Pathfinder* aPathfinder) { myPathfinder = aPathfinder; }
inline void ApplySteering(const float aTimeDelta);
inline void UpdateSteering(const float aTimeDelta);
inline void MoveTowards(const Vector3f& aTarget);
inline void FaceTowards(const Vector3f& aTarget);
inline void ContrainToGround(const bool aContrain) { myContrainToGround = aContrain; }
inline void ClearLinearSteering()
{
mySteeringOutput.linear = {};
myKinematic.velocity = {};
}
private:
KE::Pathfinder* myPathfinder = nullptr;
std::vector<Vector2f> myPath;
SteeringOutput mySteeringOutput;
Kinematic myKinematic;
Arrive myArrive;
Face myFace;
bool myContrainToGround = true;
};
inline void SteeringController::UpdateSteering(const float aTimeDelta)
{
SteeringOutput result;
result += myArrive.GetSteering();
result += myFace.GetSteering(aTimeDelta);
mySteeringOutput = result;
}
inline void SteeringController::ApplySteering(const float aTimeDelta)
{
myKinematic.Update(mySteeringOutput, aTimeDelta);
// Set unit height position based on Navmesh triangle height.
if (myContrainToGround)
{
Vector3f position = myKinematic.transform.GetPosition();
position.y = myPathfinder->GetHeightByPos(position);
myKinematic.transform.SetPosition(position);
}
}
inline void SteeringController::MoveTowards(const Vector3f& aTarget)
{
Vector3f position = myKinematic.transform.GetPosition();
myPath = myPathfinder->FindPath_2D(position, aTarget);
if (myPath.size() > 0)
{
Vector2f nextWaypoint = myPath.back();
Vector2f goalDestination = myPath.front();
myFace.SetTarget(nextWaypoint);
myArrive.SetTarget(nextWaypoint);
myArrive.SetFinalTarget(goalDestination);
}
}
inline void SteeringController::FaceTowards(const Vector3f& aTarget)
{
myFace.SetTarget(aTarget);
}