// Dartmouth CS 2, Winter 2009, Chris Bailey-Kellogg // Notes 15 | Sketch 2 Ball[] balls = new Ball[10]; int dragging = -1; // which ball is being dragged (-1 if none) int G = 50; // strength of repulsion void setup() { size(400,300); smooth(); noStroke(); // Start them at random places for (int i=0; i 0) { // Compute repulsive force, and the fraction in each direction float f = G/(dij*dij); float dx = (balls[j].x-balls[i].x)/dij; float dy = (balls[j].y-balls[i].y)/dij; // Push each away from the other (unless being dragged) if (i != dragging) { balls[i].vx -= f*dx; balls[i].vy -= f*dy; } if (j != dragging) { balls[j].vx += f*dx; balls[j].vy += f*dy; } } } } // Update velocities and positions for (int i=0; i= 0) { balls[dragging].x = mouseX; balls[dragging].y = mouseY; } } void mouseReleased() { // Stop dragging and put it at rest (0 velocity) if (dragging >= 0) { balls[dragging].vx = 0; balls[dragging].vy = 0; dragging = -1; } } void keyPressed() { if (key=='G') { // more repulsion G+=10; println("G:"+G); } else if (key=='g') { // less repulsion if (G>10) { G-=10; println("G:"+G); } } else if (key=='s') { // stop for (int i=0; i