forked from dcheesman/LEDStripController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlock.pde
147 lines (119 loc) · 3.94 KB
/
Flock.pde
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
class Flock extends Effect{
int boidCount = 80;
Boid[] boids;
float infludenceZone = 5;
color baseColor;
Flock(int _millis, color _c){
super(_millis);
baseColor = _c;
boids = new Boid[boidCount];
for(int i=0; i<boids.length; i++) {
boids[i] = new Boid();
}
imageBuffer.beginDraw();
imageBuffer.stroke(baseColor);
imageBuffer.fill(0,20);
imageBuffer.endDraw();
}
void update(){
steer();
attract();
deflect();
for(Boid boid : boids){
boid.update();
}
display();
}
void display(){
// println(boids[0].location.x + ", " + boids[0].location.y);
imageBuffer.beginDraw();
// this makes the last frame semi-transparent
imageBuffer.rect(-1,-1,cols+2,rows+2);
imageBuffer.background(0,0);
for(Boid boid : boids){
boid.display();
}
imageBuffer.endDraw();
}
void steer(){
for(int i=0; i<boids.length-1; i++){
for(int t=i+1; t<boids.length; t++){
float d = PVector.dist(boids[i].location, boids[t].location);
if(d < infludenceZone && d>0){
// get the difference between directions
PVector delta = PVector.add(boids[i].velocity, boids[t].velocity);
delta.mult(.5);
delta.normalize();
// divide by the distance (stronger when close)
delta.div(d);
boids[i].velocity.add(delta);
boids[t].velocity.add(delta);
}
}
}
}
void attract(){
for(int i=0; i<boids.length-1; i++){
for(int t=i+1; t<boids.length; t++){
float d = PVector.dist(boids[i].location, boids[t].location);
if(d < infludenceZone && d>deflectZone){
// get the difference between directions
PVector delta = PVector.sub(boids[i].location, boids[t].location);
delta.normalize();
// divide by the distance (stronger when close)
delta.div(d);
boids[i].velocity.sub(delta);
boids[t].velocity.add(delta);
}
}
}
}
float deflectZone = 2;
void deflect(){
for(int i=0; i<boids.length-1; i++){
for(int t=i+1; t<boids.length; t++){
float d = PVector.dist(boids[i].location, boids[t].location);
if(d < deflectZone && d>0){
// get the difference between directions
PVector delta = PVector.sub(boids[i].location, boids[t].location);
delta.normalize();
// divide by the distance (stronger when close)
delta.div(d/2);
boids[i].velocity.add(delta);
boids[t].velocity.sub(delta);
}
}
}
}
class Boid{
PVector location;
PVector velocity;
float maxForce = .5;
Boid(){
location = new PVector(random(cols), random(rows));
// velocity = new PVector(random(-maxForce,maxForce), random(-maxForce,maxForce));
velocity = new PVector(0,0);
}
void update(){
velocity.limit(maxForce);
location.add(velocity);
if(location.x > cols){
location.x = 0;
}
if(location.x < 0){
location.x = cols;
}
if(location.y > rows){
location.y = 0;
}
if(location.y < 0){
location.y = rows;
}
// dampen
velocity.mult(.98);
}
void display(){
imageBuffer.point(location.x, location.y);
}
}
}