fractalkitty looking for math? fractalkitty.com

100 days of code

churn_day26

#random#trig#physics#classes#3d#interactive#animation

bArrayBallmyball


let n = 400;
let bArray = []
function setup() {
  createCanvas(400, 400,WEBGL);
  fill(0)
  for (let i = 0; i < n; i++) {
    bArray.push(new Ball(random(-width/2,width/2),random(-width/2,width/2)))
  }
  angleMode(DEGREES)
  noStroke()
}

function draw() {
  translate(0,0,200*cos(frameCount/100)-200)
  ambientLight(50);
  orbitControl();
  pointLight(255, 255, 255, 0, 0, 80);
  background(100,120,140);
  
  for (i=0;i<n;i++){  
    ambientMaterial(bArray[i].color+100,bArray[i].color+140,bArray[i].color+160)
    bArray[i].move();
    bArray[i].display();
  }
}

class Ball {
  constructor(x,y) {
    
    this.x = random(-width/2,width/2);
    this.y = random(-width/2,width/2);
    this.r = random(10,50);
    this.vx = random(-2,3);
    this.vy = random(-2,3);
    this.color = random(20,90);
    this.angle = random(0,90);
  }
  move() {
    this.x=this.x+this.vy*sin(frameCount+5*this.vx)+this.vx*cos(frameCount+20*this.vx);
    this.y=this.y+this.vy*cos(frameCount-5*this.vy)-this.vx*sin(frameCount+20*this.vy);
  }
  display(){
    push()
    rotate(this.angle+frameCount)
    translate(this.x,this.y,this.x/2);
    ellipsoid(this.r,this.r/1.5,this.r/3,24,24);
    pop()
  }
}

function mousePressed() {
  let myball = new Ball((mouseX-width/2)/(sin(frameCount)*random(0,2)), mouseY-width/2);
  bArray.push(myball);
  n = n+1;
}