liabru / matter-attractors

an attractors plugin for matter.js
MIT License
143 stars 31 forks source link

isStatic causes Infinite Body values #4

Open Qriva opened 6 years ago

Qriva commented 6 years ago

Because static bodies have infinite mass and other propeties, after setting at least 1 body to static this results with completely bugged stage (bodies get infinite prop.)

Actually there should be possibility to add gravity force for static bodies - is it required for body to has infinity? Anyway I think quick fix like this would be nice anyway because everything just stops working at the moment.

gravity: function(bodyA, bodyB) {
    // apply force only if both bodies are not static
    if(!bodyA.isStatic && !bodyB.isStatic){
      // use Newton's law of gravitation
      var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
        distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
        normal = Matter.Vector.normalise(bToA),
        magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
        force = Matter.Vector.mult(normal, magnitude);

      // to apply forces to both bodies
      Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
      Matter.Body.applyForce(bodyB, bodyB.position, force);
    }
}
tandryukha commented 5 years ago

I faced same bug. It would be great to have it fixed.