mirror of
https://github.com/nature-of-code/noc-book-2
synced 2024-11-17 07:49:05 +01:00
31 lines
650 B
JavaScript
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));
|
|
}
|