xxxxxxxxxx
72
let mover;
function setup() {
createCanvas(900, 900);
background(220);
mover = new Mover(0, 0);
}
function draw() {
mover.update();
}
class Mover{
constructor(x, y){
this.pos = createVector(x, y);
this.vel = createVector(1, 1);
this.w = 10;
this.lar = createVector(1, 0);
this.lal = createVector(1, 0);
this.lar.setHeading(this.vel.heading() + HALF_PI);
this.lal.setHeading(this.vel.heading() - HALF_PI);
this.lar.setMag(this.w);
this.lal.setMag(this.w);
this.lr = p5.Vector.add(this.pos, this.lar);
this.ll = p5.Vector.add(this.pos, this.lal);
this.nf = 0;
this.nscale = 100;
this.chance = 0;
this.gh = this.vel.heading();
}
update(){
this.nf = noise(this.pos.x / this.nscale, this.pos.y / this.nscale);
this.w = this.nf * 20;
this.lar = createVector(1, 0);
this.lal = createVector(1, 0);
this.lar.setHeading(this.vel.heading() + HALF_PI);
this.lal.setHeading(this.vel.heading() - HALF_PI);
this.lar.setMag(this.w);
this.lal.setMag(this.w);
this.lr = p5.Vector.add(this.pos, this.lar);
this.ll = p5.Vector.add(this.pos, this.lal);
this.chance = random(1000);
if(this.chance > 990){
mover.tl();
this.gh = this.vel.heading() - HALF_PI;
}
if(this.chance < 10){
mover.tr();
this.gh = this.vel.heading() + HALF_PI;
}
line(this.lr.x, this.lr.y, this.ll.x, this.ll.y);
this.pos.add(this.vel);
}
tl(){
this.vel.setHeading(this.vel.heading() - 0.1);
if(this.vel.heading() > this.gh){
mover.tl();
}
}
tr(){
this.vel.setHeading(this.vel.heading() + 0.1);
if(this.vel.heading() < this.gh){
mover.tl();
}
}
}