float g = 0.8;
int num = 100;
ArrayList<Mover> movers = new ArrayList<Mover>();
void setup() {
fullScreen();
colorMode(HSB, 360, 100, 100,100);
smooth();
for (int i = 0; i < num; i++) {
Mover m = new Mover(random(0.1, 2), random(width), random(height));
movers.add(m);
}
}
void draw() {
fill(0, 0, 100,20);
rect(0,0,width,height);
for (int i = 0; i < movers.size(); i++) {
for (int j = 0; j < movers.size(); j++) {
if (i != j) {
PVector force = movers.get(j).attract(movers.get(i));
movers.get(i).applyForce(force);
}
}
movers.get(i).update();
movers.get(i).display();
}
}
class Mover {
PVector position;
PVector velocity;
PVector acceleration;
float mass;
color c;
Mover(float m, float x, float y) {
mass = m;
position = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
c = color(random(200, 240), 80, 100);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0);
}
void display() {
noStroke();
fill(c);
ellipse(position.x, position.y, mass*24, mass*24);
}
PVector attract(Mover m) {
PVector force = PVector.sub(position, m.position);
float distance = force.mag();
distance = constrain(distance, 5.0, 25.0);
force.normalize();
float strength = (g * mass * m.mass) / (distance * distance);
force.mult(strength);
return force;
}
}