xxxxxxxxxx
48
class Mover {
constructor(x,y) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.vel.mult(random(3));
}
update() {
let mouse = createVector(mouseX, mouseY);
this.acc = p5.Vector.sub(mouse, this.pos); // calculate the acc
let calc = createVector(10, 10);
// set mag based on how close object is to mouse
let mag = 0.02;
if (this.acc < calc){
mag = mag + 1;
} else {
mag = mag - 0.01;
}
this.acc.setMag(mag);
console.log(mag);
this.vel.add(this.acc); // apply acc to vel
this.vel.limit(2);
this.pos.add(this.vel); // apply vel to pos
}
show() {
ellipse(this.pos.x, this.pos.y, 16, 16);
}
checkEdges(){
if (this.pos.x > width+8) {
this.pos.x = 0;
} else if (this.pos.x < 0-8) {
this.pos.x = width;
}
if (this.pos.y > height+8) {
this.pos.y = 0;
} else if (this.pos.y < 0-8) {
this.pos.y = height;
}
}
}