xxxxxxxxxx
93
let history = [];
let movers = [];
let i = 0;
let dis = 0;
function setup() {
createCanvas(600, 600);
for(i = 0; i < 1; i++){
movers[i] = new Mover(100, random(height));
}
background(220);
}
function draw() {
for(i = 0; i < movers.length; i++){
movers[i].update();
}
}
class Mover{
constructor(x, y){
this.cpos = createVector(x, y);
this.vel = createVector(4, 0);
this.vel.setHeading(PI / 4);
this.x = x;
this.y = y;
this.d = 0;
this.push = createVector(0, 0);
this.i = 0;
this.wall = createVector(0, 0);
this.r1 = random(255);
this.r2 = random(255);
this.g1 = random(255);
this.g2 = random(255);
this.b1 = random(255);
this.b2 = random(255);
}
update(){
this.x = this.cpos.x;
this.y = this.cpos.y;
history[history.length] = createVector(this.x, this.y);
this.vel.setHeading((this.vel.heading() - 0.3) + noise(this.cpos.x / 100, this.cpos.y / 100) * 0.6);
// this.vel.setHeading(this.vel.heading() + 0.01)
if(history.length > 60){
for(this.i = 0; this.i < history.length - 60; this.i ++){
if(dist(this.cpos.x, this.cpos.y, history[this.i].x, history[this.i].y) < 20){
this.push = p5.Vector.sub(this.cpos, history[this.i]);
this.d = dist(this.cpos.x, this.cpos.y, history[this.i].x, history[this.i].y);
this.push.setMag(sq(map(this.d, 0, 20, 3, 0)) * 0.015);
this.vel.add(this.push);
}
}
}
if(this.cpos.x < 100){
this.wall = createVector(0, this.cpos.y);
this.d = dist(this.cpos.x, this.cpos.y, this.wall.x, this.wall.y);
this.push = p5.Vector.sub(this.cpos,this.wall);
this.push.setMag(map(this.d, 0, 100, 0.1, 0));
this.vel.add(this.push);
}
if(this.cpos.y < 100){
this.wall = createVector(this.cpos.x, 0);
this.d = dist(this.cpos.x, this.cpos.y, this.wall.x, this.wall.y);
this.push = p5.Vector.sub(this.cpos,this.wall);
this.push.setMag(map(this.d, 0, 100, 0.1, 0));
this.vel.add(this.push);
}
if(this.cpos.x > width -100){
this.wall = createVector(width, this.cpos.y);
this.d = dist(this.cpos.x, this.cpos.y, this.wall.x, this.wall.y);
this.push = p5.Vector.sub(this.cpos,this.wall);
this.push.setMag(map(this.d, 0, 100, 0.1, 0));
this.vel.add(this.push);
}
if(this.cpos.y > height -100){
this.wall = createVector(this.cpos.x, height);
this.d = dist(this.cpos.x, this.cpos.y, this.wall.x, this.wall.y);
this.push = p5.Vector.sub(this.cpos,this.wall);
this.push.setMag(map(this.d, 0, 100, 0.1, 0));
this.vel.add(this.push);
}
this.vel.limit(2);
this.cpos.add(this.vel);
stroke(map(this.vel.mag(), 0, 2, this.r1, this.r2), map(this.vel.mag(), 0, 2, this.g1, this.g2), map(this.vel.mag(), 0, 2, this.b1, this.b2))
strokeWeight(map(this.vel.mag(), 0, 2, 5, 2));
line(this.cpos.x, this.cpos.y, this.cpos.x + 5, this.cpos.y + 5);
if(this.cpos.x < 0) this.cpos.x = width;
if(this.cpos.y < 0) this.cpos.y = height;
if(this.cpos.x > width) this.cpos.x = 0;
if(this.cpos.y > height) this.cpos.y = 0;
}
}