xxxxxxxxxx
59
class Mover {
constructor(x, y, m) {
this.pos = createVector(x, y);
this.vel = createVector(random(-1,1),random(-1,1));
this.acc = createVector(0, 0);
this.mass = m;
this.r = sqrt(this.mass)*3;
//Variables for angular motion
// this.angle = 0;
// this.angleV = 0;
// this.angleA = 0.01;
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.acc.add(f);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
// this.angleA = this.acc.x/10; //Using the x component of the object's linear acceleration to calculate angular acceleration
// this.angleV +=(this.angleA);
// this.angleV = constrain(this.angleV,-0.1,0.1);
// this.angle +=(this.angleV);
this.acc.set(0, 0); //To reset acceleration zero at the end of each frame
}
show() {
push(); // saves the current state so the rotation of this shape doesn’t affect the rest of the world
stroke(255);
strokeWeight(2);
fill(255, 100);
translate (this.pos.x,this.pos.y); //center of rotation is object's center
// rotate(this.angle);
let angle = this.vel.heading();
// heading(this.angle);
triangle(-this.r,(this.r)/2,-this.r, (this.r)/2,this.r,0);
line(0,0,this.r,0);
pop(0); //pop() restores the previous state after rotation is complete
}
attract(mover) {
let force = p5.Vector.sub(this.pos, mover.pos);
let distanceSq = constrain(force.magSq(), 100, 1000);
let G = 1;
let strength = G * (this.mass * mover.mass) / distanceSq;
force.setMag(strength);
mover.applyForce(force);
}
}