xxxxxxxxxx
80
let mover;
let attractor;
function setup() {
createCanvas(400, 400);
mover = new Mover();
attractor = new Attractor();
}
function draw() {
background(220);
mover.render(attractor);
attractor.render();
}
class Mover{
constructor(){
this.pos = createVector(0,0);
this.acc = createVector(0,0);
this.vel = createVector(0,0);
this.size = 30;
this.Tdist = 100;
}
applyForce(force){
force.div(10);
this.acc.add(force);
}
update(){
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0,0);
this.vel.set(0,0);
// this.vel.limit(4);
}
attract(other){
let Cdist = p5.Vector.dist(this.pos,other.pos);
if (Cdist > this.Tdist+10){
let effect = p5.Vector.sub(other.pos,this.pos);
this.applyForce(effect);
} else if (Cdist < this.Tdist-10){
let effect = p5.Vector.sub(this.pos,other.pos);
this.applyForce(effect);
} else if (Cdist > this.Tdist-10 && Cdist < this.Tdist+10){
let effect = 0;
print("hi");
this.vel.set(0,0);
// this.applyForce(0);
}
}
render(other){
this.attract(other);
this.update();
ellipse(this.pos.x,this.pos.y,this.size);
}
}
class Attractor{
constructor(){
this.pos = createVector(0,0);
this.acc = createVector(0,0);
this.vel = createVector(0,0);
this.size = 30;
}
update(){
this.pos.set(mouseX,mouseY);
}
render(){
this.update();
ellipse(this.pos.x,this.pos.y,this.size);
}
}