liabru / matter-attractors

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

Attractor only works when added before other bodies #6

Open lonekorean opened 6 years ago

lonekorean commented 6 years ago

It seems that an attracting body will not attract other bodies added to the world before it.

Demo here: https://codepen.io/lonekorean/pen/cfc51f387d8f32727288f3a59113ceeb

Comment/uncomment line 42/43 to see.

jpagand commented 6 years ago

This PR will fix this issue: https://github.com/liabru/matter-attractors/pull/5

probityrules commented 6 years ago

I'm running into this issue as well. I realize that the current version is an optimization so that the number of checks doesn't increase quite as quickly as new bodies are added, but it only works under the assumption that all affected bodies for a given attractor all include the self-same attractor. However, once there are bodies that don't include the attractor, but should be affected by another body having said attractor, it seems that either a full loop like @jpagand is suggesting be used, or, maybe slightly more efficient, maintaining a smaller list of attractor-including bodies to be checked against all other bodies.

For example, if I create a body with a "magnet" attractor, requiring all bodies that are affected by this attractor also have a "magnet" attractor probably makes sense. However. if I create a body with a "fan" attractor, having every body that should be blown by a fan also include a "fan" attractor seems to make a little less sense.

Another possibility may be to go through the bodies as currently set up, but rather than reference the attractor functions on each body, let them reside elsewhere and iterate through all of the attractor functions for all bodies (so that their absence on a given body doesn't preclude them from being affected by a body later in the list). Not sure if much is gained going this route; just thinking it through.

whxru commented 5 years ago

I met the same problem too and it's easy to fix the bug, but it seems that the author will no longer update the plugin so I fix the bug and publish to npm, you can get detail in my repo.

luigimannoni commented 5 years ago

Package from @whxru fixed my issue and works with the latest matterjs installed (0.14.2)