ArrayList<Colony> colonies = new ArrayList<Colony>();
colorMode(HSB, 360, 100, 100);
background(map(mouseX, 0, width, 0, 360), map(mouseY, 0, height, 20, 40), 20);
for (Colony colony : colonies) {
colonies.add(new Colony(new PVector(mouseX, mouseY)));
ArrayList<Bacteria> members = new ArrayList<Bacteria>();
Colony(PVector _target) {
members.add(new Bacteria(target.x + random(-20, 20), target.y + random(-20, 20)));
for (int i = 0; i < members.size(); i++) {
Bacteria single = members.get(i);
single.run(members, target);
target.add(new PVector(noise(width, millis()), noise(height, millis())));
} else if (target.x < 0) {
} else if (target.y < 0) {
int lifespan = 250 + int(random(-50, 50));
Bacteria(float posX, float posY) {
location = new PVector(posX, posY);
velocity = new PVector(random(-2, 2), random(-2, 2));
acceleration = new PVector(0, 0);
void run(ArrayList<Bacteria> colony, PVector target) {
applyBehaviors(colony, target);
velocity.add(acceleration);
velocity.limit(maxSpeed);
stroke(map(mouseX, 0, width, 360, 0), 80, 100);
fill(map(mouseX, 0, width, 360, 0), 80, 100, 100);
ellipse(location.x + map(noise(velocity.x, millis() * 0.001), 0, 1, -5, 5),
location.y + map(noise(velocity.y, millis() * 0.001), 0, 1, -5, 5),
if (location.x > width - size) {
location.x = width - size;
} else if (location.x < size) {
if (location.y > height - size) {
location.y = height - size;
} else if (location.y < size) {
void divide(ArrayList<Bacteria> colony) {
float probPopulation = noise(millis() * 0.1) / colony.size();
if (random(1) < probPopulation) {
colony.add(new Bacteria(location.x, location.y));
void applyBehaviors(ArrayList<Bacteria> colony, PVector target) {
PVector separateForce = separate(colony);
PVector seekForce = seek(target);
PVector mouseRepelForce = avoidMouse(new PVector(mouseX, mouseY));
acceleration.add(separateForce);
acceleration.add(seekForce);
acceleration.add(mouseRepelForce);
PVector seek(PVector target) {
PVector desired = PVector.sub(target, location);
PVector steer = PVector.sub(desired, velocity);
PVector separate(ArrayList<Bacteria> colony) {
float desiredSeparation = size + 2;
PVector sum = new PVector();
for (Bacteria other : colony) {
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < desiredSeparation)) {
PVector diff = PVector.sub(location, other.location);
PVector avoidMouse(PVector mousePos) {
float desiredSeparation = 50;
PVector sum = new PVector();
float d = PVector.dist(location, mousePos);
if ((d > 0) && (d < desiredSeparation)) {
PVector diff = PVector.sub(location, mousePos);