fractalkitty looking for math? fractalkitty.com

borbs

flocker

#random#physics#classes#interactive#animation#math#animals

bArraycrowdDistcohDistdistxdistydistrandomnessdistArraycrowFactorcohFactordirFactorbirdiesgetDistancenewV

let bArray = [];
let crowdDist = 10;
let cohDist = 100;
let dist,xdist,ydist;
let randomness = 0.2
let n=500;
let distArray;
let crowFactor=0.09,cohFactor=0.1,dirFactor=0.03;
function setup(){
  createCanvas(windowWidth,windowHeight);
  background(0,20,30);
  for(let i=0;i<n;i++){
    bArray.push(new birdies(random(-width/2,width/2),random(-width/2,width/2)));
  }
  angleMode(DEGREES);
  noStroke()
}
function draw(){

  t=frameCount
  n=bArray.length;
  translate(width/2,height/2)
  
  fill(202, 255, 251)
  background(0,20,50,30)
  noStroke();
  for(let i=0;i<n;i++){
  fill(i/2+100,i/2+100,150+i/5)
    bArray[i].display();
    newV(i);
    
  }
fill(0,20,50,120)
  rect(-width/2,-height/2,300,50)
  fill(255)
  stroke(255)
  text('Press Space for behavior change',-width/2+20,-height/2+20);
  text('Click to add more.',-width/2+20,-height/2+40);
}

class birdies{
  constructor(x,y){
    this.x = x;
    this.y = y;
    this.vx = random(-2,2);
    this.vy = random(-2,2);
  }
  display(){
    circle(this.x,this.y,4);
  }

}

function getDistance(x1,y1,x2,y2){
  dist = sqrt(pow((x1-x2),2)+pow((y2-y1),2));
  xdist = x1-x2;
  ydist = y1-y2;
  
}

function newV(p){
  cArray = [];
  cntCrow=1;
  cntCoh=1;
  sumxA=0;
  sumyA=0;
  sumxB=0;
  sumyB=0;
  sumvx=0;
  sumvy=0;
  crow=0;
  coh=0;
  if (bArray[p].x>width/2){
    bArray[p].x=bArray[p].x-width;
  }
  if (bArray[p].x<-width/2){
    bArray[p].x=bArray[p].x+width;
  }
  if (bArray[p].y>height/2){
    bArray[p].y=bArray[p].y-height;
  }
  if (bArray[p].y<-height/2){
    bArray[p].y=bArray[p].y+height;
  }
  if(abs(bArray[p].vx)>2){
    bArray[p].vx=bArray[p].vx/1.5
  }
  if(abs(bArray[p].vy)>2){
    bArray[p].vy=bArray[p].vy/1.5
  }
  for(let i=0;i<n;i++){
    if(i!==p){
      getDistance(bArray[p].x,bArray[p].y,bArray[i].x,bArray[i].y);
      if(dist<=crowdDist){
        cArray.push(bArray[i]);
        distArray = [{i,dist,xdist,ydist}];
        sumxA+=xdist;
        sumyA+=ydist;
        cntCrow++
      }
      if(dist<=cohDist&&dist>crowdDist){
        cArray.push(bArray[i]);
        sumxB+=xdist;
        sumyB+=ydist;
        sumvx+=bArray[i].vx
        sumvy+=bArray[i].vy
        cntCoh++
      }
    }
  }
  avgXcrowd = sumxA/cntCrow;
  avgYcrowd = sumxA/cntCrow;
  deltaXcrowd = avgXcrowd/crowdDist;
  deltaYcrowd = avgYcrowd/crowdDist;
  avgXcoh = sumxB/cntCoh;
  avgYcoh = sumxB/cntCoh;
  deltaXcoh = avgXcoh/cohDist;
  deltaYcoh = avgYcoh/cohDist;
  avgVx = sumvx/cntCoh;
  avgVy = sumvy/cntCoh;
  bArray[p].vx = bArray[p].vx+deltaXcrowd*crowFactor+deltaXcoh*cohFactor+avgVx*dirFactor+random(-randomness,randomness);
  bArray[p].vy = bArray[p].vy+deltaYcrowd*crowFactor+deltaYcoh*cohFactor+avgVy*dirFactor+random(-randomness,randomness);
  bArray[p].x=bArray[p].x+bArray[p].vx
  bArray[p].y=bArray[p].y+bArray[p].vy
  
}
function mousePressed(){
  if (n<600){
    
    bArray.push(new birdies(mouseX-width/2,mouseY-height/2))
  }
}

function keyPressed() {
  if (keyCode === 32) {
    crowFactor= random(-0.1,0.1);
    cohFactor=random(-0.4,0.4)
    dirFactor=random(-0.1,0.1)
    randomness = random(0,0.3)
  }
}