-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoids.cpp
215 lines (186 loc) · 5.7 KB
/
Boids.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "Boids.h"
#include <cmath>
#define PI 3.14159265
int ReturnIntRandom(int lower, int upper)
{
return (rand() % (upper - lower + 1)) + lower;
}
float ReturnFloatRandom(float lower, float upper)
{
float r3 = lower + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (upper - lower)));
}
Boids::Boids()
{
texture.loadFromFile("arrow.png");
sprite.setTexture(texture);
sprite.setOrigin(sf::Vector2f(texture.getSize().x/2,texture.getSize().y/2));
sprite.setScale(0.02f, 0.02f);
position = Vector2f(ReturnIntRandom(0, 800), ReturnIntRandom(0, 500));
sprite.setPosition(position.ConverttoSF());
auto pi = 4.0 * atan(1.0);
auto phi = rand() / (double)(RAND_MAX);
phi *= 2.0 * pi;
velocity = Vector2f(sin(phi), cos(phi));
velocity = velocity.setMag(ReturnIntRandom(2, 4));
acceleration = Vector2f(0.0f, 0.0f);
maxForce = 1.5f;
maxSpeed = 5.0f;
}
void Boids::AvoidEdges(const int& HEIGHT, const int& WIDTH)
{
if (position.x > WIDTH)
{
position.x = 0;
}
else if (position.x < 0)
{
position.x = WIDTH;
}
if (position.y > HEIGHT)
{
position.y = 0;
}
else if (position.y < 0)
{
position.y = HEIGHT;
}
}
template <typename std::size_t S>
Vector2f Boids::align(Boids (&otherBoids)[S], int alignmentRadius)
{
int perceptionRadius = alignmentRadius;
Vector2f steering = Vector2f(0.f, 0.f);
int total = 0;
for (int i = 0; i < sizeof(otherBoids) / sizeof(otherBoids[0]); i++)
{
auto distance = position.dist(otherBoids[i].position);
if (*this != otherBoids[i] && distance < perceptionRadius)
{
steering = steering.add(otherBoids[i].velocity);
total++;
}
}
if (total > 0)
{
steering = steering.divide(total);
steering = steering.setMag(maxSpeed);
steering = steering.sub(velocity);
steering = steering.limit(maxForce);
}
return steering;
}
template <typename std::size_t S>
Vector2f Boids::separation(Boids (&otherBoids)[S], int separationRadius)
{
int perceptionRadius = separationRadius;
Vector2f steering = Vector2f(0.f, 0.f);
int total = 0;
for (int i = 0; i < sizeof(otherBoids) / sizeof(otherBoids[0]); i++)
{
float distance = position.dist(otherBoids[i].position);
if ((*this != otherBoids[i]) && (distance < perceptionRadius) && (distance > 0.0f))
{
Vector2f diff = position.sub(otherBoids[i].position);
if (distance < 1.0f)
diff = diff.mult(1 / (distance * distance));
steering = steering.add(diff);
total++;
}
}
if (total > 0)
{
steering = steering.divide(total);
steering = steering.setMag(maxSpeed);
steering = steering.sub(velocity);
steering = steering.limit(maxForce);
}
return steering;
}
Vector2f Boids::collision(std::vector<sf::CircleShape> &shape)
{
int perceptionRadius = 50;
Vector2f steering = Vector2f(0.f, 0.f);
int total = 0;
for (int i = 0; i < shape.size(); i++)
{
float distance = position.dist(Vector2f(shape[i].getPosition().x, shape[i].getPosition().y));
if ((distance < perceptionRadius) && (distance > 0.0f))
{
Vector2f diff = position.sub(Vector2f(shape[i].getPosition().x, shape[i].getPosition().y));
if (distance < 1.0f)
diff = diff.mult(1 / (distance * distance));
steering = steering.add(diff);
total++;
}
}
if (total > 0)
{
steering = steering.divide(total);
steering = steering.setMag(maxSpeed);
steering = steering.sub(velocity);
steering = steering.limit(maxForce);
}
return steering;
}
template <typename std::size_t S>
Vector2f Boids::cohesion(Boids (&otherBoids)[S], int cohesionRadius)
{
int perceptionRadius = cohesionRadius;
Vector2f steering = Vector2f(0.f, 0.f);
int total = 0;
for (int i = 0; i < sizeof(otherBoids) / sizeof(otherBoids[0]); i++)
{
auto distance = position.dist(otherBoids[i].position);
if (*this != otherBoids[i] && distance < perceptionRadius)
{
steering = steering.add(otherBoids[i].position);
total++;
}
}
if (total > 0)
{
steering = steering.divide(total);
steering = steering.sub(position);
steering = steering.setMag(maxSpeed);
steering = steering.sub(velocity);
steering = steering.limit(maxForce);
}
return steering;
}
template < std::size_t S>
void Boids::flock(Boids (&otherBoids)[S], std::vector<sf::CircleShape> &shape, int alignmentRadius, int cohesionRadius, int separationRadius)
{
Vector2f alignment = align(otherBoids, alignmentRadius);
Vector2f cohes = cohesion(otherBoids, cohesionRadius);
Vector2f sep = separation(otherBoids, separationRadius);
Vector2f col = collision(shape);
acceleration = acceleration.add(alignment);
acceleration = acceleration.add(cohes);
acceleration = acceleration.add(sep);
acceleration = acceleration.add(col);
}
void Boids::update()
{
position = position.add(velocity);
velocity = velocity.add(acceleration);
velocity = velocity.limit(maxSpeed);
acceleration = acceleration.mult(0);
float angle = atan2(velocity.y, velocity.x);
angle = angle * (180 / PI) - 90;
angle = (velocity.x < 0.0f || velocity.y < 0.0f) ? angle - 180 : angle + 180;
sprite.setRotation(angle);
sprite.setPosition(position.ConverttoSF());
}
void Boids::draw(sf::RenderWindow &window)
{
window.draw(sprite);
}
bool operator==(const Boids &a, const Boids &b)
{
return (a.position.x == b.position.x) && (a.position.y == b.position.y) && (a.velocity.x == b.velocity.x) && (a.velocity.y == b.velocity.y) && (a.acceleration.x == b.acceleration.x) && (a.acceleration.y == b.acceleration.y);
}
bool operator!=(const Boids &a, const Boids &b)
{
return !(a == b);
}
template void Boids::flock(Boids (&otherBoids)[200], std::vector<sf::CircleShape> &shape, int alignmentRadius, int cohesionRadius, int separationRadius);