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

Daily Creative Coding

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

曲がりくねった道を走る

3D
/**
* winding road
*
* @author aa_debdeb
* @date 2016/07/11
*/

float xScale = 300;
float yScale = 100;
float zScale = PI / 1024;
float speed = PI / 64;
float eyeZ = PI / 8;
float routeWidth = 100;

void setup(){
  size(500, 500, P3D);
}

void draw(){
  background(0);
  noFill();
  stroke(255, 165, 0);
  strokeWeight(3);
  translate(width / 2, height / 2, 0);
  float x1 = getX(0);
  float y1 = getY(0);
  float z = -eyeZ / zScale;
  float x2 = getX(z);
  float y2 = getY(z);
  camera(x1, y1, 0, x2, y2, z, 0, 1, 0);
  int[] rx = {-1, -1, 1, 1};
  int[] ry = {-1, 1, -1, 1};
  for(int i = 0; i < 4; i++){
    pushMatrix();
    translate(rx[i] * routeWidth, ry[i] * routeWidth, 0);
    drawLine();
    popMatrix();  
  }
}

void drawLine(){
  beginShape();
  for(float z = 0; z > -6000; z -= 5){
    float x = getX(z);
    float y = getY(z);
    vertex(x, y, z);
  }
  endShape();
}

float getX(float z){
  return map(sin(z * zScale - frameCount * speed), -1, 1, -xScale, xScale);
}

float getY(float z){
  return map(cos(z * zScale - frameCount * speed), -1, 1, -yScale, yScale);
}