matter-attractors icon indicating copy to clipboard operation
matter-attractors copied to clipboard

Magnetic attraction

Open rvanderspuy opened this issue 5 years ago • 0 comments

Hello!

I'm attempting to achieve and effect where the attraction between bodies varies depending on their distance and mass (like a magnet). I've attempted to use the following function, using the standard vector math formula, but so far have not been able to replicate the effect:

function(bodyA, bodyB) {
                let vx = bodyB.position.x - bodyA.position.x;
                let vy = bodyB.position.y - bodyA.position.y;
                let m = Math.sqrt(vx * vx + vy * vy);
                let dx = vx / m;
                let dy = vy / m;

                return {
                  x: (dx * (bodyA.mass * bodyB.mass)) / m,
                  y: (dy * (bodyA.mass * bodyB.mass)) / m
                };
}

Probably I don't understand properly which x/y values should be returned. Any assistance would be greatly appreciated! 😄

rvanderspuy avatar Sep 27 '19 09:09 rvanderspuy