日常の進捗

主に自分のための,行為とその習慣化の記録

Mod:Coding Challenge #4: Purple Rain in Processing

パラメータにY方向の重力以外にも、X方向の風を追加した。あと矢印にしたら逆に雨ではなくなった。マウスで重力と風を制御出来る。

int num = 500;
Drop[] drops = new Drop[num];

void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100, 100);
  for (int i = 0; i < num; i++) {
    drops[i] = new Drop();
  }
  background(280, 25, 95);
}

void draw() {
  background(280, 10, 100);
  for (Drop d : drops) {
    d.update();
    d.draw();
  }
}

class Drop {
  float x;
  float y;
  float z;
  float len;
  float ySpeed;
  float xSpeed;

  Drop() {
    x = random(-width-500, width+500);
    y = random(-height-100, -100);
    z = random(0, 20);
    len = map(z, 0, 20, 10, 50);
    ySpeed = map(z, 0, 20, 1, 5);
    xSpeed = map(z, 0, 20, 1, 5);
  }

  void update() {
    y = y + ySpeed;
    x = x + xSpeed;
    
    float gMin = map(mouseY,0,height,0.01,0.05);
    float gMax = gMin * 5f;
    float wMin = map(mouseX,0,width,-0.03,0.03);
    float wMax = wMin * 5f;

    float gravity = map(z, 0, 20, gMin,gMax);
    float wind = map(z, 0, 20,wMin,wMax);
    ySpeed = ySpeed + gravity;
    xSpeed = xSpeed + wind;
    if (y > height || x > width) {
      x = random(-width-500, width+500);
      y = random(-height-100, -100);
      z = random(0, 20);
      len = map(z, 0, 20, 10, 50);
      ySpeed = map(z, 0, 20, 1, 5);
      xSpeed = map(z, 0, 20, 1, 5);
    }
  }

  void draw() {
    float theta = atan2(ySpeed, xSpeed);
    pushMatrix();
    translate(x, y+len/2);
    rotate(theta-PI/2);
    float sw = map(z, 0, 20, 1, 2);   
    strokeWeight(sw);
    stroke(270, 60, 100);
    noFill();
    line(0, -len/2, 0, len/2);

    fill(270, 60, 100);
    noStroke();
    quad(0, len/2, -len/6, len*2/3-len/2, 0, len*3/4-len/2, len/6, len*2/3-len/2);

    popMatrix();
  }
}

リファレンス