Function computeAttractionForce

Computes the attraction force between two things. Value for gravity will depend on what range is used for mass. It's probably a good idea to keep mass to mean something relative - ie 1 is 'full' mass, and adjust the gravity value until it behaves as you like. Keeping mass in 0..1 range makes it easier to apply to visual properties later.

const attractor = { position: { x:0.5, y:0.5 }, mass: 1 };
const attractee = { position: Points.random(), mass: 0.01 };
attractee = Forces.apply(attractee, Forces.computeAttractionForce(attractor, attractee, 0.005));
attractor =  { position: { x:0.5, y:0.5 }, mass: 1 };
attractees = attractees.map(a => Forces.apply(a, Forces.computeAttractionForce(attractor, a, 0.005)));
// Create a force with all things as attractors.
const f = Forces.attractionForce(things, gravity);
// Apply force to all things.
// The function returned by attractionForce will automatically ignore self-attraction
things = things.map(a => Forces.apply(a, f));

attractor thing attracting (eg, earth) attractee thing being attracted (eg. satellite)

gravity will have to be tweaked to taste. distanceRange clamps the computed distance. This affects how tightly the particles will orbit and can also determine speed. By default it is 0.001-0.7

  • Parameters

    • attractor: ForceAffected

      Attractor (eg earth)

    • attractee: ForceAffected

      Attractee (eg satellite)

    • gravity: number

      Gravity constant

    • distanceRange: {
          max?: number;
          min?: number;
      } = {}

      Min/max that distance is clamped to.

      • Optional Readonlymax?: number
      • Optional Readonlymin?: number

    Returns Point