-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
165 lines (128 loc) · 4.41 KB
/
main.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
#include <SFML/Graphics.hpp>
#include <array>
#include <cmath>
using namespace sf;
const int kBoidsCount = 200;
const int kMaxDetectionRange = 200;
const int kShapeRadius = 3;
const int kMinDetectionRange = kShapeRadius + 1;
const float kTurnFactor = 0.5;
const float kAvoidFactor = 1;
const float kMatchingFactor = 0.05;
const float kCentringFactor = 0.005;
const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 700;
const int kMinSpeed = 3;
const int kMaxSpeed = 8;
struct Boid {
float x = 0, y = 0;
float vx = 0, vy = 0;
CircleShape shape;
Boid(int x, int y, int vx, int vy):
x(x), y(y), vx(vx), vy(vy),
shape(kShapeRadius)
{
shape.setFillColor(Color::White);
shape.setPosition(x, y);
}
Boid() = default;
};
void spawnBoids(std::array<Boid, kBoidsCount> &boids) {
for(auto &boid : boids) {
int vx_sign = rand() % 2 == 0 ? 1 : -1;
int vy_sign = rand() % 2 == 0 ? 1 : -1;
boid = Boid {
rand() % SCREEN_WIDTH + 0,
rand() % SCREEN_HEIGHT + 0,
vx_sign * (rand() % (kMaxSpeed - kMinSpeed) + kMinSpeed),
vy_sign * (rand() % (kMaxSpeed - kMinSpeed) + kMinSpeed),
};
}
}
void checkBorders(Boid& boid) {
if (boid.x < 0) {
boid.vx += kTurnFactor;
} else if (boid.x > SCREEN_WIDTH) {
boid.vx -= kTurnFactor;
}
if (boid.y < 0) {
boid.vy += kTurnFactor;
} else if (boid.y > SCREEN_HEIGHT) {
boid.vy -= kTurnFactor;
}
}
int main() {
srand(time(NULL));
std::array<Boid, kBoidsCount> boids;
spawnBoids(boids);
RenderWindow window(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Boids C++");
Event event;
while(window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
for (int i = 0; i < kBoidsCount; i++) {
auto &boid = boids[i];
int avg_x = 0; int avg_y = 0;
int avg_vx = 0; int avg_vy = 0;
int flock_boids = 0;
int close_dx = 0;
int close_dy = 0;
for (int j = 0; j < kBoidsCount; j++) {
if (i != j) {
auto &otherBoid = boids[j];
int distance_x = boid.x - otherBoid.x;
int distance_y = boid.y - otherBoid.y;
int distance = sqrt(pow(distance_x, 2) + pow(distance_y, 2));
if (distance < kMaxDetectionRange) {
if (distance <= kMinDetectionRange) {
close_dx += boid.x - otherBoid.x;
close_dy += boid.y - otherBoid.y;
} else {
avg_x += otherBoid.x;
avg_y += otherBoid.y;
avg_vx += otherBoid.vx;
avg_vy += otherBoid.vy;
flock_boids += 1;
}
}
}
}
if (flock_boids > 0) {
avg_x /= flock_boids;
avg_y /= flock_boids;
avg_vx /= flock_boids;
avg_vy /= flock_boids;
boid.vx = (boid.vx +
(avg_x - boid.x) * kCentringFactor +
(avg_vx - boid.vx)* kMatchingFactor);
boid.vy = (boid.vy +
(avg_y - boid.y)*kCentringFactor +
(avg_vy - boid.vy)*kMatchingFactor);
}
boid.vx = boid.vx + (close_dx*kAvoidFactor);
boid.vy = boid.vy + (close_dy*kAvoidFactor);
float speed = sqrt(pow(boid.vx, 2) + pow(boid.vy, 2));
if (speed < kMinSpeed) {
boid.vx = (boid.vx/speed)*kMinSpeed;
boid.vy = (boid.vy/speed)*kMinSpeed;
}
if (speed > kMaxSpeed) {
boid.vx = (boid.vx/speed)*kMaxSpeed;
boid.vy = (boid.vy/speed)*kMaxSpeed;
}
checkBorders(boid);
boid.x = boid.x + boid.vx;
boid.y = boid.y + boid.vy;
boid.shape.setPosition(boid.x, boid.y);
}
window.clear();
for(auto &boid : boids) {
window.draw(boid.shape);
}
window.setVerticalSyncEnabled(true);
window.display();
}
}