読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

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

マウスでエネルギーを与えることができるパーティクル

/**
* energizable particles 
*
* @author aa_debdeb
* @date 2016/11/27
*/

ArrayList<Particle> particles;
color c1 = color(79, 208, 255);
color c2 = color(255, 127, 80);

void setup(){
  size(640, 640);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 15; i++){
    particles.add(new Particle());
  }
}

void draw(){
  background(100);
  noStroke();
  fill(0);
  for(Particle p: particles){
    p.display();
    p.update();
  }
}

class Particle {
  
  PVector loc, vel;
  float size;
  
  Particle(){
    size = random(40, 80);
    loc = new PVector(random(size / 2, width - size / 2), random(size / 2, height - size / 2));
    float velSize = random(5, 20);
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
  }
  
  void display(){
    fill(lerpColor(c1, c2, constrain(vel.mag() / 5.0, 0, 1)), 200);
    ellipse(loc.x, loc.y, size, size);
  }
  
  void update(){
    float distance = PVector.dist(loc, new PVector(mouseX, mouseY));
    if(mousePressed && distance < size / 2){
      float velSize = vel.mag() * 1.1;
      float velAng = random(TWO_PI);
      vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
    }
    vel.mult(0.99);
    loc.add(vel);
    if(loc.x < size / 2){
      vel.x *= -1;
      loc.x += vel.x;
    }
    if(loc.x > width - size / 2){
      vel.x *= -1;
      loc.x += vel.x;
    }
    if(loc.y < size / 2){
      vel.y *= -1;
      loc.y += vel.y;
    }
    if(loc.y > width - size / 2){
      vel.y *= -1;
      loc.y += vel.y;
    }
  }
  
}
f:id:aa_debdeb:20161120144344j:plain