Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

パーティクル

JavaScriptモードだとcamera関数が機能しない?

/**
* 3D Particle System
*
* @author aa_debdeb
* @date 2015/10/03
*/

float PARTICLE_RADIOUS = 3.0;
float PARTICLE_NUM = 1000;
float FORCE = 2.0;

ArrayList<Particle> particles;

void setup(){
  size(500, 500, P3D);
  smooth();
  frameRate(24);
  noStroke();
  lights();
  camera(0, 0, -250, 0, 0, 0, 0, 1, 0);
  sphereDetail(10);
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < PARTICLE_NUM; i++){
    particles.add(new Particle());
  }
}

void draw(){  
  background(0);

  for(Particle particle : particles){
    particle.draw();
  }
  for(Particle particle : particles){
    particle.update();
  }
}

class Particle{
  PVector position;
  PVector velocity;
  color c;
  
  
  Particle(){
    float radious = random(100);
    float r1 = random(PI);
    float r2 = random(2 * PI); 
    position = new PVector(radious * sin(r1) * cos(r2), radious * sin(r1) * sin(r2), radious * cos(r1));
    velocity = new PVector(random(20) - 10.0, random(20) - 10.0,  random(20) - 10.0);
    c = color(random(255), random(255), random(255));
  }
  
  void draw(){
    fill(c);
    pushMatrix();
    translate(position.x, position.y, position.z);
    sphere(PARTICLE_RADIOUS);
    popMatrix();
  }
  
  void update(){
    float radious = sqrt(sq(position.x) + sq(position.y) + sq(position.z));
    float xyRadious = sqrt(sq(position.x) + sq(position.y));
    float cosR1 = position.z / radious;
    float sinR1 = xyRadious / radious;
    float cosR2 =  position.x / xyRadious;
    float sinR2 =  position.y / xyRadious;
    PVector acceleration = new PVector(FORCE * -1.0 * sinR1 * cosR2, FORCE * -1.0 * sinR1 * sinR2, FORCE * -1.0 * cosR1);
    velocity.add(acceleration);
    position.add(velocity);
  }
  
  
}