-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlocking.js
More file actions
171 lines (154 loc) · 3.73 KB
/
Copy pathFlocking.js
File metadata and controls
171 lines (154 loc) · 3.73 KB
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
class Flock {
constructor() {
this.boids = [];
}
run() {
for (let b of this.boids) {
b.run(this.boids);
}
}
addBoid(b) {
this.boids.push(b);
}
}
class Boid {
constructor(x, y) {
this.acceleration = createVector(0, 0);
let angle = random(TWO_PI);
this.velocity = createVector(cos(angle), sin(angle));
this.position = createVector(x, y);
this.r = 2.0;
this.maxspeed = 2;
this.maxforce = 0.03;
}
run(boids) {
this.flock(boids);
this.update();
this.borders();
this.render();
}
applyForce(force) {
this.acceleration.add(force);
}
flock(boids) {
let sep = this.separate(boids);
let ali = this.align(boids);
let coh = this.cohesion(boids);
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
this.applyForce(sep);
this.applyForce(ali);
this.applyForce(coh);
}
update() {
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
seek(target) {
let desired = p5.Vector.sub(target, this.position);
desired.normalize();
desired.mult(this.maxspeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce);
return steer;
}
render() {
let theta = this.velocity.heading() + radians(90);
fill(200, 100);
stroke(255);
push();
translate(this.position.x, this.position.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -this.r * 2);
vertex(-this.r, this.r * 2);
vertex(this.r, this.r * 2);
endShape();
pop();
}
borders() {
if (this.position.x < -this.r) this.position.x = width + this.r;
if (this.position.y < -this.r) this.position.y = height + this.r;
if (this.position.x > width + this.r) this.position.x = -this.r;
if (this.position.y > height + this.r) this.position.y = -this.r;
}
separate(boids) {
let desiredseparation = 25.0;
let steer = createVector(0, 0);
let count = 0;
for (let other of boids) {
let d = p5.Vector.dist(this.position, other.position);
if ((d > 0) && (d < desiredseparation)) {
let diff = p5.Vector.sub(this.position, other.position);
diff.normalize();
diff.div(d);
steer.add(diff);
count++;
}
}
if (count > 0) steer.div(count);
if (steer.mag() > 0) {
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
align(boids) {
let neighbordist = 50;
let sum = createVector(0, 0);
let count = 0;
for (let other of boids) {
let d = p5.Vector.dist(this.position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.normalize();
sum.mult(this.maxspeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxforce);
return steer;
}
return createVector(0, 0);
}
cohesion(boids) {
let neighbordist = 50;
let sum = createVector(0, 0);
let count = 0;
for (let other of boids) {
let d = p5.Vector.dist(this.position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.position);
count++;
}
}
if (count > 0) {
sum.div(count);
return this.seek(sum);
}
return createVector(0, 0);
}
}
let flock;
function setup() {
createCanvas(640, 360);
flock = new Flock();
for (let i = 0; i < 150; i++) {
flock.addBoid(new Boid(width / 2, height / 2));
}
}
function draw() {
background(50);
flock.run();
}
function mousePressed() {
flock.addBoid(new Boid(mouseX, mouseY));
}