class Swarm { Particle[] ps; int pCount = 0; int pMax = 100; int pInitial = int(random(10, 30)); float x, y; // Position (centre of mass) float vx, vy; // Velocity Swarm() { x = random(padding, width - padding); y = random(padding, height - padding); vx = random(-4, 4); vy = random(-4, 4); ps = new Particle[pMax]; for (int i = 0; i < pInitial; i++) { spawnParticle(x, y, int(random(0, 250)), (random(10) > 5)); } } boolean spawnParticle(float px, float py, int age, boolean sex) { for (int i = 0; i < pMax; i++) { if (ps[i] == null) { ps[i] = new Particle(px, py, age, sex); ps[i].move(x, y); pCount++; return true; } } return false; } void move() { x += vx * random(3); y += vy * random(3); vx += random(-1, 1); vy += random(-1, 1); vx = max(min(vx, 2), -2); vy = max(min(vy, 2), -2); if ((x < padding) || (x > width - padding)) { vx *= -9; } if ((y < padding) || (y > height - padding)) { vy *= -9; } // Administer the particles int deadCount = 0; for (int i = 0; i < pCount; i++) { if (ps[i] != null) { if (ps[i].age >= 1000) { ps[i] = null; pCount--; } else { ps[i].move(x, y); // Check for spawning for (int j = i; j < pCount; j++) { if (ps[j] != null) { if ((ps[i].age > 250) && (ps[i].age < 750) && (ps[j].age > 250) && (ps[j].age < 750) && (ps[i].sex != ps[j].sex) && (ps[i].x < ps[j].x + ps[j].size / 2) && (ps[i].x > ps[j].x - ps[j].size / 2) && (ps[i].y < ps[j].y + ps[j].size / 2) && (ps[i].y > ps[j].y - ps[j].size / 2) && (random(10) > 7.5) && (ps[i].spawnTime == 0) && (ps[j].spawnTime == 0)) { if (spawnParticle((ps[i].x + ps[j].x) / 2, (ps[i].y + ps[j].y) / 2, 0, (random(10) > 5))) { ps[i].vx = 0; ps[i].vy = 0; ps[j].vx = 0; ps[j].vy = 0; ps[i].spawnTime = 3; ps[j].spawnTime = 3; } } } } } } } /*noFill(); stroke(192, 10); strokeWeight(2); ellipse(x, y, 100, 100);*/ //fill(166, 255, 52, 80); //ellipse(x, y, 24, 24); //pCount -= deadCount; } }