xxxxxxxxxx
ArrayList particles = new ArrayList();
void setup() {
size(500,500);
background(250);
stroke(150,150,100);
}
void draw() {
background(250);
if ((frameCount%10)==0 && mousePressed) {
PVector newVelocity = new PVector(width/2-mouseX,height/2-mouseY);
newVelocity.normalize();
particles.add(new Particle(new PVector(mouseX, mouseY), newVelocity, 10));
}
for (int i=0; i < particles.size(); i++) {
Particle p = (Particle) particles.get(i);
p.update();
p.boundary();
if (p.stopped){
for (int u = 0; u < particles.size(); u++) {
Particle p2 = (Particle) particles.get(u);
p.detectCollision(p2);
if (PVector.dist(p.location,p2.location) <= p2.s/2)
line(p.location.x, p.location.y, p2.location.x, p2.location.y);
}
}
}
}
class Particle {
PVector location;
PVector velocity;
float s;
boolean stopped = false;
Particle(PVector l, PVector v, float s) {
location = l;
velocity = v;
this.s = s;
}
void update() {
location.add(velocity);
point(location.x, location.y);
}
void boundary() {
if (dist(location.x, location.y, width/2, height/2) < 50) {
velocity = new PVector(0, 0);
stopped = true;
}
}
void detectCollision(Particle p) {
if (PVector.dist(location, p.location) <= p.s/2) {
p.velocity = new PVector(0, 0);
p.stopped = true;
}
}
}