-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboid.js
137 lines (114 loc) · 3.95 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
class Boid {
constructor(){
let x = random(width)
let y = random(height);
this.perception = 50;
this.maxSpeed = this.perception / 10;
this.maxForce = this.maxSpeed / 10;
this.boidSize = 8;
this.position = createVector(x, y);
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random((this.maxSpeed/2), this.maxSpeed));
this.acceleration = createVector();
}
avoidEdges(){
if(this.position.x > width){
this.position.x = this.boidSize;
}else if (this.position.x < 0){
this.position.x = width - this.boidSize;
}
if(this.position.y > height){
this.position.y = this.boidSize;
}else if (this.position.y < 0){
this.position.y = height - this.boidSize;
}
}
avoidObsts(obstacles){
for(let obst of obstacles){
let distance = dist(this.position.x, this.position.y, obst.position.x, obst.position.y)
if(distance < (this.perception/2)){
this.velocity = this.velocity.mult(-1);
this.velocity.setMag(this.maxSpeed*1.5);
}
}
}
alignRule(boids){
let steering = createVector();
let total = 0;
for(let other of boids){
let distance = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if( other != this && distance <= this.perception){
steering.add(other.velocity);
total++;
}
}
if (total > 0){
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
cohesionRule(boids){
let steering = createVector();
let total = 0;
for(let other of boids){
let distance = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if( other != this && distance <= this.perception){
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;
}
separationRule(boids){
let steering = createVector();
let total = 0;
for(let other of boids){
let distance = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if( other != this && distance <= this.perception){
let diff = p5.Vector.sub(this.position, other.position);
diff.div(distance);
steering.add(diff);
total++;
}
}
if (total > 0){
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
applyRules(boids){
let alignment = this.alignRule(boids);
let cohesion = this.cohesionRule(boids);
let separation = this.separationRule(boids);
alignment.mult(alignSlider.value());
cohesion.mult(cohesionSlider.value());
separation.mult(separationSlider.value());
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
this.acceleration.add(separation);
}
updateBoids(){
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.acceleration.mult(0);
}
showBoid(){
strokeWeight(this.boidSize);
stroke(50, 115, 219);
point(this.position.x, this.position.y)
}
}