Daily Creative Coding

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

マウスを中心にオブジェクトを回転させる

/**
* force for rotation
*
* @author aa_debdeb
* @date 2016/07/27
*/

ArrayList<PVector> particles;

void setup(){
  size(500, 500);
  noStroke();
  fill(255, 255, 0, 200);
  particles = new ArrayList<PVector>();
  for(int i = 0; i < 3000; i++){
    float radious = map(sqrt(random(1)), 0, 1, 0, 800);
    float angle = random(TWO_PI);
    particles.add(new PVector(radious * cos(angle), radious * sin(angle)));
  }
}

void draw(){
  background(255, 0, 255);
  translate(width / 2, height / 2);
  for(PVector p: particles){
    ellipse(p.x, p.y, 50, 50);
  }
  if(mousePressed){
    PVector mouse = new PVector(mouseX - width / 2, mouseY - height / 2);
    for(PVector p: particles){
      float d = p.dist(mouse);
      if(d < 500){
        PVector force = PVector.sub(p, mouse);
        force.rotate(HALF_PI);
        force.normalize();
        force.mult(map(d, 0, 500, 6, 0));
        p.add(force);
      }
    }
  }
}