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);
particles.add(new Part(new PVector(width/2+r*sin(radians(omega)),height/2+r*cos(radians(omega)),0)));
};

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);
  centx=width/2+400*abs(sin(alpha/200))*sin(radians(alpha));
  centz=0;
  centy=height/2+400*abs(sin(alpha/200))*cos(radians(alpha));


  for (int i=particles.size()-1;i>=0;i--){
   Part p=particles.get(i);
   p.run();
   if (p.isDead()) particles.remove(i);
   };
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.add(f);
  PVector rad=PVector.sub(new PVector(centx,centy,centz-50),loc);
  rad.mult(s);
  rad.mult(0.12*(l/2));
  vel.add(rad);
  vel.limit(7);
};

void move(){
  loc.add(vel);
  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();
  };
 
boolean isDead(){
   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. 😉

There are no comments.

Please Leave a Reply

Current ye@r *

TrackBack URL :

pagetop