-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboid.js
More file actions
51 lines (46 loc) · 1000 Bytes
/
boid.js
File metadata and controls
51 lines (46 loc) · 1000 Bytes
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
class Boid {
constructor() {
this.position = createVector(width/2, height/2);
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random(0.5, 1.5));
this.acceleration = createVector();
}
getMyNeighbours(boids){
let neighbours = [];
let perception = 100;
for (let boid of boids){
if(boid == this){
continue;
}
let distance = dist(
this.position.x,
this.position.y,
boid.position.x,
boid.position.y,
);
if(distance < perception){
neighbours.push(boid);
}
}
/* TODO this.neighbours chyba lepsze */
return neighbours;
}
align(boids){
let neighbours = this.getMyNeighbours(boids);
let avg = createVector();
for (let boid of neighbours){
avg.add(boid.velocity);
}
avg.div(neighbours.length);
this.velocity = avg;
}
update(){
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
}
show(){
strokeWeight(16);
stroke(255);
point(this.position.x, this.position.y);
}
}