-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge-124.html
More file actions
144 lines (118 loc) · 5.23 KB
/
challenge-124.html
File metadata and controls
144 lines (118 loc) · 5.23 KB
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
<html>
<head>
<title>Challenge #124 - Flock Simulation</title>
<script src="https://cdn.jsdelivr.net/npm/p5@1.5.0/lib/p5.min.js"></script>
<script>
let flock = [];
const TOTAL_FLOCK = 50;
const MAX_PERCEPTION_RADIUS = 50;
let alignSlider, cohesionSlider, separationSlider;
class Boid {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random(0.5, 1.5));
this.acceleration = createVector(0, 0);
this.maxForce = 0.2;
this.maxSpeed = 4;
}
applyForce(force) {
this.acceleration.add(force);
}
edges() {
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;
}
}
update() {
this.acceleration.limit(this.maxForce);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.position.add(this.velocity);
this.acceleration.set(0, 0);
}
align(boids) {
let steeringForce = createVector(); //desiredVelocity
for (const other of boids) {
steeringForce.add(other.velocity);
}
if (boids.length > 0) {
steeringForce.div(boids.length);
steeringForce.setMag(this.maxSpeed);
steeringForce.sub(this.velocity); // steeringForce = p5.Vector.sub(desiredVelocity, this.velocity)
}
return steeringForce;
}
cohesion(boids) {
let steeringForce = createVector(); //desiredVelocity
for (const other of boids) {
steeringForce.add(other.position);
}
if (boids.length > 0) {
steeringForce.div(boids.length); // medium position
steeringForce.sub(this.position);
steeringForce.setMag(this.maxSpeed);
steeringForce.sub(this.velocity); // steeringForce = p5.Vector.sub(desiredVelocity, this.velocity)
steeringForce.limit(this.maxForce);
}
return steeringForce;
}
separation(boids) {
let steeringForce = createVector(); //desiredVelocity
for (const other of boids) {
let difference = p5.Vector.sub(this.position, other.position); // a force towards me
difference.div(difference.mag());
steeringForce.add(difference);
}
if (boids.length > 0) {
steeringForce.div(boids.length); // medium position
steeringForce.setMag(this.maxSpeed);
steeringForce.sub(this.velocity); // steeringForce = p5.Vector.sub(desiredVelocity, this.velocity)
steeringForce.limit(this.maxForce);
}
return steeringForce;
}
flock(boids) {
let self = this;
var closest = boids.filter(
other => p5.Vector.dist(self.position, other.position) < MAX_PERCEPTION_RADIUS &&
other != self
);
this.applyForce(this.align(closest).mult(alignSlider.value()));
this.applyForce(this.cohesion(closest).mult(cohesionSlider.value()));
this.applyForce(this.separation(closest).mult(separationSlider.value()));
}
show() {
strokeWeight(16);
stroke(255);
point(this.position.x, this.position.y);
}
}
function setup() {
createCanvas(800, 600);
alignSlider = createSlider(0, 5, 0.1);
cohesionSlider = createSlider(0, 5, 0.1);
separationSlider = createSlider(0, 5, 0.1);
for (let i = 0; i < TOTAL_FLOCK; i++) {
flock.push(new Boid(random(width), random(height)));
}
}
function draw() {
background(51);
for (const boid of flock) {
boid.edges();
boid.flock(flock);
boid.update();
boid.show();
}
}
</script>
</head>
</html>