-
Notifications
You must be signed in to change notification settings - Fork 0
/
flock.cpp
264 lines (198 loc) · 5.42 KB
/
flock.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "boid.hpp"
#include "flock.hpp"
#include "landscape.hpp"
#include "landscapescene.hpp"
#include "simulation.hpp"
#include <math.h>
#define DISTANCE_AVOID 20
Flock::Flock(
Landscape *landscapeView, LandscapeScene *landscapeScene,
int moveWeight, int matchWeight,
int avoidWeight, int targetWeight,
Simulation *sim
)
{
landscapeView_ = landscapeView;
landscapeScene_ = landscapeScene;
moveWeight_ = moveWeight;
matchWeight_ = matchWeight;
avoidWeight_ = avoidWeight;
targetWeight = targetWeight;
sim_ = sim;
}
Flock::~Flock()
{}
void Flock::createBoids(int numBoids)
{
for (int boidCount = 0; boidCount < numBoids; boidCount++)
{
Boid *boid = new Boid( boidCount, landscapeView_->getRandomPoint());
boids.push_back( boid );
landscapeScene_->addItem( boid );
}
}
void Flock::addBoid(Boid *newBoid)
{
boids.push_back(newBoid);
}
Boid* Flock::spawnBoid()
{
QPointF centre = centreOfFlock();
Boid *boid = new Boid(numberOfBoids(), centreOfFlock());
addBoid(boid);
return boid;
}
void Flock::removeBoid()
{
int flockSize = numberOfBoids();
if ( flockSize > 2 ) {
Boid *boid = boids.at( 0 );
landscapeScene_->removeItem( boid );
boids.remove( 0 );
boids.resize( flockSize - 1 );
delete boid;
}
}
int Flock::numberOfBoids()
{
return boids.size();
}
QVector<Boid*> Flock::getBoids()
{
return boids;
}
void Flock::updateBoids(Flock *flockToAvoid, int ticksOffset)
{
foreach ( Boid *boid, boids )
{
QPointF v1 = moveWeight_ * moveTowardsCentre(boid);
QPointF v2 = avoidWeight_ * avoidOtherBoids(boid);
QPointF v3 = matchWeight_ * matchVelocity(boid);
QPointF v4 = avoidWeight_ * avoidOtherFlock(boid, flockToAvoid);
boid->setVelocity( (boid->velocity() + v1 + v2 + v3 + v4 ) );
boundBoid(boid);
boid->limitVelocity();
boid->setPos( boid->pos() + boid->velocity() );
boid->setMsSinceStart( sim_->ticksSinceStart() + ticksOffset);
boid->addToTrail( boid->x(), boid->y() );
}
}
QPointF Flock::moveTowardsCentre(Boid *thisBoid)
{
return (centreOfFlock() - thisBoid->pos()) / 100;
}
QPointF Flock::centreOfFlock()
{
QPointF flockCentre(0, 0);
foreach (Boid *boid, boids)
{
flockCentre += boid->pos();
}
return flockCentre /= (numberOfBoids());
}
/**
* Algorithm based on the pseudocode at:
* http://www.vergenet.net/~conrad/boids/pseudocode.html
*/
QPointF Flock::avoidOtherBoids(Boid *thisBoid)
{
QPointF avoidVec( 0, 0 );
Boid *boid;
foreach (boid, boids)
{
if (boid != thisBoid)
{
if (distanceBetween(boid, thisBoid) < DISTANCE_AVOID )
{
avoidVec -= (boid->pos() - thisBoid->pos());
}
}
}
return avoidVec;
}
float Flock::distanceBetween( Boid* boid1, Boid* boid2 )
{
float xDiff, yDiff, magnitude;
xDiff = boid1->pos().x() - boid2->pos().x();
yDiff = boid1->pos().y() - boid2->pos().y();
magnitude = sqrt( pow(xDiff,2) + pow(yDiff,2) );
return magnitude;
}
/**
* Algorithm based on the pseudocode at:
* http://www.vergenet.net/~conrad/boids/pseudocode.html
*/
QPointF Flock::matchVelocity(Boid *thisBoid)
{
QPointF averageVel(0, 0);
Boid *boid;
foreach(boid, boids)
{
if (boid != thisBoid)
{
averageVel += boid->velocity();
}
}
averageVel /= (numberOfBoids() - 1);
return (averageVel - thisBoid->velocity()) / 8;
}
QPointF Flock::avoidOtherFlock(Boid *thisBoid, Flock *flockToAvoid)
{
QPointF avoidVec( 0, 0 );
Boid *boid;
float xDiff, yDiff, magnitude;
foreach (boid, flockToAvoid->getBoids())
{
if (boid != thisBoid)
{
xDiff = boid->pos().x() - thisBoid->pos().x();
yDiff = boid->pos().y() - thisBoid->pos().y();
magnitude = sqrt( pow(xDiff,2) + pow(yDiff,2) );
if (magnitude < 100) {
avoidVec -= (boid->pos() - thisBoid->pos());
}
}
}
return avoidVec*10;
}
/**
* Algorithm based on the pseudocode at:
* http://www.vergenet.net/~conrad/boids/pseudocode.html
*/
void Flock::boundBoid(Boid *boid)
{
int xmin = 0;
int xmax = landscapeView_->viewport()->width();
int ymin = 0;
int ymax = landscapeView_->viewport()->height();
if ( !landscapeScene_->target().isNull() )
boid->setVelocity( boid->velocity() + targetWeight_*( landscapeScene_->target() - boid->pos() )/100 );
if ( landscapeView_->mapFromScene( boid->scenePos() ).x() > xmax ) {
boid->setVelocityX( boid->velocity().x() * -10 );
}
if ( landscapeView_->mapFromScene( boid->scenePos() ).x() < xmin ) {
boid->setVelocityX( boid->velocity().x() * -10 );
}
if ( landscapeView_->mapFromScene( boid->scenePos() ).y() > ymax ) {
boid->setVelocityY( boid->velocity().y() * -10 );
}
if ( landscapeView_->mapFromScene( boid->scenePos() ).y() < ymin ) {
boid->setVelocityY( boid->velocity().y() * -10 );
}
}
void Flock::setMoveWeight(int newWeight)
{
moveWeight_ = newWeight;
}
void Flock::setMatchWeight(int newWeight)
{
matchWeight_ = newWeight;
}
void Flock::setAvoidWeight(int newWeight)
{
avoidWeight_ = newWeight;
}
void Flock::setTargetWeight(int newWeight)
{
targetWeight_ = newWeight;
}