/* Demonstrates a ball in motion which bounces at the edges of the canvas. The arrow keys can be used to alter the velocity of the ball. The space key returns the ball to a relatively modest random velocity. Author: Michael Goldwasser */ float cx, cy, r; // parameters for circle center (x,y) and radius float vx, vy; // velocity parameters for movement void setup() { size(500, 400); cx = 0.5*width; cy = 0.5*height; r = 0.125*min(width, height); vx = int(random(-5, 6)); vy = int(random(-5, 6)); fill(random(255), random(255), random(255)); } void draw() { cx += vx; cy += vy; if (cx - r < 0 || cx + r > width) { vx = -vx; // flip motion horizontally } if (cy - r < 0 || cy + r > height) { vy = -vy; // flip motion vertically } background(200); ellipse(cx, cy, 2*r, 2*r); } void mousePressed() { vx = int(random(-5, 6)); vy = int(random(-5, 6)); }