int x, y; // location of the ball at any time float direction; // direction of the ball at any time boolean grabbed; // true if the ball is being held float speed; // the ball’s speed final int RADIUS = 40; // the ball’s radius final float ACCELERATION = 0.10; // acceleration/deceleration amount void setup() { size(600,600); x = width/2; y = height/2; direction = random(TWO_PI); grabbed = false; speed = 10; } void mousePressed() { if (dist(x,y,mouseX,mouseY) < RADIUS) grabbed = true; } void mouseReleased() { if (grabbed) { direction = atan2(mouseY - pmouseY, mouseX - pmouseX); speed = int(dist(mouseX, mouseY, pmouseX, pmouseY)); } grabbed = false; } void draw() { background(0,0,0); ellipse(x, y, 2*RADIUS,2*RADIUS); // move the ball forward if not being held if (!grabbed) { x = x + int(speed*cos(direction)); y = y + int(speed*sin(direction)); } else { x = mouseX; y = mouseY; } speed = max(0, speed - ACCELERATION); if ((x+RADIUS >= width) || (x-RADIUS <= 0)) direction = PI - direction; if ((y+RADIUS >= height) || (y-RADIUS <= 0)) direction = -direction; }