class ParticleController { LinkedList particles; int jitterRadius = 5; void addParticles( int amt){ for(int i = 0; i < amt; i++){ addRandomParticle(); } } void addParticles(int amt, int x, int y,Vec2D v){ for (int i = 0; i < amt; i++){ addParticle(jitter(x),jitter(y),v); } } int jitter(int loc){ int jit = int(random(-1*jitterRadius,jitterRadius)); return loc + jit; } void addParticle(int x, int y){ particles.add(new Particle(x,y)); } void addParticle(int xi, int yi,Vec2D v){ particles.add(new Particle(xi,yi,v)); } void addRandomParticle(){ int x = int(random(0,width)); int y = int(random(0,height)); addParticle(x,y); } ParticleController(){ particles = new LinkedList(); } void update(){ repulseParticles(); for(Iterator it = particles.iterator(); it.hasNext();){ Particle p = (Particle)it.next(); p.update(); if(p.isDead){ it.remove(); } } } void repulseParticles(){ for(Iterator i1 = particles.iterator(); i1.hasNext();){ Particle p1 = (Particle)i1.next(); int i = particles.indexOf(p1) +1; int s= particles.size(); if ( i == s){ return; } for(Iterator i2 = particles.subList(i,particles.size()-1).iterator(); i2.hasNext();){ Particle p2 = (Particle)i2.next(); Vec2D dir = p1.sub(p2); float distSqrd = p1.distanceToSquared(p2); if(distSqrd > 0.0){ dir.normalize(); float F = 1.0/distSqrd; p1.acceleration.addSelf( dir.scale(F /p1.mass)); p2.acceleration.subSelf(dir.scale(F /p2.mass)); } } } } void draw(){ for(Iterator it = particles.iterator(); it.hasNext();){ Particle p = (Particle)it.next(); p.draw(); } } }