|
4 | 4 | * (Rules: Cohesion, Separation, Alignment.)<br>
|
5 | 5 | * From <a href="http://natureofcode.com">natureofcode.com</a>.
|
6 | 6 | */
|
7 |
| -var boids = []; |
| 7 | +let boids = []; |
8 | 8 |
|
9 | 9 | function setup() {
|
10 | 10 | createCanvas(720, 400);
|
11 | 11 |
|
12 | 12 | // Add an initial set of boids into the system
|
13 |
| - for (var i = 0; i < 100; i++) { |
| 13 | + for (let i = 0; i < 100; i++) { |
14 | 14 | boids[i] = new Boid(random(width), random(height));
|
15 | 15 | }
|
16 | 16 | }
|
17 | 17 |
|
18 | 18 | function draw() {
|
19 | 19 | background(51);
|
20 | 20 | // Run all the boids
|
21 |
| - for (var i = 0; i < boids.length; i++) { |
| 21 | + for (let i = 0; i < boids.length; i++) { |
22 | 22 | boids[i].run(boids);
|
23 | 23 | }
|
24 | 24 | }
|
25 | 25 |
|
26 |
| - |
27 | 26 | // Boid class
|
28 | 27 | // Methods for Separation, Cohesion, Alignment added
|
29 |
| -function Boid(x, y) { |
30 |
| - this.acceleration = createVector(0, 0); |
31 |
| - this.velocity = p5.Vector.random2D(); |
32 |
| - this.position = createVector(x, y); |
33 |
| - this.r = 3.0; |
34 |
| - this.maxspeed = 3; // Maximum speed |
35 |
| - this.maxforce = 0.05; // Maximum steering force |
36 |
| -} |
37 |
| - |
38 |
| -Boid.prototype.run = function(boids) { |
39 |
| - this.flock(boids); |
40 |
| - this.update(); |
41 |
| - this.borders(); |
42 |
| - this.render(); |
43 |
| -} |
44 |
| - |
45 |
| -// Forces go into acceleration |
46 |
| -Boid.prototype.applyForce = function(force) { |
47 |
| - this.acceleration.add(force); |
48 |
| -} |
49 |
| - |
50 |
| -// We accumulate a new acceleration each time based on three rules |
51 |
| -Boid.prototype.flock = function(boids) { |
52 |
| - var sep = this.separate(boids); // Separation |
53 |
| - var ali = this.align(boids); // Alignment |
54 |
| - var coh = this.cohesion(boids); // Cohesion |
55 |
| - // Arbitrarily weight these forces |
56 |
| - sep.mult(2.5); |
57 |
| - ali.mult(1.0); |
58 |
| - coh.mult(1.0); |
59 |
| - // Add the force vectors to acceleration |
60 |
| - this.applyForce(sep); |
61 |
| - this.applyForce(ali); |
62 |
| - this.applyForce(coh); |
63 |
| -} |
64 |
| - |
65 |
| -// Method to update location |
66 |
| -Boid.prototype.update = function() { |
67 |
| - // Update velocity |
68 |
| - this.velocity.add(this.acceleration); |
69 |
| - // Limit speed |
70 |
| - this.velocity.limit(this.maxspeed); |
71 |
| - this.position.add(this.velocity); |
72 |
| - // Reset acceleration to 0 each cycle |
73 |
| - this.acceleration.mult(0); |
74 |
| -} |
75 |
| - |
76 |
| -// A method that calculates and applies a steering force towards a target |
77 |
| -// STEER = DESIRED MINUS VELOCITY |
78 |
| -Boid.prototype.seek = function(target) { |
79 |
| - var desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target |
80 |
| - // Normalize desired and scale to maximum speed |
81 |
| - desired.normalize(); |
82 |
| - desired.mult(this.maxspeed); |
83 |
| - // Steering = Desired minus Velocity |
84 |
| - var steer = p5.Vector.sub(desired, this.velocity); |
85 |
| - steer.limit(this.maxforce); // Limit to maximum steering force |
86 |
| - return steer; |
87 |
| -} |
88 |
| - |
89 |
| -// Draw boid as a circle |
90 |
| -Boid.prototype.render = function() { |
91 |
| - fill(127, 127); |
92 |
| - stroke(200); |
93 |
| - ellipse(this.position.x, this.position.y, 16, 16); |
94 |
| -} |
95 |
| - |
96 |
| -// Wraparound |
97 |
| -Boid.prototype.borders = function() { |
98 |
| - if (this.position.x < -this.r) this.position.x = width + this.r; |
99 |
| - if (this.position.y < -this.r) this.position.y = height + this.r; |
100 |
| - if (this.position.x > width + this.r) this.position.x = -this.r; |
101 |
| - if (this.position.y > height + this.r) this.position.y = -this.r; |
102 |
| -} |
| 28 | +class Boid { |
| 29 | + constructor(x, y) { |
| 30 | + this.acceleration = createVector(0, 0); |
| 31 | + this.velocity = p5.Vector.random2D(); |
| 32 | + this.position = createVector(x, y); |
| 33 | + this.r = 3.0; |
| 34 | + this.maxspeed = 3; // Maximum speed |
| 35 | + this.maxforce = 0.05; // Maximum steering force |
| 36 | + } |
103 | 37 |
|
104 |
| -// Separation |
105 |
| -// Method checks for nearby boids and steers away |
106 |
| -Boid.prototype.separate = function(boids) { |
107 |
| - var desiredseparation = 25.0; |
108 |
| - var steer = createVector(0, 0); |
109 |
| - var count = 0; |
110 |
| - // For every boid in the system, check if it's too close |
111 |
| - for (var i = 0; i < boids.length; i++) { |
112 |
| - var d = p5.Vector.dist(this.position, boids[i].position); |
113 |
| - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) |
114 |
| - if ((d > 0) && (d < desiredseparation)) { |
115 |
| - // Calculate vector pointing away from neighbor |
116 |
| - var diff = p5.Vector.sub(this.position, boids[i].position); |
117 |
| - diff.normalize(); |
118 |
| - diff.div(d); // Weight by distance |
119 |
| - steer.add(diff); |
120 |
| - count++; // Keep track of how many |
121 |
| - } |
| 38 | + run(boids) { |
| 39 | + this.flock(boids); |
| 40 | + this.update(); |
| 41 | + this.borders(); |
| 42 | + this.render(); |
122 | 43 | }
|
123 |
| - // Average -- divide by how many |
124 |
| - if (count > 0) { |
125 |
| - steer.div(count); |
| 44 | + |
| 45 | + // Forces go into acceleration |
| 46 | + applyForce(force) { |
| 47 | + this.acceleration.add(force); |
126 | 48 | }
|
127 |
| - |
128 |
| - // As long as the vector is greater than 0 |
129 |
| - if (steer.mag() > 0) { |
130 |
| - // Implement Reynolds: Steering = Desired - Velocity |
131 |
| - steer.normalize(); |
132 |
| - steer.mult(this.maxspeed); |
133 |
| - steer.sub(this.velocity); |
134 |
| - steer.limit(this.maxforce); |
| 49 | + |
| 50 | + // We accumulate a new acceleration each time based on three rules |
| 51 | + flock(boids) { |
| 52 | + let sep = this.separate(boids); // Separation |
| 53 | + let ali = this.align(boids); // Alignment |
| 54 | + let coh = this.cohesion(boids); // Cohesion |
| 55 | + // Arbitrarily weight these forces |
| 56 | + sep.mult(2.5); |
| 57 | + ali.mult(1.0); |
| 58 | + coh.mult(1.0); |
| 59 | + // Add the force vectors to acceleration |
| 60 | + this.applyForce(sep); |
| 61 | + this.applyForce(ali); |
| 62 | + this.applyForce(coh); |
135 | 63 | }
|
136 |
| - return steer; |
137 |
| -} |
138 |
| - |
139 |
| -// Alignment |
140 |
| -// For every nearby boid in the system, calculate the average velocity |
141 |
| -Boid.prototype.align = function(boids) { |
142 |
| - var neighbordist = 50; |
143 |
| - var sum = createVector(0, 0); |
144 |
| - var count = 0; |
145 |
| - for (var i = 0; i < boids.length; i++) { |
146 |
| - var d = p5.Vector.dist(this.position, boids[i].position); |
147 |
| - if ((d > 0) && (d < neighbordist)) { |
148 |
| - sum.add(boids[i].velocity); |
149 |
| - count++; |
150 |
| - } |
| 64 | + |
| 65 | + // Method to update location |
| 66 | + update() { |
| 67 | + // Update velocity |
| 68 | + this.velocity.add(this.acceleration); |
| 69 | + // Limit speed |
| 70 | + this.velocity.limit(this.maxspeed); |
| 71 | + this.position.add(this.velocity); |
| 72 | + // Reset acceleration to 0 each cycle |
| 73 | + this.acceleration.mult(0); |
151 | 74 | }
|
152 |
| - if (count > 0) { |
153 |
| - sum.div(count); |
154 |
| - sum.normalize(); |
155 |
| - sum.mult(this.maxspeed); |
156 |
| - var steer = p5.Vector.sub(sum, this.velocity); |
157 |
| - steer.limit(this.maxforce); |
| 75 | + |
| 76 | + // A method that calculates and applies a steering force towards a target |
| 77 | + // STEER = DESIRED MINUS VELOCITY |
| 78 | + seek(target) { |
| 79 | + let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target |
| 80 | + // Normalize desired and scale to maximum speed |
| 81 | + desired.normalize(); |
| 82 | + desired.mult(this.maxspeed); |
| 83 | + // Steering = Desired minus Velocity |
| 84 | + let steer = p5.Vector.sub(desired, this.velocity); |
| 85 | + steer.limit(this.maxforce); // Limit to maximum steering force |
158 | 86 | return steer;
|
159 |
| - } else { |
160 |
| - return createVector(0, 0); |
161 | 87 | }
|
162 |
| -} |
163 |
| - |
164 |
| -// Cohesion |
165 |
| -// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location |
166 |
| -Boid.prototype.cohesion = function(boids) { |
167 |
| - var neighbordist = 50; |
168 |
| - var sum = createVector(0, 0); // Start with empty vector to accumulate all locations |
169 |
| - var count = 0; |
170 |
| - for (var i = 0; i < boids.length; i++) { |
171 |
| - var d = p5.Vector.dist(this.position, boids[i].position); |
172 |
| - if ((d > 0) && (d < neighbordist)) { |
173 |
| - sum.add(boids[i].position); // Add location |
174 |
| - count++; |
| 88 | + |
| 89 | + // Draw boid as a circle |
| 90 | + render() { |
| 91 | + fill(127, 127); |
| 92 | + stroke(200); |
| 93 | + ellipse(this.position.x, this.position.y, 16, 16); |
| 94 | + } |
| 95 | + |
| 96 | + // Wraparound |
| 97 | + borders() { |
| 98 | + if (this.position.x < -this.r) this.position.x = width + this.r; |
| 99 | + if (this.position.y < -this.r) this.position.y = height + this.r; |
| 100 | + if (this.position.x > width + this.r) this.position.x = -this.r; |
| 101 | + if (this.position.y > height + this.r) this.position.y = -this.r; |
| 102 | + } |
| 103 | + |
| 104 | + // Separation |
| 105 | + // Method checks for nearby boids and steers away |
| 106 | + separate(boids) { |
| 107 | + let desiredseparation = 25.0; |
| 108 | + let steer = createVector(0, 0); |
| 109 | + let count = 0; |
| 110 | + // For every boid in the system, check if it's too close |
| 111 | + for (let i = 0; i < boids.length; i++) { |
| 112 | + let d = p5.Vector.dist(this.position, boids[i].position); |
| 113 | + // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) |
| 114 | + if ((d > 0) && (d < desiredseparation)) { |
| 115 | + // Calculate vector pointing away from neighbor |
| 116 | + let diff = p5.Vector.sub(this.position, boids[i].position); |
| 117 | + diff.normalize(); |
| 118 | + diff.div(d); // Weight by distance |
| 119 | + steer.add(diff); |
| 120 | + count++; // Keep track of how many |
| 121 | + } |
175 | 122 | }
|
| 123 | + // Average -- divide by how many |
| 124 | + if (count > 0) { |
| 125 | + steer.div(count); |
| 126 | + } |
| 127 | + |
| 128 | + // As long as the vector is greater than 0 |
| 129 | + if (steer.mag() > 0) { |
| 130 | + // Implement Reynolds: Steering = Desired - Velocity |
| 131 | + steer.normalize(); |
| 132 | + steer.mult(this.maxspeed); |
| 133 | + steer.sub(this.velocity); |
| 134 | + steer.limit(this.maxforce); |
| 135 | + } |
| 136 | + return steer; |
176 | 137 | }
|
177 |
| - if (count > 0) { |
178 |
| - sum.div(count); |
179 |
| - return this.seek(sum); // Steer towards the location |
180 |
| - } else { |
181 |
| - return createVector(0, 0); |
| 138 | + |
| 139 | + // Alignment |
| 140 | + // For every nearby boid in the system, calculate the average velocity |
| 141 | + align(boids) { |
| 142 | + let neighbordist = 50; |
| 143 | + let sum = createVector(0, 0); |
| 144 | + let count = 0; |
| 145 | + for (let i = 0; i < boids.length; i++) { |
| 146 | + let d = p5.Vector.dist(this.position, boids[i].position); |
| 147 | + if ((d > 0) && (d < neighbordist)) { |
| 148 | + sum.add(boids[i].velocity); |
| 149 | + count++; |
| 150 | + } |
| 151 | + } |
| 152 | + if (count > 0) { |
| 153 | + sum.div(count); |
| 154 | + sum.normalize(); |
| 155 | + sum.mult(this.maxspeed); |
| 156 | + let steer = p5.Vector.sub(sum, this.velocity); |
| 157 | + steer.limit(this.maxforce); |
| 158 | + return steer; |
| 159 | + } else { |
| 160 | + return createVector(0, 0); |
| 161 | + } |
182 | 162 | }
|
| 163 | + |
| 164 | + // Cohesion |
| 165 | + // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location |
| 166 | + cohesion(boids) { |
| 167 | + let neighbordist = 50; |
| 168 | + let sum = createVector(0, 0); // Start with empty vector to accumulate all locations |
| 169 | + let count = 0; |
| 170 | + for (let i = 0; i < boids.length; i++) { |
| 171 | + let d = p5.Vector.dist(this.position, boids[i].position); |
| 172 | + if ((d > 0) && (d < neighbordist)) { |
| 173 | + sum.add(boids[i].position); // Add location |
| 174 | + count++; |
| 175 | + } |
| 176 | + } |
| 177 | + if (count > 0) { |
| 178 | + sum.div(count); |
| 179 | + return this.seek(sum); // Steer towards the location |
| 180 | + } else { |
| 181 | + return createVector(0, 0); |
| 182 | + } |
| 183 | + } |
183 | 184 | }
|
| 185 | + |
0 commit comments