class Spring { float cx, cy; // rest position float x, y; // current position float vx=0, vy=0; // velocity in the two directions float r = 10; // radius float k = 0.05; // spring constant float d = 0.75; // damping // Initialize a Spring at rest position (x0,y0), of size w*h Spring(float x0, float y0, float r0) { x = x0; y = y0; cx = x0; cy = y0; r = r0; // Random spring constant and damping k = random(0.01,0.02); d = random(0.975,1.0); } void draw() { stroke(128); strokeWeight(3); line(cx,cy,x,y); fill(255); stroke(128); strokeWeight(1); ellipse(x,y,2*r,2*r); } void update() { if(dist(mouseX,mouseY,x,y) < r) { // Give it a kick of velocity when (while) mouse is over it vy = 10; } // usual spring stuff vx -= k * (x-cx); vy -= k * (y-cy); vx *= d; vy *= d; x += vx; y += vy; } }