xxxxxxxxxx
let agent;
let colors = [];
let bgColor;
function setup() {
createCanvas(800, 800);
bgColor = color(20);
background(bgColor);
// Initialize vibrant colors
colors = [
'#F72585', '#7209B7', '#3A0CA3', '#4361EE', '#4CC9F0',
'#F9C74F', '#90BE6D', '#43AA8B', '#577590', '#FF6F61',
'#A7C7E7', '#FFB400', '#FF5733', '#D7263D', '#36A2EB'
];
// Create the first autonomous agent
agent = new AutonomousAgent(random(width), random(height));
}
function draw() {
// Do not clear the canvas; trails will be permanent
agent.update();
agent.display();
}
class AutonomousAgent {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = p5.Vector.random2D().mult(random(15, 30)); // Increased initial speed
this.acceleration = createVector(0, 0);
this.size = random(6, 12); // Random size
this.colorIndex = floor(random(colors.length));
this.maxSpeed = random(30, 50); // Significantly increased max speed
this.trails = []; // Store trail points
}
update() {
// Random acceleration for faster change in direction
this.acceleration = p5.Vector.random2D().mult(random(1, 2)); // Higher acceleration
// Update velocity and constrain speed to maximum limit
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
// Update position
this.position.add(this.velocity);
// Add the current position to the trail
this.trails.push({
pos: this.position.copy(),
size: this.size,
color: colors[this.colorIndex]
});
// Boundary conditions: spawn a new agent if hitting edges
if (this.position.x < 0 || this.position.x > width || this.position.y < 0 || this.position.y > height) {
this.spawnNewAgent();
}
}
display() {
// Draw the trail
for (let trail of this.trails) {
noStroke();
fill(trail.color);
ellipse(trail.pos.x, trail.pos.y, trail.size);
}
// Draw the agent
noStroke();
fill(colors[this.colorIndex]);
ellipse(this.position.x, this.position.y, this.size, this.size);
}
spawnNewAgent() {
// Spawn a new agent at the current position where the agent hits the edge
let spawnX = this.position.x;
let spawnY = this.position.y;
// Spawn a new agent at this edge contact point
let newAgent = new AutonomousAgent(spawnX, spawnY);
// Apply a random velocity to make sure the new agent moves away from the edge
newAgent.velocity = p5.Vector.random2D().mult(random(15, 30)); // Random velocity to start
// Set the newly spawned agent to the global agent variable
agent = newAgent;
}
}