xxxxxxxxxx
37
// Gravitational Attraction
// The Nature of Code
// The Coding Train / Daniel Shiffman
// https://youtu.be/EpgB3cNhKPM
// https://thecodingtrain.com/learning/nature-of-code/2.5-gravitational-attraction.html
// https://editor.p5js.org/codingtrain/sketches/MkLraatd
class Attractor {
constructor(x,y,m,p) {
this.pos = createVector(x,y);
this.mass = m;
this.r = sqrt(this.mass)*2;
this.p = p;
}
attract(mover) {
let force = p5.Vector.sub(this.pos, mover.pos);
let distanceSq = constrain(force.magSq(), 100, 1000);
let G = 5;
let strength = G * (this.mass * mover.mass) / distanceSq;
force.setMag(strength * this.p);
mover.applyForce(force);
}
show() {
noStroke();
if (this.p === 1) {
fill(255,0, 100);
}
if (this.p === -1) {
fill(0, 255, 100);
}
ellipse(this.pos.x, this.pos.y, this.r*2);
}
}