/* title: bouncing ball description: ball deflects off sketch window edges based on: sketch created August 9, 2005 by Ira Greenberg modified: With 3 balls, and initial random ball placement, Sept. 13, 2006 by Fred Green further modified: Done with Vector2D objects, 10/22/2007 */ // declare global variables int wdth, ht; Vector2D velocity1, velocity2, velocity3; Vector2D position1, position2, position3; //initialize sketch void setup(){ //set sketch window size and background color size(500, 400); background(0); //first ball velocity velocity1 = new Vector2D(3, 4); //2nd ball velocity velocity2 = new Vector2D(5, 6); // ... and 3rd velocity3 = new Vector2D(7, 8); //ball size wdth = 20; ht = 20; // turn off shape stroke rendering noStroke(); //initial ball placement position1 = new Vector2D(random(10, width-10), random(10, height-10)); position2 = new Vector2D(random(10, width-10), random(10, height-10)); position3 = new Vector2D(random(10, width-10), random(10, height-10)); //set the animation loop speed frameRate(30); smooth(); } // begin animation loop void draw(){ //update background background(0); //draw balls fill(200, 200, 0); ellipse(position1.getX(), position1.getY(), wdth, ht); ellipse(position2.getX(), position2.getY(), wdth, ht); ellipse(position3.getX(), position3.getY(), wdth, ht); //upgrade position values position1 = position1.plus(velocity1); position2 = position2.plus(velocity2); position3 = position3.plus(velocity3); //detect collisions with sides of screen velocity1.detectCollision(position1, wdth, ht, width, height); velocity2.detectCollision(position2, wdth, ht, width, height); velocity3.detectCollision(position3, wdth, ht, width, height); }