Daily Creative Coding

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

球の表面でのパーティクルの移動

/**
* surface on sphere
*
* @author aa_debdeb
* @date 2016/04/18
*/

float radious = 250;
ArrayList<Particle> particles;
PVector rNoise1, rNoise2;
float nScale = 3.4;
float maxVel = PI / 2048;
float velStep = PI / 20480;

void setup(){
  size(640, 640, P3D);
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 30; i++){
    particles.add(new Particle());
  }
  rNoise1 = new PVector(random(100000), random(100000));
  rNoise2 = new PVector(random(100000), random(100000));
  
  background(32);
  stroke(0, 0, 255, 60);
}

void draw(){
  translate(width / 2, height / 2);
  for(Particle particle: particles){
    particle.update();
  }
}

class Particle{
  
  float r1, r2;
  float vr1, vr2;
  
  Particle(){
    r1 = random(TWO_PI);
    r2 = random(TWO_PI);
    vr1 = random(-maxVel, maxVel);
    vr2 = random(-maxVel, maxVel);
  }
  
  void update(){
    vr1 +=  map(noise(rNoise1.x + cos(r1) * nScale, rNoise1.y + sin(r1) * nScale), 0, 1, -velStep, velStep);
    constrain(vr1, -maxVel, maxVel);
    float nr1 = r1 + vr1;
    if(nr1 < 0){
      nr1 += TWO_PI;
    } else if(nr1 >= TWO_PI){
      nr1 -= TWO_PI;
    }
    vr2 += map(noise(rNoise2.x + cos(r2) * nScale, rNoise2.y + sin(r2) * nScale), 0, 1, -velStep, velStep);
    constrain(vr2, -maxVel, maxVel);
    float nr2 = r2 + vr2;
    if(nr2 < 0){
      nr2 += TWO_PI;
    } else if(nr2 >= TWO_PI){
      nr2 -= TWO_PI;
    }
    float x = radious * sin(r1) * cos(r2);
    float y = radious * sin(r1) * sin(r2);
    float z = radious * cos(r1);
    float nx = radious * sin(nr1) * cos(nr2);
    float ny = radious * sin(nr1) * sin(nr2);
    float nz = radious * cos(nr1);
    line(x, y, z, nx, ny, nz); 
    r1 = nr1;
    r2 = nr2;
  }
  
}