Particle[] p = new Particle[100];
Vec testVector;
Vec [] attractor = new Vec[5];
void setup()
{
size(900,250);
testVector= new Vec(10,10,0);
for(int i=0; i<100; i++)
{
p[i]= new Particle (50,50,5,5,30);
}
for (int i=0; i<5; i++) attractor[i] = new Vec (random(width), random (height), 0);
int q = 0;
for (int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
p[q].pos.x = i*width/9;
p[q].pos.y = j*height/9;
q++;
}
}
}
void draw()
{
fill(0,35);
noStroke();
rect(0,0, width, height);
fill(#111111);
stroke(255);
stroke(255);
fill(255);
Vec v1=new Vec (20,20,0);
Vec v2=new Vec (30,30,0);
v1.add(v2);
for(int i=0; i<100; i++)
{
p[i].xp=100;
p[i].yp=100;
float distance = sqrt(pow(mouseX-p[i].pos.x, 2) + pow(mouseY - p[i].pos.y, 2));
if(distance < 200)
{
p[i].colorT+=.01;
p[i].checkColor(color(255,0,0), color(255));
p[i].curveTranslate(p[i].pos.x, p[i].pos.y, noise(.8, 2), 1.5, 3, 3, .4);
}
else
{
p[i].curveTranslate(p[i].pos.x, p[i].pos.y, .2, 1.5, 3, 3, .1);
Vec aPoint= new Vec(width/2, height/2, 0);
p[i].attract( attractor);
}
p[i].drawParticle();
}
}
class Vec{
float x, y, z;
Vec(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
void add (Vec v)
{
x = x + v.x;
y = y + v.y;
z = z + v.z;
}
void subtract(Vec v)
{
x = x - v.x;
y = y - v.y;
z = z - v.z;
}
void multiply (float m)
{
x = x*m;
y = y*m;
z = z*m;
}
float distance (Vec v)
{
float d = sqrt (pow(v.x-x, 2) + pow(v.y-y, 2) + pow(v.z-z, 2));
return d;
}
float magnitute ()
{
float d = sqrt (pow(x, 2) + pow(y,2 ) + pow(z, 2));
return d;
}
void set(float xx, float yy, float zz)
{
x = xx;
y = yy;
z = zz;
}
}
class Particle
{
float w;
float h;
Vec pos = new Vec(0,0,0);
float accel = 1;
float r;
float xp, yp
float t=0;
float sinTranslatecount=0;
float curveTranslateCount=0;
float colorT=0;
color c= color(255,0);
color c1=color(#CEA9A9);
color c2=color(#00AFD8);
int colorCount=0;
Vec v= new Vec(5,5,0);
Vec ppos;
float inc1 = 0.1;
float n1 = 0.09;
float inc2 = .1;
float n2 = 0.09;
Particle(float X, float Y, float W, float H, float R)
{
pos.x=X;
pos.y=Y;
w=W;
h=H;
r=R;
}
void inflate(float rate)
{
w=w*rate;
h=h*rate;
}
void attract(Vec [] a)
{
for(int i=0; i <a.length; i++)
{
float d =pos.distance(a[i]);
if (d < 10)
{
v.x += accel * (a[i].x - pos.x) / pos.distance(a[i]);
v.y += accel * (a[i].y - pos.y) / pos.distance(a[i]);
}
else
{
v.x += accel * (a[i].x - pos.x) / pos.distance(a[i])/10;
v.y += accel * (a[i].y - pos.y) / pos.distance(a[i])/10;
}
if (d <2) accel=.995
else accel=1.02;
}
v.multiply(.98);
}
void checkColor(color c1, color c2)
{
if(c != color (255))
{
colorCount++;
}
if (colorCount>60)
{
c=c2;
colorCount=0;
}
if (colorT<0) colorT=0;
if (colorT>1) colorT=1;
}
void sinTranslate(float xt, float yt, float rt)
{
pos.x=(1-t)*xp+t*xt;
pos.y=(1-t)*yp+t*yt;
t=t+.01;
t= sin(sinTranslatecount);
sinTranslatecount= sinTranslatecount+0.1;
}
void translate(float xt, float yt, float rt)
{
if(t<=1)
{
pos.x=(1-t)*xp+t*xt;
pos.y=(1-t)*yp+t*yt;
t=t+1;
}
}
void curveTranslate(float xx, float yy, float sx, float sy, float rx, float ry, float speed)
{
curveTranslateCount+=speed;
pos.x = xx+(cos(curveTranslateCount*sx)* rx);
pos.y = yy+(sin(curveTranslateCount*sy)* ry);
}
void drawParticle()
{
checkColor(color(255,0,0), color(255));
float s= 20/(w+h);
pos.add(v);
strokeWeight(1);
float y1 = (noise(n1) - 0.5) * 55.0;
float y2 = (noise(n2) - 0.5) * 55.0;
line(pos.x-10, pos.y + y1, pos.x, pos.y);
line(pos.x+10, pos.y + y2, pos.x, pos.y);
n1 += inc1;
n2 += inc2;
stroke(lerpColor(c1,c2,constrain(colorT,0,1)));
}
}