-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
67 lines (54 loc) · 1.37 KB
/
sketch.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
const flock = [];
const obsts = [];
let total = 0;
let alignSlider, cohesionSlider, separationSlider;
function setup(){
createCanvas(windowWidth-200,windowHeight-300);
alignSlider = createSlider(0, 2 , 1, 0.5);
cohesionSlider = createSlider(0, 2 , 1, 0.5);
separationSlider = createSlider(0, 2 , 1, 0.5);
for (let i = 0; i < 120; i++){
flock.push(new Boid());
}
}
function draw(){
background(51);
for (let boid of flock){
boid.avoidEdges();
boid.applyRules(flock);
boid.updateBoids();
boid.showBoid();
if(obsts.length>0){
boid.avoidObsts(obsts);
}
}
for(let obst of obsts){
obst.showObst();
}
}
function mouseClicked() {
for (let boid of flock){
let distance = dist(mouseX, mouseY, boid.position.x, boid.position.y)
if (distance < (boid.perception)){
total = 1;
}
}
if(total == 0){
obsts.push(new Obstacle(mouseX, mouseY, flock));
}else{
total = 0;
}
}
function mouseDragged() {
for (let boid of flock){
let distance = dist(mouseX, mouseY, boid.position.x, boid.position.y)
if (distance < (boid.perception)){
total = 1;
}
}
if(total == 0){
obsts.push(new Obstacle(mouseX, mouseY, flock));
}else{
total = 0;
}
}