xxxxxxxxxx
52
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Mover {
constructor(x,y) {
this.position = createVector(x,y);
this.velocity = createVector();
this.acceleration = createVector();
this.topspeed = 5;
this.history = [];
}
update() {
//var xy = xys[zz % 6];
console.log(11);
console.log(xy);
// Compute a vector that points from position to mouse
var mouse = createVector(xy[0], xy[1]);
this.acceleration = p5.Vector.sub(mouse, this.position);
// Set magnitude of acceleration
this.acceleration.setMag(0.2);
this.velocity.add(this.acceleration);
this.velocity.limit(this.topspeed);
this.position.add(this.velocity);
let v = createVector(this.position.x, this.position.y);
this.history.push(v);
if (this.history.length > 500) {
this.history.splice(0, 1);
}
}
display() {
beginShape();
for (let i = 0; i < this.history.length; i++) {
let pos = this.history[i];
stroke(random(255),random(255),random(255));
point(pos.x, pos.y);
endShape();
}
noStroke();
strokeWeight(2);
fill(35, 103, 246);
ellipse(this.position.x, this.position.y, 10, 10);
}
}