Coder Social home page Coder Social logo

Repulsion Example about matter-attractors HOT 4 CLOSED

liabru avatar liabru commented on June 8, 2024
Repulsion Example

from matter-attractors.

Comments (4)

probityrules avatar probityrules commented on June 8, 2024

Your math is defining force such that greater distance equals more force: if you have both bodies at y = 0; and one's x at 0 and the other's at 1, the force is 0.00001. However, if you have the latter's at x = 1000;, the force is 0.01.

Relatedly, you will get more force for the same amount of distance diagonally than you will along the x or y axis, since you're treating both axis apart from each other.

I might recommend having something like 1 / distance to make attraction/repulsion exponentially stronger the closer the bodies are, and define distance as something like Math.sqrt(deltaX * deltaX + deltaY * deltaY);

from matter-attractors.

adamcoulombe avatar adamcoulombe commented on June 8, 2024

Thanks.... I tried my best to interpret your recommendations, and it seems a little better. but I think i may still have something wrong...

  plugin: {
    attractors: [
      function(bodyA, bodyB) {
        var deltaX = bodyA.position.x - bodyB.position.x;
        var deltaY = bodyA.position.y - bodyB.position.y;
        var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
        return {
          x: 1/distance,
          y: 1/distance,
        };
      }
    ]
  }

https://codepen.io/adamcoulombe/pen/qBOaxem?editors=0010

I appreciate your help :)

from matter-attractors.

probityrules avatar probityrules commented on June 8, 2024

Yeah, you need to maintain the original vector's direction for the above to work. Probably something like the following to make a unit vector and then apply the correct amount of force along each axis:

    var deltaX = bodyA.position.x - bodyB.position.x;
    var deltaY = bodyA.position.y - bodyB.position.y;
    var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    var force = 1 / distance;
    var unitX = deltaX / distance;
    var unitY = deltaY / distance;
    return {
        x: -unitX * force,
        y: -unitY * force
    };

from matter-attractors.

adamcoulombe avatar adamcoulombe commented on June 8, 2024

Ah!! there we go! That's exactly what i was looking to do.

I will say it again, I really appreciate the help.

The math is a little beyond me but this lib appears to do what I was in search of.

Thank you!!

from matter-attractors.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.