Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnetic attraction #13

Open
rvanderspuy opened this issue Sep 27, 2019 · 0 comments
Open

Magnetic attraction #13

rvanderspuy opened this issue Sep 27, 2019 · 0 comments

Comments

@rvanderspuy
Copy link

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! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant