Hi everyone!
I found some awesome demos while trying to figure out what I could do for my assignment. I miserably tried to manipulate code for hours…thinking I could simply look at a flocking code and other notes and synthesize a predator from which the boids would flee from. But, a challenge it was and a challenge it has remained. View a screen capture of everything here.
Here are some awesome demos:
Particles with Attraction and Repulsion Forces
I managed to integrate my ball into the flocking code found on processing (see below), but I couldn’t figure out how to code such that the flock would flee from the ball as well. So, I just pretended that this was a game and the user’s purpose was to avoid the flock with the mouse.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
float xPos; float yPos; float xSpeed; float ySpeed; float diam = 50; float easing = 0.10; Flock flock; void setup() { size(640, 360); xPos = random(diam/2, width-diam/2); yPos = random(diam/2, height-diam/2); xSpeed = random(-5.0, 5.0); ySpeed = random(-5.0, 5.0); flock = new Flock(); // Add an initial set of boids into the system for (int i = 0; i < 150; i++) { flock.addBoid(new Boid(width/2,height/2)); } } void draw() { background(0); noStroke(); drawBall(xPos, yPos, diam); float targetX = mouseX; // target of ellipse X direction float dx = targetX - xPos; xPos += dx * easing; float targetY = mouseY; // target of ellipse Y direction float dy = targetY - yPos; yPos += dy * easing; xPos = updateBallX(xPos, xSpeed); yPos = updateBallY(yPos, ySpeed); checkBounds(); flock.run(); } void drawBall(float x_, float y_, float s_) { float x = x_; float y = y_; float s = s_; ellipse(x, y, s, s); } float updateBallX(float x_, float xs_) { float xpos = x_; float xspeed = xs_; xpos = xpos + xspeed; return xpos; } float updateBallY(float y_, float ys_) { float ypos = y_; float yspeed = ys_; ypos = ypos + yspeed; return ypos; } void checkBounds() { if(xPos > width-diam/2 || xPos < diam/2) { xSpeed = xSpeed * -1; } if(yPos > height-diam/2 || yPos < diam/2 ) { ySpeed = ySpeed * -1; } } void mousePressed() { flock.addBoid(new Boid(mouseX,mouseY)); } // The Flock (a list of Boid objects) class Flock { ArrayList<Boid> boids; // An ArrayList for all the boids Flock() { boids = new ArrayList<Boid>(); // Initialize the ArrayList } void run() { for (Boid b : boids) { b.run(boids); // Passing the entire list of boids to each boid individually } } void addBoid(Boid b) { boids.add(b); } } // The Boid class class Boid { PVector location; PVector velocity; PVector acceleration; float r; float maxforce; // Maximum steering force float maxspeed; // Maximum speed Boid(float x, float y) { acceleration = new PVector(0, 0); // This is a new PVector method not yet implemented in JS // velocity = PVector.random2D(); // Leaving the code temporarily this way so that this example runs in JS float angle = random(TWO_PI); velocity = new PVector(cos(angle), sin(angle)); location = new PVector(x, y); r = 2.0; maxspeed = 2; maxforce = 0.03; } void run(ArrayList<Boid> boids) { flock(boids); update(); borders(); render(); } void applyForce(PVector force) { // We could add mass here if we want A = F / M acceleration.add(force); } // We accumulate a new acceleration each time based on three rules void flock(ArrayList<Boid> boids) { PVector sep = separate(boids); // Separation PVector ali = align(boids); // Alignment PVector coh = cohesion(boids); // Cohesion // Arbitrarily weight these forces sep.mult(1.5); ali.mult(1.0); coh.mult(1.0); // Add the force vectors to acceleration applyForce(sep); applyForce(ali); applyForce(coh); } // Method to update location void update() { // Update velocity velocity.add(acceleration); // Limit speed velocity.limit(maxspeed); location.add(velocity); // Reset accelertion to 0 each cycle acceleration.mult(0); } // A method that calculates and applies a steering force towards a target // STEER = DESIRED MINUS VELOCITY PVector seek(PVector target) { PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target // Scale to maximum speed desired.normalize(); desired.mult(maxspeed); // Above two lines of code below could be condensed with new PVector setMag() method // Not using this method until Processing.js catches up // desired.setMag(maxspeed); // Steering = Desired minus Velocity PVector steer = PVector.sub(desired, velocity); steer.limit(maxforce); // Limit to maximum steering force return steer; } void render() { // Draw a triangle rotated in the direction of velocity float theta = velocity.heading2D() + radians(90); // heading2D() above is now heading() but leaving old syntax until Processing.js catches up fill(200, 100); stroke(255); pushMatrix(); translate(location.x, location.y); rotate(theta); beginShape(TRIANGLES); vertex(0, -r*2); vertex(-r, r*2); vertex(r, r*2); endShape(); popMatrix(); } // Wraparound void borders() { if (location.x < -r) location.x = width+r; if (location.y < -r) location.y = height+r; if (location.x > width+r) location.x = -r; if (location.y > height+r) location.y = -r; } // Separation // Method checks for nearby boids and steers away PVector separate (ArrayList<Boid> boids) { float desiredseparation = 25.0f; PVector steer = new PVector(0, 0, 0); int count = 0; // For every boid in the system, check if it's too close for (Boid other : boids) { float d = PVector.dist(location, other.location); // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) if ((d > 0) && (d < desiredseparation)) { // Calculate vector pointing away from neighbor PVector diff = PVector.sub(location, other.location); diff.normalize(); diff.div(d); // Weight by distance steer.add(diff); count++; // Keep track of how many } } // Average -- divide by how many if (count > 0) { steer.div((float)count); } // As long as the vector is greater than 0 if (steer.mag() > 0) { // First two lines of code below could be condensed with new PVector setMag() method // Not using this method until Processing.js catches up // steer.setMag(maxspeed); // Implement Reynolds: Steering = Desired - Velocity steer.normalize(); steer.mult(maxspeed); steer.sub(velocity); steer.limit(maxforce); } return steer; } // Alignment // For every nearby boid in the system, calculate the average velocity PVector align (ArrayList<Boid> boids) { float neighbordist = 50; PVector sum = new PVector(0, 0); int count = 0; for (Boid other : boids) { float d = PVector.dist(location, other.location); if ((d > 0) && (d < neighbordist)) { sum.add(other.velocity); count++; } } if (count > 0) { sum.div((float)count); // First two lines of code below could be condensed with new PVector setMag() method // Not using this method until Processing.js catches up // sum.setMag(maxspeed); // Implement Reynolds: Steering = Desired - Velocity sum.normalize(); sum.mult(maxspeed); PVector steer = PVector.sub(sum, velocity); steer.limit(maxforce); return steer; } else { return new PVector(0, 0); } } // Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location PVector cohesion (ArrayList<Boid> boids){ float neighbordist = 50; PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all locations int count = 0; for (Boid other : boids) { float d = PVector.dist(location, other.location); if ((d > 0) && (d < neighbordist)) { sum.add(other.location); // Add location count++; } } if (count > 0) { sum.div(count); return seek(sum); // Steer towards the location } else { return new PVector(0, 0); } } } |
Ideally, one would create another function that tells the flock to avoid the x and y position of the ball (plus its diameter I assume) at any times.
Furthermore, I changed the class’s original multi-ball animation script by adding some color coding (see below). I just wrote another function and incorporated it.
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 |
float xPos; float yPos; float xPos2; float yPos2; float xSpeed; float ySpeed; float xSpeed2; float ySpeed2; float diam = 50; float easing = 0.05; void setup() { size(500, 400); //background(133, 227, 252); xPos = random(diam/2, width-diam/2); yPos = random(diam/2, height-diam/2); xPos2 = random(diam/2, width-diam/2); yPos2 = random(diam/2, height-diam/2); xSpeed = random(-5.0, 5.0); ySpeed = random(-5.0, 5.0); xSpeed2 = random(-5.0, 5.0); ySpeed2 = random(-5.0, 5.0); } void draw(){ background(0); noStroke(); drawBall(xPos, yPos, diam); // gonna need to give some arguments drawBall(xPos2, yPos2, diam); xPos = updateBallX(xPos, xSpeed); yPos = updateBallY(yPos, ySpeed); xPos2 = updateBallX(xPos2, xSpeed2); yPos2 = updateBallY(yPos2, ySpeed2); float targetX = xPos; float dx = targetX - xPos; xPos += dx * easing; float targetY = yPos; float dy = targetY - yPos; yPos += dy * easing; float targetX2 = xPos2; float dx2 = targetX2 - xPos2; xPos2 += dx2 * easing; float targetY2 = yPos2; float dy2 = targetY2 - yPos2; yPos2 += dy2 * easing; checkBounds(); checkBounds2(); vroom(); } void drawBall(float x_, float y_, float s_) { float x = x_; float y = y_; float s = s_; ellipse(x, y, s, s); } float updateBallX(float x_, float xs_) { float xpos = x_; float xspeed = xs_; xpos = xpos + xspeed; return xpos; } float updateBallY(float y_, float ys_) { float ypos = y_; float yspeed = ys_; ypos = ypos + yspeed; return ypos; } void checkBounds() { if(xPos > width-diam/2 || xPos < diam/2) { xSpeed = xSpeed * -1; } if(yPos > height-diam/2 || yPos < diam/2) { ySpeed = ySpeed * -1; } } void checkBounds2() { if (xPos2 > width-diam/2 || xPos2 < diam/2) { xSpeed2 = xSpeed2*-1; } if (yPos2 > height-diam/2 || yPos2 < diam/2) { ySpeed2 = ySpeed2*-1; } } void vroom() { if (xPos >= xPos2 || yPos >= yPos2) { fill(#7512A0); println("x: " + xPos); println("y: " + yPos); } else if (xPos < xPos2 || yPos < yPos2){ fill(255); } } |