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.0; // gravity // Initialize a Spring at rest position (x0,y0) Spring(float x0, float y0) { x = x0; y = y0; cx = x0; cy = y0; } void draw() { // A "mass" at the current position, and a line to the center noStroke(); ellipse(x,y,r*2,r*2); stroke(128); line(cx,cy, x,y); } // Update the spring, with a new center void update(float ncx, float ncy) { cx = ncx; cy = ncy; // Spring equations for both x and y; gravity for y vx -= k * (x-cx); vx *= d; x += vx; vy -= k * (y-cy); vy += g; vy *= d; y += vy; } }