Skip to content

Magnetic attraction #13

@rvanderspuy

Description

@rvanderspuy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions