int NUM_PARTICLES = 500; int NUM_ATTRACTORS = 1; float damp = 0.9; // damping of velocity float accel = 20; //acceleration value Particle[] particle; //declares the particle class Attractor[] attractor; //declares the attractor class float colorGradient=0; boolean gradientSwitch=true; ///////////////////////// void setup() { size(500,500); background(255); smooth(); noFill(); begin(); } void begin(){ attractor = new Attractor[NUM_ATTRACTORS]; // sets number of attractors for (int i = 0; i < attractor.length; i++) { // for loop going from 0 to number of slots in attractor array float x=width/2+cos(i)*width/3; float y=height/2+sin(i)*height/3; attractor[i] = new Attractor(x,y); // calls the class to construct attractors } particle = new Particle[NUM_PARTICLES]; for (int i = 0; i < particle.length; i++) { float x=width/2+cos(i)*width*.75; float y=height/2+sin(i)*height*.75; particle[i] = new Particle(x,y); } } //////////////////////////////// void draw() { //background(255); float gradientMax=250; if(gradientSwitch){ colorGradient=colorGradient+1; } else{ colorGradient=colorGradient-1; } if(colorGradient>=gradientMax){ gradientSwitch=false; } if(colorGradient<=0){ gradientSwitch=true; } for (int j = 0; j < particle.length-4; j=j+1) { // for loop going from 0 to number of slots in particle class stroke(colorGradient,50-colorGradient/10,150-colorGradient/10,1); //line(particle[j].x,particle[j].y,particle[j].px,particle[j].py); // line from particle position to previous particle position curve(particle[j].x,particle[j].y,particle[j+1].x,particle[j+1].y,particle[j+2].x,particle[j+2].y,particle[j+3].x,particle[j+3].y); particle[j].step(); // calls the function step under the particle class } } //////////////////////////// void mousePressed() { saveFrame("####.jpeg"); background(255); begin(); } ///////////////////////// class Attractor { //attractor class creates x and y values to be accessed later as attractor[i].x or attractor[i].y float x,y; int particleCount=0; Attractor(float xx, float yy) { x = xx; y = yy; } } ////////////////////////// class Particle { float x,y,vx,vy,px,py; Particle(float xx, float yy) { //class constructor only read once x = px = xx; // creates a random value for x start position y = py = yy; // creates a random value for y start position vx = random(-accel/4,accel/4); //random value for velocity according to accel vy = random(-accel/4,accel/4); } void step() { px = x; //updates old px with current x py = y; for (int i = 0; i < attractor.length; i++) { // loop from 0 number of slots in attractor array //float d2=dist(attractor[i].x,attractor[i].y,x,y); float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y);//distance from attractor to current particle if (d2 > .5 ) { vx += accel * (attractor[i].x-x) / d2; //velocity is acceleration*x distance / distance vy += accel * (attractor[i].y-y) / d2; } else{ attractor[i].particleCount++; } if (attractor[i].particleCount>2){ //background(255); attractor[i].x=attractor[i].x+vx*20; attractor[i].y=attractor[i].y+vy*20; attractor[i].particleCount=0; } if (attractor[i].x>width*2||attractor[i].y>height*2||attractor[i].x<-width||attractor[i].y<-width){ //background(255); attractor[i].x=width/2; attractor[i].y=height/2; attractor[i].particleCount=0; } x += vx; // add velocity to x position of particle y += vy; vx *= damp; // dampens the ammount of velocity vy *= damp; } } }