Skip to content

Attraction

The attractionForce attracts (or repels) a thing based on a list of attractors.

For example, lets say we have earth and sun

const sun = { mass: 1000, position: Math.random() };
const earth = { mass: 0.01, position: Math.random() };

The sun should be the thing that attracts the earth, so it is setup in the force, along with a gravity value of 0.1:

// Init the sun's attractive force
const sunAttractionForce = Forces.attractionForce(sun, 0.1);
// Apply the force to earth
earth = Forces.apply(earth, sunAttractionForce);

The force can take a third parameter for the range at which the force is applied:

// Only apply force when distance between sun and thing is between 1-10%
const sunAttractionForce = Forces.attractionForce(sun, 0.1, { max: 0.1, min: 0.01});