
int NUM_PARTICLES = 2000; //set number of first particle set in applet
int NUM_PARTICLES2 = 2000; //set number of second particle set in applet
int NUM_STALKERS = 6000; //set number of stalkers
int NUM_NEGATTRACTORS = 20; //set number of dettractors
int NUM_NEGATTRACTORS2 = 20; //set number of dettractors
int NUM_SHAREDATTRACTORS = 5; //set number of shared attractors
int NUM_ATTRACTORS = 5; //set number of red only attractors
int NUM_ATTRACTORS2 = 5; //set number of blue only dettractors
float accel;
int refreshCounter=0;
int counter;
float crowdedattractorcount=0;
float crowdedattractorX=width/2;
float crowdedattractorY=height/2;
color c1=color (73,175,159,3);//green/blue color
color c2=color (12,111,206,3);//blue color
color c3=color (255,3);//white
color c4=color (211,69,17,3);// red color
color c5=color (35,193,139,3);// blue/green
//color c5=color (245,20,118,3);// pink color
color c6=color (250,222,8,3);// yellow
//color c6=color (166,20,245,3);// purple
color crowdedattractorColor= color(c3);
Particle[] particle = new Particle[NUM_PARTICLES]; //create array of Particles
Particle[] particle2 = new Particle[NUM_PARTICLES2]; //create array of Particles
Stalker[] stalkers = new Stalker[NUM_STALKERS]; //create array of Particles
Attractor[] attractors = new Attractor[NUM_ATTRACTORS]; //create array of Attractors for attracting red
Attractor[] attractors2 = new Attractor[NUM_ATTRACTORS2]; //create array of Attractors for attracting blue
Attractor[] sharedattractors = new Attractor[NUM_SHAREDATTRACTORS];//create array of shared attractors
Attractor[] negattractors = new Attractor[NUM_NEGATTRACTORS]; //create array of Attractors for detracting red
Attractor[] negattractors2 = new Attractor[NUM_NEGATTRACTORS2]; //create array of Attractors for detracting blue
Forces forcefield; //create a force class called forcefield
void setup() { //run this once to setup
size(600,240,P3D); //set size to 600 by 240 pixels
background(0); //set background to black
forcefield = new Forces(NUM_ATTRACTORS,NUM_NEGATTRACTORS,50 ); //put in numattractors,numnegattractors acceleration
sharedattractors = new Attractor[NUM_SHAREDATTRACTORS]; //instantiates 20 Attractors called negattractors
for (int i = 0; i < NUM_SHAREDATTRACTORS; i++)
{
sharedattractors[i] = new Attractor(int (random(25,600)), int (random(0,240)) ); //places 0-9 of the dettractors at random locations
}
negattractors = new Attractor[NUM_NEGATTRACTORS]; //instantiates 20 Attractors called negattractors
for (int i = 0; i < NUM_NEGATTRACTORS; i++)
{
negattractors[i] = new Attractor(int (random(25,600)), int (random(0,240)) ); //places 0-9 of the dettractors at random locations
}
attractors = new Attractor[NUM_ATTRACTORS]; //instantiates 5 Attractors called attractors
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
attractors[i] = new Attractor( int (random(50,550)), int (random(10,230)) ); //places 5 attractors at random locations on applet
}
negattractors2 = new Attractor[NUM_NEGATTRACTORS2]; //instantiates 10 Attractors called negattractors
for (int i = 0; i < NUM_NEGATTRACTORS2; i++)
{
negattractors2[i] = new Attractor(int (random(width)), int (random(height)) ); //places 0-9 of the dettractors at random locations
}
attractors2 = new Attractor[100]; //instantiates 100 Attractors called attractors
for (int i = 0; i < NUM_ATTRACTORS2; i++)
{
attractors2[i] = new Attractor( int (random(width)), int (random(height)) ); //places 10 attractors at random locations on applet
}
for (int i = 0; i < particle.length; i++) //for loop running through all of the 2000 particles
{
particle[i] = new Particle(0,0,1,1,0,0,color(c4),color(c5)); //instantiating each of the 2000 particles
if (i<(particle.length/2)) //if statement that allows me to set a parameter on half of the to one thing and to something else on the other half
{
particle[i].x=width; //if the particle is 0-999 makes the x position equal to the width of the applet so its all the way to the right
}
}
for (int n = 0; n < particle2.length; n++) //for loop running through all of the 2000 particles
{
particle2[n] = new Particle(0,0,1,1,0,0,color(c2),color(c5)); //instantiating each of the 2000 particles
if (n<(particle2.length/2)) //if statement that allows me to set a parameter on half of the to one thing and to something else on the other half
{
particle2[n].x=width; //if the particle is 0-999 makes the x position equal to the width of the applet so its all the way to the right
}
}
for (int j = 0; j < stalkers.length; j++) //for loop running through all of the 2000 particles
{
stalkers[j] = new Stalker(0,random(0,height),1,1,0,0,color(255,5),color(255,5)); //instantiating each of the 2000 particles
if (j<(stalkers.length/2)) //if statement that allows me to set a parameter on half of the to one thing and to something else on the other half
{
stalkers[j].x=width; //if the particle is 0-999 makes the x position equal to the width of the applet so its all the way to the right
}
}
}
void draw() //run over and over
{
counter = counter+1;
int blah = counter%4;
if (counter>100)
{
for(int i=0; i<stalkers.length; i++)
{
stalkers[i].c=color(255,5);
}
if (blah<=0)
{
fill(0,1);
noStroke();
rect(0,0,width,height);
}
}
for (int j = 0; j < particle.length; j++) //for loop running from 0-1999
{
particle[j].step(); //every particle do method step which makes it move to its next calculated position
particle[j].drawParticlePath(j); //every particle do method checkDistance which checks its distance from all attractors
}
for (int o = 0; o < particle2.length; o++) //for loop running from 0-1999
{
particle2[o].step2(); //every particle do method step which makes it move to its next calculated position
particle2[o].drawParticlePath(o); //every particle do method checkDistance which checks its distance from all attractors
}
if (counter>100)
{
for (int k = 0; k < stalkers.length; k++) //for loop running from 0-1999
{
stalkers[k].stalkerstep(); //every particle do method step which makes it move to its next calculated position
stalkers[k].drawParticlePath(k); //every particle do method checkDistance which checks its distance from all attractors
}
}
forcefield.drawAttractorPoints(); //forcefield do method drawAttractorPoints which draws all Attractors
//forcefield.drawAttractorPoints2(); //forcefield do method drawAttractorPoints which draws all Attractors
for (int l = 0; l < NUM_ATTRACTORS; l++) //loop counting particles visited attractor
{
attractors[l].countparticles();
if (attractors[l].particlecounter>100)
{
attractors[l].die();
attractors[l].particlecounter=0;
}
}
for (int p = 0; p < NUM_ATTRACTORS2; p++) //loop counting particles visited attractor
{
attractors2[p].countparticles2();
if (attractors2[p].particlecounter2>150)
{
attractors2[p].die();
attractors2[p].particlecounter2=0;
}
}
for (int y = 0; y < NUM_SHAREDATTRACTORS; y++) //loop counting particles visited attractor
{
sharedattractors[y].countparticles();
sharedattractors[y].countparticles2();
if (sharedattractors[y].particlecounter+sharedattractors[y].particlecounter2>150)
{
sharedattractors[y].die();
sharedattractors[y].particlecounter=0;
sharedattractors[y].particlecounter2=0;
}
}
for (int m = 0; m < NUM_ATTRACTORS; m++) //loop picking most crowded attractor
{
noFill();
stroke(0);
//ellipse(crowdedattractorX,crowdedattractorY,10,10);
attractors[m].choseStalkerAttractor();
if (attractors[m].crowdedcounter>crowdedattractorcount)
{
crowdedattractorcount=attractors[m].crowdedcounter;
crowdedattractorX=attractors[m].x;
crowdedattractorY=attractors[m].y;
if(attractors[m].redcounter>500)
{
crowdedattractorColor=color (c4);
}
}
}
for (int q = 0; q < NUM_ATTRACTORS2; q++) //loop picking most crowded attractor
{
attractors2[q].choseStalkerAttractor();
if (attractors2[q].crowdedcounter>crowdedattractorcount)
{
crowdedattractorcount=attractors2[q].crowdedcounter;
crowdedattractorX=attractors2[q].x;
crowdedattractorY=attractors2[q].y;
if(attractors2[q].bluecounter>500)
{
crowdedattractorColor=color (c2);
}
}
}
for (int q = 0; q < NUM_SHAREDATTRACTORS; q++) //loop picking most crowded attractor
{
sharedattractors[q].checkColor();
sharedattractors[q].choseStalkerAttractor();
if (sharedattractors[q].crowdedcounter>crowdedattractorcount)
{
crowdedattractorcount=sharedattractors[q].crowdedcounter;
crowdedattractorX=sharedattractors[q].x;
crowdedattractorY=sharedattractors[q].y;
if(sharedattractors[q].redcounter>500)
{
crowdedattractorColor=color (c4); //red
}else if(sharedattractors[q].bluecounter>500){
crowdedattractorColor=color (c2); //blue
}else{
crowdedattractorColor=color(c5); //green/blue
}
}
}
crowdedattractorcount=0;
noFill();
stroke(255,255,0);
//ellipse(crowdedattractorX,crowdedattractorY,10,10);
}
void mouseClicked() //do the following when mouse clicked
{
setup(); //refresh when mouse is clicked
}
//class that controls the different forces in the applet
float stalkerdistance;
class Forces
{
Forces (int _NUM_ATTRACTORS,int _NUM_NEGATTRACTORS, float _a) //constructor for forces (put in number attractors, number dettractors, acceleration)
{
accel = _a;
}
void drawAttractorPoints() //do the following when method drawAttractor Points called
{
for (int i = 0; i < NUM_ATTRACTORS; i++) //for all the attractors
{
if (mousePressed == true) //if mouse pressed is true
{
stroke(255,50); //color attractor black
}
else //if mouse is not pressed
{
stroke(255,50); //color attractor white
}
if (mouseX>(attractors[i].x-25) && mouseX<(attractors[i].x+25) && mouseY>(attractors[i].y-25) && mouseY<(attractors[i].y+25)) //if mouse is w/in 50by50 square zone around attractor
{
stroke(255,0,0,50); //color attractor red
}
stroke(0,0,0);
attractors[i].spot(); //do method spot for all attractors --draw plus sign at location
attractors2[i].spot();
stroke(35,193,139,50);
if(sharedattractors[i].redcounter>500)
{stroke(255,0,0,50);}
if(sharedattractors[i].bluecounter>500)
{stroke(0,0,255,50);}
sharedattractors[i].spot();
}
for (int i = 0; i < NUM_NEGATTRACTORS; i++) //for all of the dettractors
{
if (mousePressed == true) //if mouse pressed is true
{
stroke(0,50); //color dettractor white
}
else //if mouse is not pressed
{
stroke(255,0,0,50); //color dettractor black
}
if (mouseX>(negattractors[i].x-25) && mouseX<(negattractors[i].x+25) && mouseY>(negattractors[i].y-25) && mouseY<(negattractors[i].y+25)) //if mouse is w/in 50by50 square zone around dettractor
{
stroke(255,0,0); //color dettractor red
}
//negattractors[i].spot(); //do method spot for all dettractors --draw plus sign at location
//negattractors2[i].spot();
}
}
/////////////////////////////////// ///////////////////////////getting attraction for red particles
Vector3D getAccel(float _x, float _y) //method of forces class to calculate the particles acceleration based on all active attractors and dettractors
{ //returns a vectors called pos which is the new acceleration vector
float d2 = 0; //distance between particle and attractor variable
float ax = 0; //attraction in x axis
float ay = 0; //attraction in y axis
for (int i = 0; i < NUM_ATTRACTORS; i++) //do these calcs for every attractor
{
d2 = sq(attractors[i].x-_x) + sq(attractors[i].y-_y); //this is square of distance from particle to attractors
if (d2 > 0.1) //if that distance is greater than .1
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (attractors[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (attractors[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (attractors[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (attractors[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
for (int i = 0; i < NUM_SHAREDATTRACTORS; i++) //do these calcs for every attractor
{
if(sharedattractors[i].bluecounter<450)
{
d2 = sq(sharedattractors[i].x-_x) + sq(sharedattractors[i].y-_y); //calculates the distance between the given vector and the attractor/////this is wrong and way way higher than dist actually is
if (d2 > 0.1) //if that distance is greater than .1
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (sharedattractors[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (sharedattractors[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (sharedattractors[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (sharedattractors[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
}
for (int i = 0; i < NUM_NEGATTRACTORS; i++) //do these calcs for every dettractor
{
d2 = sq(negattractors[i].x-_x) + sq(negattractors[i].y-_y); //calculation distance between all particles and dettractors
if (d2 > 0.1) //if distance is less than .01
{ //if mouse is not w/in 100by100 square around dettractor
if (mousePressed == true) //if mouse is pressed
{
ax = ax + accel * (negattractors[i].x-_x) /d2 ; //calculate attraction in x axis
ay = ay + accel * (negattractors[i].y-_y) /d2 ; //calculate attraction in x axis
}
else
{ //if mouse is not pressed
ax = ax - accel/10 * (negattractors[i].x-_x) / d2; //calculate dettraction in x axis
ay = ay - accel/10 * (negattractors[i].y-_y) / d2; //calculate dettraction in x axis
}
}
}
Vector3D pos = new Vector3D(ax,ay); //new vector of acceleration
return pos; //return new acceleration vector called pos
}
/////////////////////////////////// /////////////////////// //getting attraction for blue particles
Vector3D getAccel2(float _x, float _y) //method of forces class to calculate the particles acceleration based on all active attractors and dettractors
{ //returns a vectors called pos which is the new acceleration vector
float d2 = 0; //distance between particle and attractor variable
float ax = 0; //attraction in x axis
float ay = 0; //attraction in y axis
for (int i = 0; i < NUM_ATTRACTORS2; i++) //do these calcs for every attractor
{
d2 = sq(attractors2[i].x-_x) + sq(attractors2[i].y-_y); //calculates the distance between the given vector and the attractor/////this is wrong and way way higher than dist actually is
if (d2 > 0.1) //if that distance is greater than .1
{ //if mouse is not w/in 50by50 square around attractor
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (attractors2[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (attractors2[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (attractors2[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (attractors2[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
for (int i = 0; i < NUM_SHAREDATTRACTORS; i++) //do these calcs for every attractor
{
if(sharedattractors[i].redcounter<450)
{
d2 = sq(sharedattractors[i].x-_x) + sq(sharedattractors[i].y-_y); //calculates the distance between the given vector and the attractor/////this is wrong and way way higher than dist actually is
if (d2 > 0.1) //if that distance is greater than .1
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (sharedattractors[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (sharedattractors[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (sharedattractors[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (sharedattractors[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
}
for (int i = 0; i < NUM_NEGATTRACTORS2; i++) //do these calcs for every dettractor
{
d2 = sq(negattractors2[i].x-_x) + sq(negattractors2[i].y-_y); //calculation distance between all particles and dettractors
if (d2 > 0.1) //if distance is less than .01
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax + accel * (negattractors2[i].x-_x) /d2 ; //calculate attraction in x axis
ay = ay + accel * (negattractors2[i].y-_y) /d2 ; //calculate attraction in x axis
}
else
{ //if mouse is not pressed
ax = ax - accel/10 * (negattractors2[i].x-_x) / d2; //calculate dettraction in x axis
ay = ay - accel/10 * (negattractors2[i].y-_y) / d2; //calculate dettraction in x axis
}
}
}
Vector3D pos = new Vector3D(ax,ay); //new vector of acceleration
return pos; //return new acceleration vector called pos
}
Vector3D getStalkerAccel(float _x, float _y) //method of forces class to calculate the particles acceleration based on all active attractors and dettractors
{ //returns a vectors called pos which is the new acceleration vector
float d2 = 0; //distance between particle and attractor variable
float ax = 0; //attraction in x axis
float ay = 0; //attraction in y axis
float stalkeraccel = 500;
d2 = sq((crowdedattractorX)-_x) + sq((crowdedattractorY)-_y); //calculates the distance between the given vector and the attractor/////this is wrong and way way higher than dist actually is
stalkerdistance = d2;
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis
ax = ax + stalkeraccel * ((crowdedattractorX)-_x) / d2; //calculate attraction force in x axis
ay = ay + stalkeraccel * ((crowdedattractorY)-_y) / d2; //calculate attraction force in y axis//ax*=5;
for (int i = 0; i < NUM_NEGATTRACTORS2; i++) //do these calcs for every dettractor
{
d2 = sq(negattractors2[i].x-_x) + sq(negattractors2[i].y-_y); //calculation distance between all particles and dettractors
if (d2 > 0.1) //if distance is less than .01
{
if (mouseX>(negattractors2[i].x-25) && mouseX<(negattractors2[i].x+25) && mouseY>(negattractors2[i].y-25) && mouseY<(negattractors2[i].y+25)) //if mouse is w/in 50by50 square around dettractor
{
//do nothing -- do not detract
}else{ //if mouse is not w/in 100by100 square around dettractor
if (mousePressed == true) //if mouse is pressed
{
ax = ax + stalkeraccel * (negattractors2[i].x-_x) /d2 ; //calculate attraction in x axis
ay = ay + stalkeraccel * (negattractors2[i].y-_y) /d2 ; //calculate attraction in x axis
}
else
{ //if mouse is not pressed
ax = ax - stalkeraccel/10 * (negattractors2[i].x-_x) / d2; //calculate dettraction in x axis
ay = ay - stalkeraccel/10 * (negattractors2[i].y-_y) / d2; //calculate dettraction in x axis
}
}
}
}
for (int i = 0; i < NUM_ATTRACTORS2; i++) //do these calcs for every dettractor
{
d2 = sq(attractors2[i].x-_x) + sq(attractors2[i].y-_y); //calculation distance between all particles and dettractors
if (d2 > 0.1) //if distance is less than .01
{
if (mouseX>(attractors2[i].x-25) && mouseX<(attractors2[i].x+25) && mouseY>(attractors2[i].y-25) && mouseY<(attractors2[i].y+25)) //if mouse is w/in 50by50 square around dettractor
{
//do nothing -- do not detract
}else{ //if mouse is not w/in 100by100 square around dettractor
if (mousePressed == true) //if mouse is pressed
{
ax = ax - stalkeraccel/10 * (attractors2[i].x-_x) /d2 ; //calculate attraction in x axis
ay = ay - stalkeraccel/10 * (attractors2[i].y-_y) /d2 ; //calculate attraction in x axis
}
else
{ //if mouse is not pressed
ax = ax + stalkeraccel/2 * (attractors2[i].x-_x) / d2; //calculate dettraction in x axis
ay = ay + stalkeraccel/2 * (attractors2[i].y-_y) / d2; //calculate dettraction in x axis
}
}
}
}
for (int i = 0; i < NUM_ATTRACTORS; i++) //do these calcs for every attractor
{
d2 = sq(attractors[i].x-_x) + sq(attractors[i].y-_y); //this is square of distance from particle to attractors
if (d2 > 0.1) //if that distance is greater than .1
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (attractors[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (attractors[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (attractors[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (attractors[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
for (int i = 0; i < NUM_SHAREDATTRACTORS; i++) //do these calcs for every attractor
{
if(sharedattractors[i].bluecounter<450)
{
d2 = sq(sharedattractors[i].x-_x) + sq(sharedattractors[i].y-_y); //calculates the distance between the given vector and the attractor/////this is wrong and way way higher than dist actually is
if (d2 > 0.1) //if that distance is greater than .1
{
if (mousePressed == true) //if mouse is pressed
{
ax = ax - accel/2 * (sharedattractors[i].x-_x) / d2; //calculate dettraction force in x axis
ay = ay - accel/2 * (sharedattractors[i].y-_y) / d2; //calculate dettraction force in y axis
}
else
{ //if mouse is not pressed
ax = ax + accel * (sharedattractors[i].x-_x) / d2; //calculate attraction force in x axis
ay = ay + accel * (sharedattractors[i].y-_y) / d2; //calculate attraction force in y axis
}
}
}
}
for (int i = 0; i < NUM_NEGATTRACTORS; i++) //do these calcs for every dettractor
{
d2 = sq(negattractors[i].x-_x) + sq(negattractors[i].y-_y); //calculation distance between all particles and dettractors
if (d2 > 0.1) //if distance is less than .01
{ //if mouse is not w/in 100by100 square around dettractor
if (mousePressed == true) //if mouse is pressed
{
ax = ax + accel * (negattractors[i].x-_x) /d2 ; //calculate attraction in x axis
ay = ay + accel * (negattractors[i].y-_y) /d2 ; //calculate attraction in x axis
}
else
{ //if mouse is not pressed
ax = ax - accel/10 * (negattractors[i].x-_x) / d2; //calculate dettraction in x axis
ay = ay - accel/10 * (negattractors[i].y-_y) / d2; //calculate dettraction in x axis
}
}
}
Vector3D pos = new Vector3D(ax,ay); //new vector of acceleration
return pos; //return new acceleration vector called pos
}
}
class Stalker {
float x,y; //declating global variables for the x and y position of the center of the particle
float r; //declaring global variable for the angle of rotation of the ellipse
float t=0; //declaringglobal variable t set to start at 0 for every particle
float xp; //declares a global variable xp set to start at 100 for every particle
float yp; //declares a global variable xp set to start at 100 for every particle
float sinTranslateCount=0; //creates global variable for every particle starting count at zero
float curveTranslateCount = 0; //creates global variable for every particle starting count at zero
float colorT=0;
color c; //=color(73,175,159,5); //colors original color
color d; //=color(73,175,159,5); //colors original color
float w=1; //declaring global variable w for the width //our particle has a size
float h=1; //declaring global variable w for the width //our particle has a size
float distance;//declaring global variable distance of particle to attractor
float startxx;//declaring global variable start x position
float startyy;//declaring global variable
float endxx;//declaring global variable
float endyy;//declaring global variable
Vector3D pos =new Vector3D(0,0,0); //particles acceleration vector (to add to current vector to get next vector)
Vector3D v =new Vector3D(0,0,0);//particles current vector
Stalker(float X, float Y, float W, float H, float R,float colorT, color C, color D) { //class constructor, x,ypositions, width, height, rotation,start T value, starting color
x = 0;
y = random(height);
pos.x=X;
pos.y=Y;
w=W; //translating global values in local variables
h=H; //translating global values in local variables
r=R; //translating global values in local variables
c = C;
d = D;
}
void drawParticlePath(int count) //draw particle path from step to step colored according to if attractors/dettractors are flipped
{
stroke(c1); //sets the stroke to white green
if (mousePressed == true) //if mouse is pressed
{
stroke (c3); //set stroke to white
}
else
{
stroke (c);
}
//line(startxx, startyy, endxx, endyy); //draw tiny line from start x,y position to end x,y position
}
void stalkerstep() /////this is where it moves a certain amount based on the acceleration from forcefield.getAccel
{
pos = forcefield.getStalkerAccel(x,y); //gets the acceleration vector from forcefield
startxx = x; //startingx position
startyy = y; //starting y positon
x += pos.x; //adding acceleration vector
y += pos.y; //adding acceleration vector
endxx = x; //new x position
endyy = y; //new y position
if (y<0 || y>height)
{
y= random(height);
}
if (x<0 || x>width)
{
x= random(width);
}
d=crowdedattractorColor;
//d=color (255,255,0,3);
float d2 = sqrt(sq((crowdedattractorX)-x) + sq((crowdedattractorY)-y));
colorT=(1/(d2/30)-random(0,.75));
if(colorT>1)
{
colorT=1;
}
if(colorT<0)
{
colorT=0;
}
//println(colorT);
if(d2<500)
{
stroke(lerpColor(c,d,colorT)); //sets the stroke to white
}else{
stroke(255,5); //sets the stroke to white
}
point (x,y); //draw tiny dot at particles position
}
}
//attractor class made up of attractors which have a location, size/shape to draw, attraction or detraction (should make this a method of attractor class?)
class Attractor
{
float x,y;
float stalkerdestinationx;
float stalkerdesinationy;
float particlecounter;
float particlecounter2;
int redcounter=0;
int bluecounter=0;
int crowdedcounter=0;
Attractor(float _x, float _y ) //constructor
{
x = _x;
y = _y;
}
void spot() //method to draw itself as a little plus sign
{
line(x-4, y, x+4, y);
line(x, y-4, x, y+4);
}
void countparticles()
{
for (int i=0; i<NUM_PARTICLES; i++)
{
float distance = sqrt( sq(particle[i].x-x) + sq(particle[i].y-y)); //calculate particle's distance from each attractor
if (distance<10) //if distance is less than 100
{
particlecounter=particlecounter+.001;
// println(particlecounter);
}
}
}
void countparticles2()
{
for (int i=0; i<NUM_PARTICLES2; i++)
{
float distance = sqrt( sq(particle2[i].x-x) + sq(particle2[i].y-y)); //calculate particle's distance from each attractor
if (distance<10) //if distance is less than 100
{
particlecounter2=particlecounter2+.001;
// println(particlecounter);
}
}
}
void die()
{
x= random(width);
y= random(height);
}
void choseStalkerAttractor()
{
redcounter=0;
bluecounter=0;
for (int i=0; i<NUM_PARTICLES; i++)
{
float distance = sqrt( sq(particle[i].x-x) + sq(particle[i].y-y)); //calculate particle's distance from each attractor
if (distance<40) //if distance is less than 50
{
redcounter=redcounter+1;
}
}
for (int i=0; i<NUM_PARTICLES2; i++)
{
float distance2 = sqrt( sq(particle2[i].x-x) + sq(particle2[i].y-y)); //calculate particle's distance from each attractor
if (distance2<60) //if distance is less than 50
{
bluecounter=bluecounter+1;
}
}
crowdedcounter=redcounter+bluecounter;
}
void checkColor()
{
for (int i=0; i<NUM_PARTICLES; i++) //blue particles
{
float distance2 = sqrt( sq(particle[i].x-x) + sq(particle[i].y-y)); //calculate particle's distance from each attractor
if (distance2<60) //if distance is less than 50
{
if((bluecounter>200))
{
particle[i].colorT=(1/(distance2/30)-random(0,.75));
}
else
{
particle[i].colorT=particle[i].colorT-.25;
}
}
else
{
particle[i].colorT=particle[i].colorT-.25;
}
if (particle[i].colorT>1)
{
particle[i].colorT=1;
}
if (particle[i].colorT<0)
{
particle[i].colorT=0;
}
}
for (int i=0; i<NUM_PARTICLES2; i++) //blue particles
{
float distance2 = sqrt( sq(particle2[i].x-x) + sq(particle2[i].y-y)); //calculate particle's distance from each attractor
if (distance2<60) //if distance is less than 50
{
if((redcounter>200))
{
particle2[i].colorT=(1/(distance2/30)-random(0,.75));
}
else
{
particle2[i].colorT=particle2[i].colorT-.25;
}
}
else
{
particle2[i].colorT=particle2[i].colorT-.25;
}
if (particle2[i].colorT>1)
{
particle2[i].colorT=1;
}
if (particle2[i].colorT<0)
{
particle2[i].colorT=0;
}
}
}
void checkStalkerColor()
{
for (int i=0; i<NUM_STALKERS; i++) //blue particles
{
float distance2 = sqrt( sq(stalkers[i].x-crowdedattractorX) + sq(stalkers[i].y-crowdedattractorY)); //calculate stalkers's distance from each attractor
if (distance2<60) //if distance is less than 50
{
if((redcounter>200))
{
particle2[i].colorT=particle2[i].colorT+.1;
}
else
{
particle2[i].colorT=particle2[i].colorT-.25;
}
}
else
{
particle2[i].colorT=particle2[i].colorT-.25;
}
if (particle2[i].colorT>1)
{
particle2[i].colorT=1;
}
if (particle2[i].colorT<0)
{
particle2[i].colorT=0;
}
}
}
}
class Vector3D
{
float x; //global variable x position
float y; //global variable y position
float z; //global variable y position
Vector3D (float x_, float y_, float z_) //constructor for a 3D vector
{
x=x_; //sets x to input number x_
y=y_; //sets y to input number y_
z=z_; //sets y to input number z_
}
//constructor for a 2D vector
Vector3D (float x_, float y_) //if vector called with 2 inputs sets it to a vector
{
x=x_; //sets x to 0
y=y_; //sets y to 0
z=0; //sets z to 0
}
//constructor for an empty vector
Vector3D () //if vector called with no inputs sets it to a vector
{
x=0; //sets x to 0
y=0; //sets y to 0
z=0; //sets z to 0
}
//methods of vector class
//addition function
void add (Vector3D v) //this is a function that returns nothing (Void = no function return type)
{
x =v.x+x;
y =v.y+y;
z =v.z+z;
}
//subract function
void subtract(Vector3D v)
{
x =v.x-x;
y =v.y-y;
z =v.z-z;
}
//multiplication function //to divide just multiple with a number below one
void mult (float n)
{
x=x*n;
y=y*n;
z=z*n;
}
//distance funciton //calc distance between two vectors
float distance (Vector3D v)
{
float distance= sqrt (pow(v.x-x,2) + pow(v.y-y,2) + pow(v.z-z,2) );
return distance;
}
//magnitude function //calculating the length of the vector
float magnitude()
{
return (float) sqrt (pow(x,2) + pow(y,2) + pow(z,2) );//Calculate the magnitude (length) of the vector and return the magnitude of the vector
}
//set function values //change vector parameters without using another function //manual input
void set (float xx, float yy, float zz)
{
x=xx;
y=yy;
z=zz;
}
//set function values //change vector parameters without using another function // set with vectors values
void set (Vector3D v)
{
x=v.x;
y=v.y;
z=v.z;
}
}
class Particle {
float x,y; //declating global variables for the x and y position of the center of the particle
float r; //declaring global variable for the angle of rotation of the ellipse
float t=0; //declaringglobal variable t set to start at 0 for every particle
float xp; //declares a global variable xp set to start at 100 for every particle
float yp; //declares a global variable xp set to start at 100 for every particle
float sinTranslateCount=0; //creates global variable for every particle starting count at zero
float curveTranslateCount = 0; //creates global variable for every particle starting count at zero
float colorT =0; //use this number to tell each particle to be how red=1 or how white=0
color c; //=color(73,175,159,5); //colors original color
color c1=color (73,175,159,3);//green/blue color
color c2=color (12,111,206,3);//blue color
color c3=color (255,3);// white color
color c4=color (211,69,17,3);// orange color
color c5=color (155,5,75,3);// pink color
color c6=color (82,14,173,3);// purple color
color d;
float w=1; //declaring global variable w for the width //our particle has a size
float h=1; //declaring global variable w for the width //our particle has a size
float distance;//declaring global variable distance of particle to attractor
float startxx;//declaring global variable start x position
float startyy;//declaring global variable
float endxx;//declaring global variable
float endyy;//declaring global variable
int colorcount;
float stepping;
Vector3D pos =new Vector3D(0,0,0); //particles acceleration vector (to add to current vector to get next vector)
Vector3D v =new Vector3D(0,0,0);//particles current vector
Particle(float X, float Y, float W, float H, float R,float colorT, color C, color D) { //class constructor, x,ypositions, width, height, rotation,start T value, starting color
x = 0;
y = random(height);
pos.x=X;
pos.y=Y;
w=W; //translating global values in local variables
h=H; //translating global values in local variables
r=R; //translating global values in local variables
c = C;
d = D;
}
void drawParticlePath(int count) //draw particle path from step to step colored according to if attractors/dettractors are flipped
{
stroke(lerpColor(c,d,colorT)); //sets the stroke to white
line(startxx, startyy, endxx, endyy); //draw tiny line from start x,y position to end x,y position
}
void step() /////this is where it moves a certain amount based on the acceleration from forcefield.getAccel
{
pos = forcefield.getAccel(x,y); //gets the acceleration vector from forcefield
startxx = x; //startingx position
startyy = y; //starting y positon
x += pos.x; //adding acceleration vector
y += pos.y; //adding acceleration vector
endxx = x; //new x position
endyy = y; //new y position
stroke(color(c));
//point (x,y); //draw tiny dot at particles position
}
void step2() /////this is where it moves a certain amount based on the acceleration from forcefield.getAccel
{
pos = forcefield.getAccel2(x,y); //gets the acceleration vector from forcefield
startxx = x; //startingx position
startyy = y; //starting y positon
x += pos.x; //adding acceleration vector
y += pos.y; //adding acceleration vector
endxx = x; //new x position
endyy = y; //new y position
stroke(color(c));
//point (x,y); //draw tiny dot at particles position
}
}
OpenProcessing is an online community platform devoted to sharing and discussing Processing sketches in a collaborative, open-source environment.
Download Processing
Terms of Service
To contact, send an email to:

See the feedback forum and vote!
Follow OpenProcessing on Twitter.
All sketches are licensed under Creative Commons Attribution-Share Alike 3.0.
Syntax highlighting and Processing brush under LGPL 3.
All the source code is licensed under Creative Commons GNU GPL.
Comments engine by Scriptsmill Comments Script.
