matter-attractors
matter-attractors copied to clipboard
Magnetic attraction
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! 😄