• fullscreen
  • particle.pde
  • particleSystem.pde
  • public class particle {
      PVector position;
      PVector velocity;
      PVector angle;
      PVector angularVelocity;
      float radius;
      float life;
      boolean alive;
    
      public particle() {
        position=new PVector();
        velocity=new PVector();
        life=0;
        angle=new PVector();
        angularVelocity=new PVector();
        alive=false;
      }
    
      public particle(PVector position) {
        this.position=position;
        velocity=new PVector();
        radius=(float) random(3,10);
        life=(int) random(10,1000);
        angle=new PVector();
        angularVelocity=new PVector((float) random(0,0.05),(float) random(0,0.05),(float) random(0,0.05));
        alive=true;
      }
    
      public particle(PVector position, PVector velocity) {
        this.position=position;
        this.velocity=velocity;
        radius=(float) random(3,10);
        life=(int) random(10,1000);
        angle=new PVector();
        angularVelocity=new PVector((float) random(0,0.05),(float) random(0,0.05),(float) random(0,0.05));
        alive=true;
      }
    
      public void move() {
        position=PVector.add(position,velocity);
        angle=PVector.add(angularVelocity,angle);
        life--;
        if(life<0)
          life=0;
      }
      public void plot() {
        pushMatrix();
        translate(position.x,position.y,position.z);
          pushMatrix();
          rotateX(angle.x);
            pushMatrix();
              rotateY(angle.y);
              pushMatrix();
                rotateZ(angle.z);
                box(radius);
              popMatrix();
            popMatrix();
          popMatrix();
        popMatrix();
      }
    }
    
    
    float rotX=0,rotY=0;
    float a=0;
    float b=0;
    ArrayList<particle> particles;
    void setup() {
      size(800,600,P3D);
      particles=new ArrayList<particle>();
      for(int i=0;i<(int) random(100,1000);i++) {
        float r=width/12;
        float theta=(float) random(0,TWO_PI);
        float phi=(float) random(0,PI);
        PVector p=new PVector(r*sin(theta)*sin(phi),r*cos(theta)*sin(phi),r*cos(phi));
        PVector v=PVector.mult(p,1/p.mag());
        v.mult((float) random(1,2));
        particles.add(new particle(p,v));
      }
    }
    void mouseDragged() {
      rotX+=(mouseX-pmouseX)*0.01;
      rotY-=(mouseY-pmouseY)*0.01;
    }
    void draw() {
      background(0);
      directionalLight(255,0,0,0,1,0);
      directionalLight(255,255,0,-1,0,0);
      directionalLight(255,0,0,0,-1,0);
      directionalLight(0,255,0,1,0,0);
      translate(width/2,height/2,0);
      rotateX(rotY);
      rotateY(rotX);
      pushMatrix();
      rotateZ(b);
      pushMatrix();
      rotateY(a);
      noFill();
      stroke(255);
      box(width/4);
      fill(255,150);
      noStroke();
      for(int i=0;i<particles.size();i++) {
        if(particles.get(i).alive)
          particles.get(i).plot();
      }
      for(int i=0;i<particles.size();i++) {
        particles.get(i).move();
        if(particles.get(i).position.mag()>width/3) {
          float r=(float) random(10,width/12);
          float theta=(float) random(0,TWO_PI);
          float phi=(float) random(0,PI);
          PVector p=new PVector(r*sin(theta)*sin(phi),r*cos(theta)*sin(phi),r*cos(phi));
          PVector v=PVector.mult(p,1/p.mag());
          v.mult((float) random(1,2));
          particles.get(i).position=p;
          particles.get(i).velocity=v;
          particles.get(i).life=(float) random(10,1000);
          particles.get(i).alive=true;
        }
      }
      popMatrix();
      popMatrix();
      a+=0.01;
      b+=0.01;
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    camilo rey

    simple particle system

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    this is a simple particle system. A source pushes particles like a fountain, when particles reach a particular distance from the source they reappear at the source and are pushed away.

    You need to login/register to comment.