class Spring { float cx, cy; // rest position float x, y; // current position float vx=0, vy=0; // velocity in the two directions float r; // radius float k; // spring constant float d; // damping // Initialize a Spring at rest position (x0,y0), radius r, // spring constant k, and damping d Spring(float x0, float y0, float r0, float k0, float d0) { x = x0; y = y0; cx = x0; cy = y0; r = r0; k = k0; d = d0; } void draw() { fill(0); stroke(255); strokeWeight(2); ellipse(x,y,r*2,r*2); } // Update the spring, with a new center void update(float ncx, float ncy) { cx = ncx; cy = ncy; // usual spring stuff vx -= k * (x-cx); vy -= k * (y-cy); vx *= d; vy *= d; x += vx; y += vy; } }