﻿ particle system « Coding, Sounds and Colors | A blog about algorithmic experiments in music and visual art. Sort of.

## Dancing Stars

posted by on 2013.05.18, under Processing
18:

This code came out by playing with particle systems in Processing.
I have talked about particle systems in the post before in the context of generating a still image. In this particle system there is an important and conceptual difference, though: the number of particles is not conserved. Each particle has a life span, after which the particle is removed from the system: this is done by using ArrayList rather than a simple array. In the rendered video below, you will see clearly that particles start to disappear. The disappearing of particles could have been in principle obtained by simply gradually reducing the opacity of a given particle to 0, rather than removing it: in other words, the particle is still there, it’s only invisible. In the particle system I have used, the two approaches make no difference, apart from saving computational resources (which still is a big deal!), since the particles do not interact among each other. If inter particle interactions were present, the situation would be completely different: an invisible particle would still interact with the others, hence it would not be really “dead”.
Believe it or not, to accept the fact that the number of particles in a system might not be conserved required at the beginning of the 20th century a huge paradigm shift in the way physical systems, in particular at the quantum scale, are described, giving a good reason (among others) to develop a new physical framework, called Quantum Field Theory.

import processing.opengl.*;

int N=500*30;
ArrayList<Part> particles= new ArrayList<Part>();
float t=0;
float p=1;
float centx,centy,centz;
float s=0.01;
float alpha=0;
float st=0;

void setup(){
size(500,500,OPENGL);
background(0);
directionalLight(126, 126, 126, 0, 0, -1);
ambientLight(102, 102, 102);

centx=width/2;
centy=height/2;
centz=0;
for (int i=0;i<N;i++){
float r=random(0,500);
float omega=random(0,360);
};

for (int i=0;i<particles.size();i++){
Part p=particles.get(i);
p.display();
};

};

void draw(){
if (random(0.0,1.0)<0.01) {
t=0;
st=random(0.002,0.003);
p=random(1.0,1.3);
};
s+=(st-s)*0.01;
background(0);
directionalLight(255, 255, 255, 0, 4, -10);
ambientLight(255, 255, 255);
centz=0;

for (int i=particles.size()-1;i>=0;i--){
Part p=particles.get(i);
p.run();
};
t+=0.01;
alpha+=5;
};
%%%%Define the class Part
class Part{

PVector loc;
PVector vel;
float l;
float lifesp;

Part(PVector _loc){
loc=_loc.get();
vel=new PVector(random(-0.5,0.5),random(-0.5,0.5),0.1*random(-0.1,0.1));
l =random(2.0,5.0);
lifesp=random(255,2000);
};

void applyForce(float t){
PVector f= newPVector(random(-0.1,0.1)*sin(noise(loc.x,loc.y,t/2)),random(-0.1,0.1)*cos(noise(loc.x,loc.y,t/2)),0.01*sin(t/10));
f.mult(0.1*p);
vel.limit(7);
};

void move(){
lifesp-=1;
};

void display(){
noStroke();
fill(255,random(130,220));
pushMatrix();
translate(loc.x,loc.y,loc.z);
ellipse(0,0,l,l);
popMatrix();
};

void run(){
applyForce(t);
move();
display();
};

if (lifesp<0.0){
return true;
} else {
return false;
}
}

};

Here you find a render of the video (which took me quite some time…), which has being aurally decorated with a spacey drone, because… well, I like drones. 😉

## Floating

posted by on 2013.05.11, under Processing
11:

Here’s a code which uses the idea of a “particle system”, i.e. a system which consists of a number of objects (which may vary with time) interacting among themselves, and with their environment. In this case, I have just used 3 particles which are subject to a centripetal force, and that repulse each other. Once you set up a basic interaction system, you can think about how to represent at each time the data produced by the system: in this case I have choose to draw for each particle a rotating line, whose properties, like its transparency, depend on some other parameters (distance from the center).
The interesting thing about using particle systems to make visual art is that in principle one is dealing with a “parameter space”, which doesn’t need to be mapped via the intuition which it was built in the first place with: in other words, there’s no need to represent the objects as actual particles (with positions, velocity, etc.), but one can consider the system as a machine that produces data, to be then represented in the way one artistically feels like. The advantage of using a particle system is that all these data will evolve according to some dynamical evolution which is (in general) not totally random, giving rise to patterns in the final artistic outcome.

int d=3;
Agent[] point=new Agent[d];
void setup(){
randomSeed(90);
size(500,500);
background(255);
for (int i=0;i<d;i++)
{
point[i]= new Agent(random(10,width),random(10,height),random(-1.5,1.5),random(-1.5,1.5));
};
};

void draw(){
for (int i=0;i<d;i++)
{
point[i].resetrep();
for (int j=0;j<d;j++)
{
if (i!=j) point[i].interact(point[j]);
};
};
for (int i=0;i<d;i++)
{
point[i].applyForce();
point[i].move();
point[i].paint();
};
};

class Agent{
PVector loc;
PVector vel;
PVector rep;
float alpha;
int tran;
float l;
float mag=0;

Agent(float _locx,float _locy, float _velx,float _vely){
loc=new PVector(_locx,_locy);
vel=new PVector(_velx,_vely);
rep=new PVector(0,0);
alpha=0;
tran=10;
l=random(20,50);
};

void resetrep(){
rep.x=0;
rep.y=0;
}

void interact(Agent other){
PVector reps=PVector.sub(loc,other.loc);
float m=reps.mag();
reps.normalize();
reps.mult(3/(m+0.0001));
};

void applyForce(){
PVector force=new PVector(random(-0.02,0.02),random(-0.02,0.02));
force.limit(1);
vel.limit(6);
};

void move(){
alpha+=2;
};

void paint(){
pushMatrix();
translate(loc.x,loc.y);
popMatrix();
};
};

Here’s a screenshot of the animation after a few seconds.