int NUM_PARTICLES = 2000;
int NUM_NEGATTRACTORS = 10;
int NUM_ATTRACTORS = 10;
Particle[] particle = new Particle[NUM_PARTICLES];
Attractor[] attractors = new Attractor[NUM_ATTRACTORS+50];
Attractor[] negattractors = new Attractor[NUM_NEGATTRACTORS];
Forces forcefield;
void setup() {
size(600,240,P3D);
background(0);
forcefield = new Forces(NUM_ATTRACTORS,NUM_NEGATTRACTORS,50 );
for (int i = 0; i < particle.length; i++)
{
particle[i] = new Particle(0,0,1,1,0);
if (i<(particle.length/2))
{
particle[i].x=width;
}
}
}
void draw() {
fill(255,0,0);
for (int j = 0; j < particle.length; j++)
{
particle[j].step();
particle[j].checkDistance(j);
}
forcefield.drawAttractorPoints();
}
void mouseClicked()
{
forcefield.addAttractor();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
setup();
}
}
}
class Forces {
float accel;
int NUM_ATTRACTORS=10;
int NUM_NEGATTRACTORS=10;
Forces (int _NUM_ATTRACTORS,int _NUM_NEGATTRACTORS, float _a)
{
accel = _a;
NUM_ATTRACTORS = _NUM_ATTRACTORS;
NUM_NEGATTRACTORS = _NUM_NEGATTRACTORS;
negattractors = new Attractor[NUM_NEGATTRACTORS+50];
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
negattractors[i] = new Attractor(int (random(25,600)), int (random(0,250)) );
}
NUM_ATTRACTORS = _NUM_ATTRACTORS;
attractors = new Attractor[100];
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
attractors[i] = new Attractor( int (random(25,600)), int (random(0,250)) );
}
}
void addAttractor()
{
attractors[NUM_ATTRACTORS] = new Attractor(mouseX,mouseY);
count();
}
void count()
{
NUM_ATTRACTORS=NUM_ATTRACTORS+1;
}
void drawAttractorPoints()
{
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
if (mousePressed == true)
{
stroke(0,50);
}else{
stroke(255,50);
}
if (mouseX>(attractors[i].x-10) && mouseX<(attractors[i].x+10) && mouseY>(attractors[i].y-10) && mouseY<(attractors[i].y+10))
{
stroke(255,0,0);
}
attractors[i].spot();
}
for (int i = 0; i < NUM_NEGATTRACTORS; i++)
{
if (mousePressed == true)
{
stroke(255,50);
}else{
stroke(0,50);
}
if (mouseX>(negattractors[i].x-10) && mouseX<(negattractors[i].x+10) && mouseY>(negattractors[i].y-10) && mouseY<(negattractors[i].y+10))
{
stroke(255,0,0);
}
negattractors[i].spot();
}
}
Vector3D getAccel(float _x, float _y)
{
float d2 = 0;
float ax = 0;
float ay = 0;
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
d2 = sq(attractors[i].x-_x) + sq(attractors[i].y-_y);
if (d2 > 0.1)
{
if (mouseX>(attractors[i].x-10) && mouseX<(attractors[i].x+10) && mouseY>(attractors[i].y-10) && mouseY<(attractors[i].y+10))
{
}else{
if (mousePressed == true)
{
ax = ax - accel/2 * (attractors[i].x-_x) / d2;
ay = ay - accel/2 * (attractors[i].y-_y) / d2;
} else {
ax = ax + accel * (attractors[i].x-_x) / d2;
ay = ay + accel * (attractors[i].y-_y) / d2;
}
}
}
}
for (int i = 0; i < NUM_NEGATTRACTORS; i++)
{
d2 = sq(negattractors[i].x-_x) + sq(negattractors[i].y-_y);
if (d2 > 0.1)
{
if (mouseX>(negattractors[i].x-10) && mouseX<(negattractors[i].x+10) && mouseY>(negattractors[i].y-10) && mouseY<(negattractors[i].y+10))
{
}else{
if (mousePressed == true)
{
ax = ax + accel * (negattractors[i].x-_x) / d2;
ay = ay + accel * (negattractors[i].y-_y) / d2;
} else {
ax = ax - accel/2 * (negattractors[i].x-_x) / d2;
ay = ay - accel/2 * (negattractors[i].y-_y) / d2;
}
}
}
}
Vector3D pos = new Vector3D(ax,ay);
return pos;
}
}
class Attractor {
float x,y;
Attractor(float _x, float _y )
{
x = _x;
y = _y;
}
void spot()
{
line(x-4, y, x+4, y);
line(x, y-4, x, y+4);
}
}
class Vector3D
{
float x;
float y;
float z;
Vector3D (float x_, float y_, float z_)
{
x=x_;
y=y_;
z=z_;
}
Vector3D (float x_, float y_)
{
x=x_;
y=y_;
z=0;
}
Vector3D ()
{
x=0;
y=0;
z=0;
}
void add (Vector3D v)
{
x =v.x+x;
y =v.y+y;
z =v.z+z;
}
void subtract(Vector3D v)
{
x =v.x-x;
y =v.y-y;
z =v.z-z;
}
void mult (float n)
{
x=x*n;
y=y*n;
z=z*n;
}
float distance (Vector3D v)
{
float distance= sqrt (pow(v.x-x,2) + pow(v.y-y,2) + pow(v.z-z,2) );
return distance;
}
float magnitude()
{
return (float) sqrt (pow(x,2) + pow(y,2) + pow(z,2) )
}
void set (float xx, float yy, float zz)
{
x=xx;
y=yy;
z=zz;
}
void set (Vector3D v)
{
x=v.x;
y=v.y;
z=v.z;
}
}
class Particle {
float x,y;
float r;
float t=0;
float xp;
float yp;
float sinTranslateCount=0;
float curveTranslateCount = 0;
color c=color(73,175,159,5);
float colorT=0;
color c1=color (73,175,159,5);
color c2=color (255,255,0,5);
color c3=color (255,5);
float w=1;
float h=1;
float accel;
float distance;
boolean d=false;
float startxx;
float startyy;
float endxx;
float endyy;
Vector3D pos =new Vector3D(0,0,0);
Vector3D v =new Vector3D(0,0,0)
Particle(float X, float Y, float W, float H, float R)
x = 0;
y = random(height);
pos.x=X;
pos.y=Y;
w=W;
h=H;
r=R;
}
void checkDistance(int count)
{
stroke(c1);
if (mousePressed == true)
{
for (int j = 0; j < NUM_NEGATTRACTORS; j++)
{
distance = sqrt( sq(negattractors[j].x-x) + sq(negattractors[j].y-y));
if (distance<50)
{
stroke(lerpColor(c1,c3,distance/50));
}
}
}else{
for (int i = 0; i < NUM_ATTRACTORS; i++)
{
distance = sqrt( sq(attractors[i].x-x) + sq(attractors[i].y-y));
if (distance<100)
{
stroke(lerpColor(c1,c2,distance/100));
}
}
}
if (key == CODED) {
if (keyCode == DOWN) {
{
if (distance>50)
{
w=distance*2;
h=distance*2;
drawEllipse();
}
}
}
}
line(startxx, startyy, endxx, endyy);
}
void step()
{
pos = forcefield.getAccel(x,y);
float xstart = x;
float ystart = y;
x += pos.x;
y += pos.y;
float xend = x;
float yend = y;
endxx=xend;
endyy=yend;
startxx=xstart;
startyy=ystart;
stroke(0,0,200,1)
point (x,y);
}
void inflate(float rateX, float rateY)
{
w=w*rateX;
h=h*rateY;
}
void drawEllipse()
{
float s=100.0/((w+h)/4);
for (float t=0; t<=360; t=t+s)
{
float a=endxx+(w/5)*cos(radians(t))* cos(radians(r))-h/2*sin(radians(t))*sin(radians(r));
float b=endyy+(h/5)*sin(radians(t))* cos(radians(r))+w/2*cos(radians(t))*sin(radians(r));
stroke(50 ,255/(distance/2));
point(a,b);
}
}
}