-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flock.cpp
73 lines (61 loc) · 2.29 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
#include <vector>
#include "Flock.hpp"
#include "Mobile.hpp"
#include "Phantom.hpp"
#include "Triple.hpp"
Flock::Flock(std::string name, Mobile *character, double targetRadius, double slowRadius, double flockRadius, double maxAcceleration):
KinematicV(name),
character(character),
targetRadius(targetRadius),
slowRadius(slowRadius),
flockRadius(flockRadius),
maxAcceleration(maxAcceleration),
target(new Phantom()),
accum(0)
{}
std::vector<Triple> Flock::getVelIncr(unsigned int ticks, unsigned int delta_ticks) {
Triple steering;
Triple direction;
int tam = 0;
double distance, targetSpeed;
Triple cp, tp;
//if (this->accum == 0) {
for (unsigned int i = 0; i < boids.size(); i++) {
if (target->pos.length() < flockRadius) {
target->pos += boids[i]->pos;
tam++;
}
}
if (tam > 0) target->pos /= tam;
//}
//if (this->accum++ == 3000) this->accum = 0;
std::tie(cp, tp) = points(this->character, this->target);
/*
steering = tp - cp;
if (steering.length() > 0.001) {
steering.normalized();
steering *= maxAcceleration;
}*/
// TODO: distance
direction = tp - cp;
distance = direction.length();
if (distance < targetRadius) {
steering = target->vel - character->vel;
if (steering.length() > maxAcceleration) {
steering.normalized();
steering *= maxAcceleration;
}
return std::vector<Triple>(1, steering);
}
targetSpeed = maxAcceleration - character->vel.dot(direction.normalized());
if (distance < slowRadius) {
targetSpeed *= (distance - targetRadius) / (slowRadius - targetRadius);
}
if (targetSpeed < 0) targetSpeed = 0;
if (targetSpeed > maxAcceleration) targetSpeed = maxAcceleration;
steering = direction.normalized() * targetSpeed;
return std::vector<Triple>(1, steering);
}
void Flock::addBoid(Mobile *boid) {
this->boids.push_back(boid);
}