This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SimpleFlock.cpp
executable file
·110 lines (92 loc) · 2.15 KB
/
SimpleFlock.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
#include "SimpleFlock.h"
#include <limits>
#ifdef min
# undef min
#endif
#ifdef max
# undef max
#endif
#define DIST_SIMPLE 0
#define DIST_REAL 1
#define DIST_RULE DIST_SIMPLE
#define DATUM_NEAREST 0
#define DATUM_FIRST 1
#define DATUM_RULE DATUM_NEAREST
FlockGroup::Object::Object() {
pos = Vec2f(
std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN()
);
dist = std::numeric_limits<float>::quiet_NaN();
}
FlockGroup::Object::Object(const Vec2f &o) : pos(o) {
dist = std::numeric_limits<float>::quiet_NaN();
}
FlockGroup::Object::Object(const Vec2f &o, const Object &t) : pos(o) {
#if DIST_RULE == DIST_SIMPLE
dist = fabs(pos.x - t.pos.x) + fabs(pos.y - t.pos.y);
#elif DIST_RULE == DIST_REAL
dist = (pos - t.pos).length();
#else
# error "Not defined."
#endif
}
void FlockGroup::Object::clear(void) {
pos = Vec2f(
std::numeric_limits<float>::max(),
std::numeric_limits<float>::max()
);
dist = std::numeric_limits<float>::max();
}
FlockGroup::FlockGroup() {
adjuster = NULL;
nearest.clear();
radius = Vec2f(
std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::quiet_NaN()
);
last = now = 0;
}
FlockGroup::~FlockGroup() {
}
void FlockGroup::begin(void) {
last = now;
now = 0;
radius = nearest.pos - target.pos;
float l = radius.normalize();
l *= 0.5f;
radius *= l;
nearest.clear();
}
void FlockGroup::end(void) {
}
Vec2f FlockGroup::getTargetPosition(const Vec2f &o, Adjuster adj) {
Object f(o, target);
now++;
#if DATUM_RULE == DATUM_NEAREST
if(f.dist < nearest.dist)
#elif DATUM_RULE == DATUM_FIRST
if(now == 1)
#else
# error "Not defined."
#endif
nearest = f;
if(radius.isInvalid()) {
return target.pos;
} else {
float s = (float)(now - 1) / (float)last;
float r = s * 3.14159265f * 2.0f;
if(f.pos.x > target.pos.x)
r = -r;
Vec2f p = radius.rotate(r);
if(adj)
p = adj(p, target.pos, now, last);
else if(adjuster)
p = adjuster(p, target.pos, now, last);
p += target.pos;
return p;
}
}
void FlockGroup::setTargetObject(const Vec2f &t) {
target = Object(t);
}