noc-book-2/content/examples/05_steering/example_5_9_flocking/sketch.js
2023-06-16 17:44:02 +00:00

31 lines
650 B
JavaScript

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Demonstration of Craig Reynolds' "Flocking" behavior
// See: http://www.red3d.com/cwr/
// Rules: Cohesion, Separation, Alignment
// Click mouse to add boids into the system
let flock;
function setup() {
createCanvas(640, 240);
flock = new Flock();
// Add an initial set of boids into the system
for (let i = 0; i < 120; i++) {
let boid = new Boid(width / 2, height / 2);
flock.addBoid(boid);
}
}
function draw() {
background(255);
flock.run();
}
// Add a new boid into the System
function mouseDragged() {
flock.addBoid(new Boid(mouseX, mouseY));
}