xxxxxxxxxx
// Define an array of agents
let agents = [];
let numAgents = 10;
function setup() {
createCanvas(800, 600);
// Initialize multiple agents
for (let i = 0; i < numAgents; i++) {
agents.push(new Agent(random(width), random(height)));
}
}
function draw() {
background(220);
// Update and display all agents
for (let agent of agents) {
agent.update();
agent.display();
}
}
class Agent {
constructor(x, y) {
// Position and velocity of the agent
this.position = createVector(x, y);
this.velocity = createVector(random(-1, 1), random(-1, 1));
this.angle = random(TWO_PI);
this.size = random(15, 25); // Random size for diversity
}
update() {
// Apply noise for smooth randomness
let noiseFactor = noise(this.position.x * 0.01, this.position.y * 0.01);
let angleChange = map(noiseFactor, 0, 1, -0.1, 0.1);
this.angle += angleChange;
// Update velocity with noise-based direction
let direction = p5.Vector.fromAngle(this.angle);
this.velocity = direction.mult(2);
// Update position
this.position.add(this.velocity);
// Keep the agent within the canvas boundaries (wrap around)
if (this.position.x > width) this.position.x = 0;
if (this.position.x < 0) this.position.x = width;
if (this.position.y > height) this.position.y = 0;
if (this.position.y < 0) this.position.y = height;
}
display() {
// Draw the agent
fill(100, 200, 150, 150);
noStroke();
ellipse(this.position.x, this.position.y, this.size, this.size);
}
}