-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
131 lines (120 loc) · 3.99 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
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
const flock = [];
const obstacles = [];
let alignSlider, cohesionSlider, separationSlider, customSlider, customP, customSlider2, customP2, modeSelector;
function setup() {
createCanvas(800, 400);
alignSlider = createSlider(0, 2, 1, 0.02);
alignSlider.position(10, 450);
const alignP = createP('Alignment');
alignP.style('font-size', '16px');
alignP.style('font-family', 'Arial');
alignP.position(15, 410);
cohesionSlider = createSlider(0, 2, 1, 0.02);
cohesionSlider.position(150, 450);
const cohesionP = createP('Cohesion');
cohesionP.style('font-size', '16px');
cohesionP.style('font-family', 'Arial');
cohesionP.position(155, 410);
separationSlider = createSlider(0, 2, 1, 0.02);
separationSlider.position(290, 450);
const separationP = createP('Separation');
separationP.style('font-size', '16px');
separationP.style('font-family', 'Arial');
separationP.position(295, 410);
customSlider = createSlider(0, 2, 1, 0.02);
customSlider.position(430, 450);
customP = createP('');
customP.style('font-size', '16px');
customP.style('font-family', 'Arial');
customP.position(435, 410);
customSlider2 = createSlider(0, 2, 1, 0.02);
customSlider2.position(570, 450);
customP2 = createP('');
customP2.style('font-size', '16px');
customP2.style('font-family', 'Arial');
customP2.position(575, 410);
modeSelector = createSelect();
modeSelector.position(10, 10);
modeSelector.option('Default');
modeSelector.option('Reduced perception');
modeSelector.option('Follow a target');
modeSelector.option('Avoid collisions');
modeSelector.option('All at once');
modeSelector.selected('Default');
modeSelector.changed(modeChange);
modeChange();
angleMode(RADIANS);
}
function modeChange() {
flock.length = 0
obstacles.length = 0
switch (modeSelector.value()) {
case 'Reduced perception':
customSlider.style('visibility', 'visible');
customP.html('Eyesight');
customP.style('visibility', 'visible');
customSlider2.style('visibility', 'hidden');
customP2.style('visibility', 'hidden');
for (let i = 0; i < 100; i++) {
flock.push(new ReducedPerceptionBoid());
}
break;
case 'Follow a target':
customSlider.style('visibility', 'visible');
customP.html('Attraction');
customP.style('visibility', 'visible');
customSlider2.style('visibility', 'hidden');
customP2.style('visibility', 'hidden');
for (let i = 0; i < 100; i++) {
flock.push(new FollowGoalBoid());
}
break;
case 'Avoid collisions':
customSlider.style('visibility', 'hidden');
customP.style('visibility', 'hidden');
customSlider2.style('visibility', 'hidden');
customP2.style('visibility', 'hidden');
for (let i = 0; i < 100; i++) {
flock.push(new ObstacleAvoidanceBoid());
}
for (let i = 0; i < 30; i++) {
obstacles.push(new Obstacle());
}
break;
case 'All at once':
customSlider.style('visibility', 'visible');
customP.html('Perception');
customP.style('visibility', 'visible');
customSlider2.style('visibility', 'visible');
customP2.html('Attraction');
customP2.style('visibility', 'visible');
for (let i = 0; i < 100; i++) {
flock.push(new AllBoid());
}
for (let i = 0; i < 30; i++) {
obstacles.push(new Obstacle());
}
break;
default:
customSlider.style('visibility', 'hidden');
customP.style('visibility', 'hidden');
customSlider2.style('visibility', 'hidden');
customP2.style('visibility', 'hidden');
for (let i = 0; i < 100; i++) {
flock.push(new Boid());
}
break;
}
}
function draw() {
background(51);
noFill();
for (let obstacle of obstacles) {
obstacle.show();
}
for (let boid of flock) {
boid.flock(flock);
boid.update();
boid.show();
}
}