xxxxxxxxxx
103
let i = 0;
let close = 5;
let far = 10;
let nzoom = 100;
function setup() {
createCanvas(400, 400);
center = createVector(width / 2, height / 2);
dot = new Dot(width / 2 - 4, height / 2);
background(220);
strokeWeight(1);
}
function draw() {
dot.update();
}
class Dot{
constructor(x, y){
this.pos = createVector(x, y);
this.vel = createVector(1, 0);
this.dist = p5.Vector.sub(this.pos, center);
this.prox = createVector(0, 0);
this.closest = createVector(0, 0);
this.pa = 0;
this.rev = 0;
this.history = [];
this.history[0] = [];
}
update(){
if(this.history.length > 3){
this.closest = createVector(100, 0);
for(i = 0; i < this.history[this.history.length - 2].length; i++){
if(this.history[this.history.length - 2][i] !== undefined){
this.prox = p5.Vector.sub(this.pos, this.history[this.history.length - 2][i]);
if(this.prox.mag() < this.closest.mag()){
this.closest = this.prox;
}
if(this.prox.mag() < close){
this.pos.add(this.prox);
// if(this.prox.mag() < close - 1){
// this.prox.setMag(1);
// this.pos.add(this.prox);
// this.prox = p5.Vector.sub(this.pos, this.history[this.history.length - 3][i]);
// }
}
}
}
} else {
this.dist = p5.Vector.sub(this.pos, center);
this.vel.setHeading(this.dist.heading() + HALF_PI);
}
if(this.history.length > 2){
if(this.closest.mag() < close){
// this.vel.setHeading(this.closest.heading() + HALF_PI - 0.1);
this.closest.setMag(1);
this.pos.add(this.closest);
this.vel.setHeading(this.closest.heading() + HALF_PI);
// this.vel.setHeading(this.dist.heading() + HALF_PI);
} else if(this.closest.mag() > far){
// this.vel.setHeading(this.closest.heading() + HALF_PI + 0.1);
this.closest.setMag(1);
// this.pos.sub(this.closest);
this.vel.setHeading(this.closest.heading() + HALF_PI);
// this.vel.setHeading(this.dist.heading() + HALF_PI);
} else if(this.closest.mag() < far + map(noise(this.pos.x / nzoom, this.pos.y / nzoom), 0, 1, 0, 2) && this.closest.mag() > close + map(noise(this.pos.x / nzoom, this.pos.y / nzoom), 0, 1, 0, 2)){
this.vel.setHeading(this.closest.heading() + HALF_PI);
}
}
this.vel.limit(1);
this.dist = p5.Vector.sub(this.pos, center);
if(this.pa > 3 && this.dist.heading() < 1){
this.history[this.history.length] = [];
this.rev++;
if(this.history.length > 2){
this.history[this.history.length - 3] = undefined;
}
}
this.pos.add(this.vel);
point(this.pos.x, this.pos.y);
this.history[this.rev][this.history[this.rev].length] = createVector(this.pos.x, this.pos.y);
this.pa = this.dist.heading();
// print(this.history.length);
// print(this.history[this.history.length - 1].length)
}
}