6#include "../res-management/model.h"
7#include "../res-management/terrain.h"
8#include "../res-management/animator.h"
9#include "../utils/rigidbody.h"
18 glm::vec2 initialDirection = { 1.f, 0.f };
19 glm::vec2 targetDirection;
25 glm::vec2 corner_min = glm::vec2(-scale, -scale);
26 float width = 2 * scale;
27 bool collision =
false;
30 float x = (float)rand() / RAND_MAX * width + corner_min.x;
31 float z = (float)rand() / RAND_MAX * width + corner_min.y;
36 void randomizeDirection() {
37 this->
currentDirection = normalize(glm::vec2{ (float)rand() / RAND_MAX, (float)rand() / RAND_MAX });
53 this->terrain = terrain;
55 randomizePosition(terrain, rigidbodyHandler);
59 float rotationAngle = acos(dot(this->initialDirection, this->currentDirection));
61 glm::vec3 translation = glm::vec3(this->currentPosition.x,
62 terrain->
getHeightAt(this->currentPosition.x, this->currentPosition.y), this->currentPosition.y);
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));
73 targetDirection = direction;
93 std::vector<Boid*>
findNeighbours(std::vector<Boid*> totalBoids,
float radius,
float visionAngle);
104 glm::vec2
applyCohesion(std::vector<Boid*>neighbours,
float strength)
const;
114 glm::vec2
applyAlignment(std::vector<Boid*>neighbours,
float strength)
const;
125 glm::vec2
applySeparation(std::vector<Boid*> neighbours,
float strength,
float radius)
const;
171 this->animator =
nullptr;
172 this->terrain = terrain;
173 this->rigidbodyHandler = rigidbodyHandler;
183 this->model =
nullptr;
185 this->animator = animator;
186 this->terrain = terrain;
187 this->rigidbodyHandler = rigidbodyHandler;
194 for (
auto boid : boids) {
233 void setModel() { this->model = model; this->animator =
nullptr; }
248 void setAnimator() { this->model =
nullptr; this->animator = animator; }
266 void updateBoids(
float deltaTime, glm::vec3 playerPosition);
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;
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
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
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