Code
//Pictoric Machine
int nParticles = 500;
Particle parray[];
float DRAG = 0.95;
color[] goodcolor =
{
#CD540A, #AE7450, #AD611E, #E97913, #4D3711,
#E4CC62, #AFAC37, #B1AF4F, #929553, #D1DE8C,
#D0E5AA, #5A8D67, #5A8D67, #334B4E, #3D737D,
#8798C9, #6D7590, #8B95BA, #A9A8B9, #F7F5F6,
#000000, #FFFFFF
};
void setup(){
size(380,420, P3D);
noStroke();
background(255);
parray = new Particle[nParticles];
for (int i=0; i<nParticles; i++){
float x = random(width);
float y = random(height);
color somecolor = goodcolor[int(random(goodcolor.length))];
parray[i] = new Particle(x,y,somecolor);
parray[i].alfa = 10;
parray[i].mass = random(20);
if (somecolor == 0xFFFFF){
parray[i].radius = random(1000);
}
else if (somecolor == 0x000000){
parray[i].radius = 2;
parray[i].alfa = 255;
parray[i].mass = 3000;
}
}
}
//=============================
void draw(){
//colors
for (int i=0; i< nParticles; i++){
parray[i].clearForces();
for (int j=0; j<i; j++){
//calculate distances
float dx = parray[i].px - parray[j].px;
float dy = parray[i].py - parray[j].py;
float dh = sqrt(dx*dx + dy*dy);
float fx = (mouseX/width) * dx/(dh*dh);
float fy = (mouseX/width) * dy/(dh*dh);
if (dh < 100){
fx = -fx;
fy = -fy;
}
if (parray[i].thiscolor == parray[j].thiscolor){
fx = 2 *(dx/dh);
fy = 2 *(dy/dh);
}
if(mousePressed) {
parray[i].accumulateForce( fx, fy);
parray[j].accumulateForce(-fx, -fy);
}
else{
parray[i].accumulateForce(-fx, -fy);
parray[j].accumulateForce( fx, fy);
}
}
float fx = random(-1,1);
float fy = random(-1,1);
parray[i].accumulateForce(fx,fy);
parray[i].update();
parray[i].render();
parray[i].screenwarp();
}
fill(0);
rect(0,0, width,3);
rect(0,0, 3,height);
rect(0,height-3, width,3);
rect(width-3,0, 3,height);
}
//=======================================
class Particle {
float px, py;
float vx, vy;
float fx, fy;
float mass;
float radius;
color thiscolor;
float alfa;
Particle(float xin, float yin, color col){
px = xin;
py = yin;
thiscolor = col;
vx = 0;
vy = 0;
fx = 0;
fy = 0;
mass = random(1,10);
radius = mass*2.1;
}
void clearForces(){
fx = 0;
fy = 0;
}
void accumulateForce(float gx, float gy){
fx += gx;
fy += gy;
}
void update(){
vx += fx / mass;
vy += fy / mass;
vx *= DRAG;
vy *= DRAG;
px += vx;
py += vy;
}
void screenwarp(){
if (py<0){
py += height;
}
if (px<0){
px += width;
}
else if (px>width){
px -= width;
}
}
void render(){
fill(thiscolor,alfa);
ellipse(px,py,radius,radius);
}
}
void keyPressed(){
if(key == ' ') {
background(255);
}
}
041 -- A System of Particles: (Due Wed Oct 25)
Statement:Develop an ecology or composition in which a population of animated forms reacts with attraction and/or repulsion according to at least two force-based influences. These influencing factors might include:
- The location of the cursor;
- "Food" sources, "predators", "walls", or other environmental features, possibly placed by the user (or not);
- Other members of the population, possibly according to some aspect of their own variability (for example, small ones might avoid big ones, etc.)
Your population should have at least 10 members. You may, at your option, eliminate the background() command to create an accretive composition; if you do, provide an option that allows a keypress to turn the background back on. See Craig Reynold's work for some possible inspiration. For example code, see the following: One-dimensional spring; Gravity and bouncing; Magnetic attraction & repulsion forces; Particle-particle interactions (collisions). (These examples use Processing V.068; to compile, change loop() to draw()).
Some things to experiment with: Experiment with turning off the background() command in order to make an accretive composition. Up for a challenge? Consider creating a 1-dimensional group of particles connected by springs (as in a simulation of a hair, chain or thread), or a 2D mesh of particles connected by springs (in other words, a fabric simulation), such as this one by Max Kaufmann. For the grand-daddy of spring apps, look at the Soda Constructor by Ed Burton. Also consider DynaDraw, which connects a pen to the cursor by a 2-dimensional spring. hide statement