import traer.physics.*; //set up particle variables ParticleSystem physics; Particle[] particles; float velocity = 15.0; float particleSize = 3; float attractStrength = 15.0; int particleAmount = 2000; boolean attractRepel = false; color particleColor; //set up some other working variables to switch stuff on and off int numFrames = 5000; boolean renderToFrames = false; boolean firstMouseClick = false; boolean showTarget = false; //show the target ball (or not) boolean clearBackground = false; //setup main screen colour variables int blendAmt = 30; //the alpha value of the screen clear color backgroundColor; //setup the baouncing ball variables Ball[] balls; //create a type for a list that contains Ball types Ball b; //Prepare variables for the instance to live in: int disturbSpeed = 25; //the speed of the bouncing target ball ///SET UP/// void setup(){ //set up environment size( 640, 480 ); smooth(); frameRate(50); colorMode(HSB); backgroundColor = color(370,250,100,blendAmt); background(backgroundColor); particleColor = color(0,0,255, 50);// sets the color and blend of the particles resetWorld(int(random(0,width)), int(random(0,height))); } ///RESET WORLD/// void resetWorld(int x, int y){ firstMouseClick = true; //clear the background and remake the particles if the mouse is pressed background(backgroundColor); //make some bouncing balls createBalls(); //make some particles createParticles(x, y); } void draw(){ //render out some frames if the s key is pressed if (keyPressed){ if(key == 's'){ renderToFrames =! renderToFrames; } else if(key== 't'){ showTarget =! showTarget; } else if(key == 'b'){ clearBackground =! clearBackground; } } //redraw the background // blendAmt = int(255-map(mouseX,0, width,0, 255)); // backgroundColor = color(254,255,100,blendAmt); noStroke(); if(clearBackground==false){ fill(backgroundColor); rect(0,0,width,height); } if(firstMouseClick == true){ drawBalls(); //draws the target balls renderFrames(); // render frames (if we want) drawParticles(); //draw the particles physics.tick(); physics.advanceTime( 1.0 ); //move the particle that follows the target ball particles[1].moveTo( balls[0].xpos, balls[0].ypos, 0 ); } if ( mousePressed ) { resetWorld(mouseX, mouseY); } } ///RENDER FRAMES// void renderFrames(){ if (renderToFrames==true){ if(frameCount < numFrames){ saveFrame("./render2/particles-####.jpg"); } } } ///REVERSE ATTRACTION/// //method to reverse the attraction (to repulsion), not in use at the moment void attractRepel(){ if(attractRepel==true){ attractStrength = -attractStrength; for ( int i = 2; i < particles.length; ++i ) { physics.makeAttraction( particles[i], particles[1], attractStrength, 50.0 ); } } } ///CREATE PARTICLES/// void createParticles(int x, int y){ //create a new physics environment from the Traer physics engine physics = new ParticleSystem( 0.005, 0.05 ); //create an array to hold the particle objects particles = new Particle[particleAmount]; //birth some particle objects for (int i = 1; i < particles.length; ++i ){ if(i==1){ //one particle follows the bouncing ball (yes, I know this is redundant..) //it has more mass than all the other particles particles[1] = physics.makeParticle( 100.0, width/2, height/2, 0.0 ); } else{ //make the other particles particles[i] = physics.makeParticle( 1.0, width/2, height/2, random(-100.0,100.0) ); } //move the particles to the centre particles[i].moveTo( x, y, 0.0 ); //give them some velocity particles[i].addVelocity( random(-velocity,velocity), random(-velocity,velocity),random(-velocity,velocity)); //set up the attraction physics.makeAttraction( particles[i], particles[1], attractStrength, 50.0 ); //if(i%2 == 0){ if(i!=1){ particles[i].setMass(random(0.1,3.0)); } } } ///DRAW PARTICLES/// void drawParticles(){ noStroke(); fill(particleColor); for ( int i = 2; i < particles.length; ++i ) { //keep recylcing the particles - if they fall off the bottom of the screen, put them back on top if(particles[i].position().y() > height){ particles[i].moveTo( particles[i].position().x(), -100, 0.0 ); } //translate(0,0,particles[i].position().z()); // fill(random(250,350),random(100,255),255, random(0,50)); ellipse( particles[i].position().x(), particles[i].position().y(), particleSize, particleSize ); //actually draw something to the screen where the particles are } } ///CREATE TARGET BALLS/// void createBalls(){ balls = new Ball[1]; //at the moment, I'm just using one ball, but this is set up to be a whole array of them //repeat through the list (as long as we're less that the length of it) for(int i=0; i