int numBalls = 75; float spring = 0.10; float gravity = 0.10; float friction = -0.15; Ball[] balls = new Ball[numBalls]; void setup() { size(200, 600); noStroke(); smooth(); for (int i = 0; i < numBalls; i++) { balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls); } } void draw() { background(255); for (int i = 0; i < numBalls; i++) { balls[i].collide(); balls[i].move(); balls[i].display(); } } class Ball { float x, y; float diameter; float vx = 0; float vy = 0; int id; Ball[] others; Ball(float xin, float yin, float din, int idin, Ball[] oin) { x = xin; y = yin; diameter = din; id = idin; others = oin; } void collide() { for (int i = id + 1; i < numBalls; i++) { float dx = others[i].x - x; float dy = others[i].y - y; float distance = sqrt(dx*dx + dy*dy); float minDist = others[i].diameter/2 + diameter/2; if (distance < minDist) { float angle = atan2(dy, dx); float targetX = x + cos(angle) * minDist; float targetY = y + sin(angle) * minDist; float ax = (targetX - others[i].x) * spring; float ay = (targetY - others[i].y) * spring; vx -= ax; vy -= ay; others[i].vx += ax; others[i].vy += ay; } } } void move() { vy += gravity; x += vx; y += vy; if (x + diameter/3 > width) { x = width - diameter/3; vx *= friction; } else if (x - diameter/3 < 0) { x = diameter/3; vx *= friction; } if (y + diameter/2 > height) { y = height - diameter/3; vy *= friction; } else if (y - diameter/3 < 0) { y = diameter/3; vy *= friction; } } void display() { stroke(153); fill(175,224,230); rect(x, y, diameter, diameter); } }