AireEngine
A 3D Open-World Game Engine
Loading...
Searching...
No Matches
ai.h
1#pragma once
2
3#include <vector>
4#include <glm/glm.hpp>
5
6#include "../res-management/model.h"
7#include "../res-management/terrain.h"
8#include "../res-management/animator.h"
9#include "../utils/rigidbody.h"
10
11namespace Aire {
15 class Boid
16 {
17 private:
18 glm::vec2 initialDirection = { 1.f, 0.f };
19 glm::vec2 targetDirection;
21 Terrain* terrain;
23 void randomizePosition(Terrain* terrain, RigidbodyHandler* rigidbodyHandler) {
24 float scale = terrain->getXZScale() * 0.9f;
25 glm::vec2 corner_min = glm::vec2(-scale, -scale);
26 float width = 2 * scale;
27 bool collision = false;
28 do {
29 collision = false;
30 float x = (float)rand() / RAND_MAX * width + corner_min.x;
31 float z = (float)rand() / RAND_MAX * width + corner_min.y;
32 this->currentPosition = { x, z };
33 } while (collision);
34 }
35
36 void randomizeDirection() {
37 this->currentDirection = normalize(glm::vec2{ (float)rand() / RAND_MAX, (float)rand() / RAND_MAX });
38 }
39
40 public:
41 glm::vec2 currentDirection;
42 glm::vec2 currentPosition;
43 glm::mat4 model2world;
52 Boid(Terrain* terrain, RigidbodyHandler* rigidbodyHandler) {
53 this->terrain = terrain;
54 // Spawn boid at random position and direction
55 randomizePosition(terrain, rigidbodyHandler);
56 randomizeDirection();
57 this->targetDirection = currentDirection;
58 // Initial rotation for object
59 float rotationAngle = acos(dot(this->initialDirection, this->currentDirection));
60 // Initial translation for object
61 glm::vec3 translation = glm::vec3(this->currentPosition.x,
62 terrain->getHeightAt(this->currentPosition.x, this->currentPosition.y), this->currentPosition.y);
63
64 this->model2world = glm::translate(glm::mat4(1.0f), translation) * glm::rotate(glm::mat4(1.0f), rotationAngle + 90, glm::vec3(0.f, 1.f, 0.f));
65 };
66
72 void setTargetDirection(glm::vec2 direction) {
73 targetDirection = direction;
74 }
75
82 void updateDirection(float speed, float transition);
83
93 std::vector<Boid*> findNeighbours(std::vector<Boid*> totalBoids, float radius, float visionAngle);
94
95
104 glm::vec2 applyCohesion(std::vector<Boid*>neighbours, float strength) const;
105
114 glm::vec2 applyAlignment(std::vector<Boid*>neighbours, float strength) const;
115
125 glm::vec2 applySeparation(std::vector<Boid*> neighbours, float strength, float radius) const;
126
134 glm::vec2 avoidEdges(float strength);
135
144 glm::vec2 avoidObstacles(RigidbodyHandler* rigidbodyHandler, float strength);
145
152 void setRigidbody(float radius, glm::vec3 pos);
153 };
154
155
160 public:
168 BoidManager(Model* model, Terrain* terrain, RigidbodyHandler* rigidbodyHandler) {
169 this->model = model;
170 model->isStatic = false;
171 this->animator = nullptr;
172 this->terrain = terrain;
173 this->rigidbodyHandler = rigidbodyHandler;
174 }
182 BoidManager(Animator* animator, Terrain* terrain, RigidbodyHandler* rigidbodyHandler) {
183 this->model = nullptr;
184 animator->isStatic = false;
185 this->animator = animator;
186 this->terrain = terrain;
187 this->rigidbodyHandler = rigidbodyHandler;
188 }
189
194 for (auto boid : boids) {
195 delete boid;
196 }
197 }
198
204 Terrain* getTerrain() { return terrain; }
205
211 void setTrackPlayer(bool trackPlayer) { this->trackPlayer = trackPlayer; }
212
216 void addBoid();
217
223 void addBoids(int numBoids);
224
228 std::vector<Boid*>& getBoids() { return boids; }
229
233 void setModel() { this->model = model; this->animator = nullptr; }
234
238 Model* getModel() { return model; }
239
243 bool hasModel() { return model != nullptr; }
244
248 void setAnimator() { this->model = nullptr; this->animator = animator; }
249
253 Animator* getAnimator() { return animator; }
254
258 bool hasAnimator() { return animator != nullptr; }
259
266 void updateBoids(float deltaTime, glm::vec3 playerPosition);
267
274 void renderBoids(ShaderProgram& shader, Aire::Camera& camera);
275
276 private:
277 Model* model;
278 Animator* animator;
279 Terrain* terrain;
280 RigidbodyHandler* rigidbodyHandler;
281 std::vector<Boid*> boids;
282 bool trackPlayer = false;
284 float boidVisionRange = 6.f;
285 float boidVisionAngle = 150.f;
286 float cohesionStrength = 1.f;
287 float alignmentStrength = 1.f;
288 float separationStrength = 3.f;
289 float boidSpeed = 5.f;
290 };
291}
Class for playing the animation.
Definition animator.h:18
bool isStatic
Flag indicating whether the model is static.
Definition animator.h:20
A Boid that can swarm in a flock.
Definition ai.h:16
glm::vec2 applySeparation(std::vector< Boid * > neighbours, float strength, float radius) const
Creates a direction vector away from the neighbouring boids.
Definition ai.cpp:101
glm::vec2 applyCohesion(std::vector< Boid * >neighbours, float strength) const
Creates a direction vector towards the average position of the neighbouring boids.
Definition ai.cpp:74
glm::vec2 avoidObstacles(RigidbodyHandler *rigidbodyHandler, float strength)
Creates a direction vector away from the obstacles in the simulation space.
Definition ai.cpp:151
std::vector< Boid * > findNeighbours(std::vector< Boid * > totalBoids, float radius, float visionAngle)
Finds the neighbouring boids within a given radius and vision angle.
Definition ai.cpp:58
Boid(Terrain *terrain, RigidbodyHandler *rigidbodyHandler)
Create a new boid.
Definition ai.h:52
void updateDirection(float speed, float transition)
Updates the direction of the boid towards the target direction.
Definition ai.cpp:29
glm::vec2 currentDirection
Definition ai.h:41
glm::vec2 avoidEdges(float strength)
Creates a direction vector away from the edges of the simulation space.
Definition ai.cpp:127
void setTargetDirection(glm::vec2 direction)
Setter for targetDirection.
Definition ai.h:72
glm::mat4 model2world
Definition ai.h:43
glm::vec2 applyAlignment(std::vector< Boid * >neighbours, float strength) const
Creates a direction vector towards the average direction of the neighbouring boids.
Definition ai.cpp:88
glm::vec2 currentPosition
Definition ai.h:42
void setRigidbody(float radius, glm::vec3 pos)
Sets the rigidbody of the boid.
Definition ai.cpp:161
Rigidbody * rigidbody
Definition ai.h:44
Manager for the boids.
Definition ai.h:159
Animator * getAnimator()
Definition ai.h:253
std::vector< Boid * > & getBoids()
Definition ai.h:228
void setModel()
Manually set the model for the boids.
Definition ai.h:233
bool hasAnimator()
Definition ai.h:258
Model * getModel()
Definition ai.h:238
void setAnimator()
Manually set the animator for the boids.
Definition ai.h:248
bool hasModel()
Definition ai.h:243
void addBoid()
Add a new boid to the list of boids.
Definition ai.cpp:166
void updateBoids(float deltaTime, glm::vec3 playerPosition)
Update the position of the boids.
Definition ai.cpp:180
BoidManager(Model *model, Terrain *terrain, RigidbodyHandler *rigidbodyHandler)
Create a new boid manager.
Definition ai.h:168
void setTrackPlayer(bool trackPlayer)
Make the boids track the player.
Definition ai.h:211
Terrain * getTerrain()
Get the terrain associated with the boids.
Definition ai.h:204
void renderBoids(ShaderProgram &shader, Aire::Camera &camera)
Render all boids.
Definition ai.cpp:207
~BoidManager()
Destroy all boids.
Definition ai.h:193
void addBoids(int numBoids)
Add a nmber of new boids.
Definition ai.cpp:174
BoidManager(Animator *animator, Terrain *terrain, RigidbodyHandler *rigidbodyHandler)
Create a new boid manager.
Definition ai.h:182
Definition camera.h:9
Definition Model.h:30
bool isStatic
Flag indicating whether the model is static.
Definition Model.h:32
A singleton class used to handle all rigid bodies in the scene.
Definition rigidbodyHandler.h:13
Definition rigidbody.h:8
A linked OpenGL program.
Definition shaderProgram.h:16
A class that represents a terrain object.
Definition terrain.h:28
float getXZScale() const
Gets the terrain scale factor in the x and z axes.
Definition terrain.h:84
float getHeightAt(const float x, const float z)
Retrieves the height of the terrain at a given point in world space.
Definition terrain.cpp:114