-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboid.js
179 lines (150 loc) · 4.44 KB
/
boid.js
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
// Flocking
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/124-flocking-boids.html
// https://youtu.be/mhjuuHl6qHM
// https://editor.p5js.org/codingtrain/sketches/ry4XZ8OkN
//init 1.5 1 2
// 1.5 3 2
let alignmentValue = 1.5
let cohesionValue = 1.5 // was 1
let separationValue = 2.5 // was 2
class Boid {
constructor(p) {
this.position = p.createVector(p.random(p.width), p.random(p.height));
this.velocity = p5.Vector.random2D();
this.velocity.setMag(p.random(2, 5)); // was 2, 4
this.acceleration = p.createVector();
this.maxForce = 0.2; //was 0.2 was 1
this.maxSpeed = 6; //was 5 was 4
this.p = p
}
edges() {
if (this.position.x > this.p.width) {
this.position.x = 0;
} else if (this.position.x < 0) {
this.position.x = this.p.width;
}
if (this.position.y > this.p.height) {
this.position.y = 0;
} else if (this.position.y < 0) {
this.position.y = this.p.height;
}
}
align(boids) {
let perceptionRadius = 25; // was 50
// let perceptionRadius = this.p.random(50, 100)
let steering = this.p.createVector();
//let steering = this.p.createVector(p.mouseX, p.mouseY)
let total = 0;
for (let other of boids) {
let d = this.p.dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
);
if (other != this && d < perceptionRadius) {
steering.add(other.velocity);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
// console.log("STEERING: ", steering)
return steering;
}
separation(boids) {
let perceptionRadius = 24; //was 24 was 50
// let perceptionRadius = this.p.random(100, 200)
let steering = this.p.createVector();
let total = 0;
for (let other of boids) {
let d = this.p.dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
);
if (other != this && d < perceptionRadius) {
let diff = p5.Vector.sub(this.position, other.position);
diff.div(d * d);
steering.add(diff);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
cohesion(boids) {
let perceptionRadius = 40; //was 50 was 100
//let perceptionRadius = this.p.random(50, 150)
let steering = this.p.createVector();
let total = 0;
for (let other of boids) {
let d = this.p.dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
);
if (other != this && d < perceptionRadius) {
steering.add(other.position);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.sub(this.position);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
flock(boids) {
let alignment = this.align(boids);
let cohesion = this.cohesion(boids);
let separation = this.separation(boids);
alignment.mult(alignmentValue);
cohesion.mult(cohesionValue);
separation.mult(separationValue);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
this.acceleration.add(separation);
}
move_to_mouse( position) {
var bipc = this.p.createVector(this.p.mouseX, this.p.mouseY);
var v = bipc.sub(position)
//v.div(1000); // was 100, 500 is good
v.div(this.p.random(4000, 5000)) //the greater the better for subtlety.
// v.div(this.p.random(100, 20000))
return v;
}
update() {
this.position.add(this.velocity);
this.position.add(this.move_to_mouse(this.position))
this.position.add(this.move_to_mouse(this.position))
this.position.add(this.move_to_mouse(this.position))
//this.position.add(this.move_to_mouse(this.position))
this.velocity.add(this.acceleration);
this.velocity.add(this.move_to_mouse(this.position))
this.velocity.limit(this.maxSpeed);
this.acceleration.mult(0);
}
show() {
this.p.strokeWeight(6);
//this.p.strokeWeight(this.p.random(3,10))
this.p.stroke(220);
// this.p.stroke(this.p.random(200, 255))
this.p.point(this.position.x, this.position.y);
}
}