Here's a Processing sketch demonstrating emergent behavior through a flocking algorithm (inspired by Craig Reynolds' Boids model). The sketch shows how simple rules applied to individual agents can produce complex, self-organized group behavior:
// Emergent Flocking Behavior Sketch
// By: AI Assistant
// Controls:
// - Press 'r' to reset
// - Adjust parameters in setup()
int numBoids = 50; // Number of agents
float separationDist = 50; // Distance to avoid others
float alignmentDist = 100; // Distance to match neighbors' velocity
float cohesionDist = 150; // Distance to stay near neighbors
float maxSpeed = 3; // Maximum velocity
float maxForce = 0.2; // Maximum steering force
Boid[] boids;
void setup() {
size(800, 600);
boids = new Boid[numBoids];
for (int i = 0; i < numBoids; i++) {
boids[i] = new Boid();
}
}
void draw() {
background(51);
for (Boid b : boids) {
// Calculate steering forces
PVector sep = b.separate(boids); // Separation
PVector ali = b.align(boids); // Alignment
PVector coh = b.cohesion(boids); // Cohesion
// Apply forces with weighted priorities
b.applyForce(sep.mult(1.5));
b.applyForce(ali.mult(1.0));
b.applyForce(coh.mult(1.0));
b.update();
b.display();
}
}
void keyPressed() {
if (key == 'r') {
// Reset simulation
for (Boid b : boids) {
b.position.set(random(width), random(height));
b.velocity.set(PVector.random2D());
}
}
}
class Boid {
PVector position;
PVector velocity;
PVector acceleration;
Boid() {
position = new PVector(random(width), random(height));
velocity = PVector.random2D();
velocity.setMag(random(2, maxSpeed));
acceleration = new PVector(0, 0);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxSpeed);
position.add(velocity);
acceleration.mult(0); // Reset acceleration
// Wrap screen edges
if (position.x > width) position.x = 0;
else if (position.x < 0) position.x = width;
if (position.y > height) position.y = 0;
else if (position.y < 0) position.y = height;
}
void applyForce(PVector force) {
acceleration.add(force);
}
// Rule 1: Separation - steer away from nearby neighbors
PVector separate(Boid[] others) {
PVector steer = new PVector(0, 0);
int count = 0;
for (Boid other : others) {
if (other == this) continue; // Skip self
float d = PVector.dist(position, other.position);
if (d > 0 && d < separationDist) {
PVector diff = PVector.sub(position, other.position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++;
}
}
if (count > 0) {
steer.div((float)count);
}
if (steer.mag() > 0) {
steer.normalize();
steer.mult(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
}
return steer;
}
// Rule 2: Alignment - steer towards average heading of neighbors
PVector align(Boid[] others) {
PVector steer = new PVector(0, 0);
int count = 0;
for (Boid other : others) {
if (other == this) continue;
float d = PVector.dist(position, other.position);
if (d > 0 && d < alignmentDist) {
steer.add(other.velocity);
count++;
}
}
if (count > 0) {
steer.div((float)count);
steer.setMag(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
}
return steer;
}
// Rule 3: Cohesion - steer towards average position of neighbors
PVector cohesion(Boid[] others) {
PVector steer = new PVector(0, 0);
int count = 0;
for (Boid other : others) {
if (other == this) continue;
float d = PVector.dist(position, other.position);
if (d > 0 && d < cohesionDist) {
steer.add(other.position);
count++;
}
}
if (count > 0) {
steer.div((float)count);
steer.sub(position);
steer.setMag(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
}
return steer;
}
void display() {
fill(255);
stroke(0);
// Draw triangle pointing in direction of motion
float angle = velocity.heading() + PI/2;
float arrowSize = 4;
float arrowLength = 6;
pushMatrix();
translate(position.x, position.y);
rotate(angle);
// Main body
ellipse(0, 0, 8, 8);
// Triangle arrow
beginShape();
vertex(0, 0);
vertex(-arrowLength, -arrowSize);
vertex(-arrowLength, arrowSize);
endShape(CLOSE);
popMatrix();
}
}
To use this sketch:
- Install Processing from processing.org
- Copy the code into a new Processing sketch
- Run the sketch
- Press 'r' to reset the simulation at any time
- Adjust the parameters (separationDist, alignmentDist, etc.) in the setup() section to see different behaviors
The sketch demonstrates three core flocking behaviors:
- Separation: Agents steer away from nearby neighbors
- Alignment: Agents match the average direction of neighbors
- Cohesion: Agents steer toward the average position of neighbors
The emergent behavior arises from these simple local rules applied to all agents simultaneously. The system exhibits:
- Group formation
- Collision avoidance
- Smooth turning
- Self-organization
- Responsiveness to environment
You can modify the parameters to create different effects:
- Lower separationDist → more tightly packed flock
- Increase maxSpeed → faster movement
- Adjust weights in draw() → prioritize different behaviors
- Change the steering weights (1.5, 1.0, 1.0) → emphasize certain rules
This is a classic example of how complex group behavior can emerge from simple individual rules without centralized control.