class Ball { float x, y; // position float vx=0, vy=0; // velocity in the two directions float r=5; // radius float gravity=0.1; // the amount of acceleration) float drag=0.99; // multiplicative factor for velocity float frictX=0.75; // multiplicative factor, only when bounce float frictY=0.75; // multiplicative factor, only when bounce color c=shotColor; // color // Initial ball is off screen Ball() { x = -2*r; y = -2*r; } void draw() { fill(c); ellipse(x,y,r*2,r*2); } void update() { // Accelerate according to gravity vy += gravity; // Now damp according to drag vx *= drag; vy *= drag; // Move in the appropriate direction by the step size x += vx; y += vy; // Bounce if (x > width-r || x < r) { // Frictionless walls vx = -vx; } if (y > height-r) { y = height-r; vy = -vy; vx *= frictX; vy *= frictY; // damp } } // Shoot the Ball from position (x0,y0), moving with velocity (vx0,vy0), // and with the given color void shoot(float x0, float y0, float vx0, float vy0, color c0) { x = x0; y = y0; vx = vx0; vy = vy0; c = c0; } }