class Ball { float x, y; // position float vx=0, vy=0; // velocity in the two directions float r=5; // radius float drag; // multiplicative factor for velocity Ball(float x0, float y0, float drag) { x = x0; y = y0; this.drag = drag; } void draw() { ellipse(x,y,r*2,r*2); } void update() { // Damp according to drag vx *= drag; vy *= drag; // Move in the appropriate direction by the step size x += vx; y += vy; // Bounce, with frictionless walls if (x > width-r) { x=width-r; vx=-vx; } else if (x < r) { x=r; vx=-vx; } if (y > height-r) { y=height-r; vy=-vy; } else if (y < r) { y=r; vy = -vy; } } }