Daily Creative Coding

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

水面から浮き上がる物体

/**
* Object Coming to The Surface
*
* @author aa_debdeb
* @date 2016/02/10
*/

float position;
float speed = -5;
float radious = 150;
float surface = 400;

void setup(){
  size(500, 500);
  frameRate(30);
  smooth();
  stroke(128);
  strokeWeight(5);
  
  position = radious;
}

void draw(){
  background(255);

  float startAng = PI + asin(position / radious);  
  float stopAng = TWO_PI -asin(position / radious);
  beginShape();
  curveVertex(0, surface);
  curveVertex(0, surface);
  curveVertex(width / 2 - radious, surface);
  float angStep = (stopAng - startAng) / 16;
  for(int i = 1; i < 16; i++){
    float ang = startAng + angStep * i;
    PVector arcPoint = new PVector(width / 2 + radious * cos(ang), surface + position + radious * sin(ang));
      curveVertex(arcPoint.x, arcPoint.y);
  }
  curveVertex(width / 2 + radious, surface);
  curveVertex(width, surface);
  curveVertex(width, surface);
  endShape();
    
  position += speed;
  if(position <= -radious || radious <= position){
    speed *= -1;
  }
}