For this week assignment in forces, I wanted to do examine the mutual attraction between objects.

https://editor.p5js.org/wallflower/full/ClBM4d989

For mutual attraction between forces we use this formula:

Fg = G * (m1*m2/r2)v

I wanted to explore this concept because it seemed more intriguing to explore the forces not just between two entities but between n body objects!

My key takeaway from the methodology is every object has a force applying to its surroundings and this is correlated with the objects mass. Objects with higher masses have higher force than the objects have smaller masses. In the p5.js environment, I used the circles diameter to assign mass hypothetically since its a digital environment.

class Mover {
  constructor(x,y, vx, vy, m){
    this.pos = createVector(x,y);
    this.vel = createVector(vx,vy);
    this.acc = createVector(0,0);
    this.mass = m; //mass of my ellipse
    this.r = sqrt(this.mass)*2; //this is coming from the area calculation of an ellipse pi r2
  }
  

In my class, first, I created a constructor taking the values for pos, vel positions and a mass value. this.r calculation makes the ellipse proportional to its mass and its also coming from the its area calcuation pi rsquare

 applyForce(force) {
    let f = p5.Vector.div(force, this.mass);
    this.acc.add(f);
  }

this function calculates the acceleration, accelereration equals to = force/mass, so when user inputs a value depending on the mass of the object acc will be differerent! Later I add the f value to the acc.

  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);
    
  }

the force method gives me the direction for the object to move in which direction, the mover is passed from the object i created to keep the objects close to the center.

distance gives me the magnitude of the vector and I constrain it to avoid objects being too close to each other or get 0 or very small values to avoid infinite force

G is the const gravitational force