fractalkitty looking for math? fractalkitty.com

weekly creative coding

birb train

#trig#vectors#3d#sound#interactive#animation#animals#wcc

created for birb's nest train challenge

birbluneChoobirbTrainmakeTrackdrawTrack

let t1, t2;
let birb;
let luneChoo;
function preload() {
  birb = loadModel(
    "/assets/Epic%20Fulffy-Krunk%20(1).stl"
  );
  luneChoo = loadSound("/assets/luneycall.m4a");
}
function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
  angleMode(DEGREES);
  stroke(255);
  makeTrack(120, 120);
  luneChoo.amp(0.4);
}
function draw() {
  background(40);
  t = frameCount;
  rotateX(t / 3);
  rotateY(t / 5);
  orbitControl();
  drawTrack();
  birbTrain();
  push();
  translate(100, 20, -20);
  rotateZ(120);
  drawTrack();
  birbTrain();
  pop();
}
function birbTrain() {
  for (let i = 0; i < 20; i++) {
    push();
    normalMaterial();
    t = frameCount + 20 + (360 / 20) * i;
    translate(t1[t % 360].x, t1[t % 360].y - 15, t1[t % 360].z);
    rotateZ(t - 90);
    model(birb);
    pop();
  }
}
function makeTrack(a, b) {
  t1 = [];
  t2 = [];
  d = 10;
  for (let i = 0; i <= 360; i++) {
    t1[i] = createVector(a * cos(i), b * sin(i) + 20 * sin(i * 3), a * sin(i));
    t2[i] = createVector(
      (a + d) * cos(i),
      (b + d) * sin(i) + 20 * sin(i * 3),
      (a + d) * sin(i)
    );
  }
}

function drawTrack() {
  for (let i = 1; i <= 360; i++) {
    line(t1[i - 1].x, t1[i - 1].y, t1[i - 1].z, t1[i].x, t1[i].y, t1[i].z);
    line(t2[i - 1].x, t2[i - 1].y, t2[i - 1].z, t2[i].x, t2[i].y, t2[i].z);
    line(t1[i].x, t1[i].y, t1[i].z, t2[i].x, t2[i].y, t2[i].z);
  }
}
function keyPressed() {
  if (!luneChoo.isPlaying()) {
    luneChoo.play();
  }
}