import processing.pdf.*; import processing.video.*; /* This is a basic particle system. The particles move around and are either attracted to or repelled from the mouse. Particle System Particles: Member variables - position - velocity - acceleration Methods: - constructor - update() - draw() */ Particle[] pArray; // declare p - p is null! // object used to write movies; see MovieMaker reference void setup() { size( 500, 500,P3D); // set screen size stroke(0, 170, 250); strokeWeight(1); // defining what the lines look like //smooth(); // turn on anti-aliasing // set up the video - whis will let us save a video of our sketch pArray = new Particle[500]; // set up my array of Particles with 50 elements // loop through the Particle array (pArray) and instantiate new particles // for each element and set each one with a random velocity for( int i=0; i width ) // check particle position - is it off the screen in x? { vel.x *= -1.0f; // reverse x velocity } if ( pos.y < 0 || pos.y > height ) // check particle position - is it off the screen in x? { vel.y *= -1.0f; // reverse y velocity } } // end of bounce() /** mouseAttract() Move particles towards or away from the mouse by doing some basic vector math to determine the relationship between the particle and the mouse and based on that, calcuating an appropriate acceleration to move the particle either away from or to the mouse */ void mouseAttract() { float magnetism; // magnetism factor - +tve values attract if ( attract == true ) // check if this particle should be attracted or repulsed { magnetism = 5.0f; // make particles be attracted to the mouse } else { magnetism = -2.0f; // make particles be repulsed by the mouse } PVector mouse = new PVector( mouseX, mouseY ); // create mouse pos as a vector mouse.sub( pos ); // subtract mouse pos from particle pos // mouse now contains the difference vector between this particle // and the mouse float magnitude = mouse.mag(); // find out how far the particle is from the mouse acc.set( mouse ); // store this as the acceleration vector acc.mult( magnetism / (magnitude * magnitude) ); // scale the attraction/repuse effect using // an inverse square } // end of mouseAttract() } // end of particle class