/* * title: bouncing particles * description: particles deflect off sketch window edges * originally based on: sketch created August 9, 2005 by Ira Greenberg * modified: With any number of particles, with acceleration. * In this version, we also have Triangles and other shapes * as an example of inheritance. * by Fred Green, 11/19/2007 */ // declare global variables int wdth, ht; int number = 300; //Array of Particles: Particle [] p; //Variables for initializing velocity etc.: Vector2D velocity, position, acceleration; //initialize sketch void setup(){ //set sketch window size and background color size(500, 400); background(0); //Create the Particle array (doesn't create the Particles!): p = new Particle[number]; //ball size wdth = 20; ht = 20; //Init acceleration (same for all particles): acceleration = new Vector2D(0, 0.02); //Create each new particle: for (int i = 0; i < number; i++) { velocity = new Vector2D(random(-0.8, 0.8), random(-0.8, 0.8)); position = new Vector2D(random(10, width-10), random(10, height-10)); if (random(0, 1.0) < 0.25) p[i] = new Particle(position, velocity, acceleration, wdth, ht); else if (random(0, 1.0) < 0.5) p[i] = new Line(position, velocity, acceleration, wdth, ht); else if (random(0, 1.0) < 0.75) p[i] = new Star(position, velocity, acceleration, wdth, ht); else p[i] = new Triangle(position, velocity, acceleration, wdth, ht); } // turn off shape stroke rendering noStroke(); //set the animation loop speed frameRate(80); smooth(); } // begin animation loop void draw(){ //update background //background(0); //"Fade" rendering: fill(0, 20); rect(0, 0, width, height); //render and update each particle: for (int i = 0; i < number; i++) { p[i].render(); p[i].move(); } }