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 float g = 1; // gravity // Initialize a Spring at rest position (x0,y0) Spring(float x0, float y0, float r0) { x = x0; y = y0; cx = x0; cy = y0; r = r0; } void draw() { stroke(128); strokeWeight(3); line(cx,cy,x,y); fill(255); stroke(128); strokeWeight(1); ellipse(x,y,2*r,2*r); } // Update the spring, with a new center void update(float ncx, float ncy) { // set the new center cx = ncx; cy = ncy; // usual spring stuff vx -= k * (x-cx); vy -= k * (y-cy); vy += g; vx *= d; vy *= d; x += vx; y += vy; } }