/* 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 further modified: Encapsulate velocities in a Particle class, 10/29/2007 */ // declare global variables int wdth, ht; Particle p1, p2, p3; Vector2D velocity1, velocity2, velocity3; Vector2D position1, position2, position3; Vector2D acceleration; //initialize sketch void setup(){ //set sketch window size and background color size(500, 400); background(0); //first ball velocity velocity1 = new Vector2D(0.3, 0.4); //2nd ball velocity velocity2 = new Vector2D(0.5, 0.6); // ... and 3rd velocity3 = new Vector2D(.7, 0.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)); //initialize acceleration acceleration = new Vector2D(0, 0.0); //Create particles p1 = new Particle(position1, velocity1, acceleration, wdth, ht); p2 = new Particle(position2, velocity2, acceleration, wdth, ht); p3 = new Particle(position3, velocity3, acceleration, wdth, ht); //set the animation loop speed frameRate(200); smooth(); } // begin animation loop void draw(){ //update background background(0); //draw balls p1.render(); p2.render(); p3.render(); //move them p1.move(); p2.move(); p3.move(); }