-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboid.js
179 lines (163 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
class Boid{
constructor(){
//constructs new boid object
this.position = createVector(random(width), random(height))
this.velocity = p5.Vector.random2D()
this.velocity.setMag(random(2, 4))
this.acceleration = createVector()
this.perceptionRadius = perceptionRadiusSlider.value()
this.r = 3
this.maxForce = .1
this.maxSpeed = 10
this.history = []
}
edges(){
//allows boids to wrap around the screen
if (this.position.x > width){
this.position.x = 0
} else if (this.position.x < 0){
this.position.x = width
}
if (this.position.y > height){
this.position.y = 0
} else if (this.position.y < 0){
this.position.y = height
}
}
//fix visual radius to allow an angle without visual
//add flake
align(boids){
//alignment algorithm for boids in relation to other boids within perception radius
let steering = createVector()
let total = 0
for (let other of boids){
let d = dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
)
if (other != this && d < this.perceptionRadius){
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;
}
separation(boids){
//separation algorithm for boids in relation to other boids within perception radius
let steering = createVector()
let total = 0
for (let other of boids){
let d = dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
)
if (other != this && d < this.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){
//cohesion algorithm for boids in relation to other boids within perception radius
let steering = createVector()
let total = 0
for (let other of boids){
let d = dist(
this.position.x,
this.position.y,
other.position.x,
other.position.y
)
if (other != this && d < this.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){
//changes acceleration value based on alignment,cohesion,separation algorithms
//changes alignment,cohesion,separation values based on sliders
let alignment = this.align(boids)
let cohesion = this.cohesion(boids)
let separation = this.separation(boids)
alignment.mult(alignmentSlider.value())
cohesion.mult(cohesionSlider.value())
separation.mult(separationSlider.value())
//this.angle = perceptionSlider.value()
this.perceptionRadius = perceptionRadiusSlider.value()
this.acceleration.add(alignment)
this.acceleration.add(cohesion)
this.acceleration.add(separation)
}
checkCollision(){
}
update(){
//if(trailBox.checked() == true){
//this.history.push(this.position.x, this.position.y)
//}
this.position.add(this.velocity)
this.velocity.add(this.acceleration)
this.velocity.limit(this.maxSpeed)
this.acceleration.mult(0)
}
//trails dont work, figure out later
show(){
//draws each boid based on its vector location and angle
//translate moves drawing position from origin (top of screen) to specified pos
let theta = this.velocity.heading() + radians(90)
push()
fill(255)
stroke(255)
translate(this.position.x, this.position.y)
rotate(theta)
beginShape()
vertex(0, this.r * -2)
vertex(-this.r, this.r * 2)
vertex(this.r, this.r * 2)
endShape(CLOSE)
pop()
}
//doesn't work, fix later
showTrail(){
if(this.history.length > 5){
push()
stroke(200)
beginShape()
for (let i = 0; i < this.history.length; i++){
let pos = this.history[i]
noFill()
vertex(pos[0], pos[1])
endShape()
}
this.history.pop()
pop()
}
}
}