2016-09-03 18:27:12 -04:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
var MatterAttractors = {
|
|
|
|
name: 'matter-attractors',
|
|
|
|
|
|
|
|
version: '0.1.0',
|
|
|
|
|
|
|
|
for: 'matter-js@^0.10.0',
|
|
|
|
|
|
|
|
install: function(base) {
|
2016-11-02 20:49:22 -04:00
|
|
|
base.after('Body.create', function() {
|
|
|
|
MatterAttractors.Body.init(this);
|
|
|
|
});
|
2016-09-03 18:27:12 -04:00
|
|
|
|
2016-11-02 20:49:22 -04:00
|
|
|
base.after('Engine.update', function() {
|
|
|
|
MatterAttractors.Engine.update(this);
|
|
|
|
});
|
2016-09-03 18:27:12 -04:00
|
|
|
},
|
|
|
|
|
2016-10-15 18:23:35 -04:00
|
|
|
Body: {
|
|
|
|
init: function(body) {
|
|
|
|
body.attractors = body.attractors || [];
|
|
|
|
}
|
2016-09-03 18:27:12 -04:00
|
|
|
},
|
|
|
|
|
2016-10-15 18:23:35 -04:00
|
|
|
Engine: {
|
|
|
|
update: function(engine) {
|
|
|
|
var world = engine.world,
|
2016-11-02 20:49:22 -04:00
|
|
|
bodies = Matter.Composite.allBodies(world);
|
2016-10-15 18:23:35 -04:00
|
|
|
|
|
|
|
for (var i = 0; i < bodies.length; i += 1) {
|
|
|
|
var bodyA = bodies[i],
|
|
|
|
attractors = bodyA.attractors;
|
|
|
|
|
|
|
|
if (attractors && attractors.length > 0) {
|
|
|
|
for (var j = i + 1; j < bodies.length; j += 1) {
|
|
|
|
var bodyB = bodies[j];
|
|
|
|
|
|
|
|
for (var k = 0; k < attractors.length; k += 1) {
|
|
|
|
var attractor = attractors[k],
|
|
|
|
forceVector = attractor;
|
|
|
|
|
2016-11-02 20:49:22 -04:00
|
|
|
if (Matter.Common.isFunction(attractor)) {
|
2016-10-15 18:23:35 -04:00
|
|
|
forceVector = attractor(bodyA, bodyB);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceVector) {
|
2016-11-02 20:49:22 -04:00
|
|
|
Matter.Body.applyForce(bodyB, bodyB.position, forceVector);
|
2016-10-15 18:23:35 -04:00
|
|
|
}
|
2016-09-03 18:27:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Matter.Plugin.register(MatterAttractors);
|
|
|
|
|
2016-11-03 20:04:56 -04:00
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window.MatterAttractors = MatterAttractors;
|
|
|
|
}
|
2016-09-03 18:27:12 -04:00
|
|
|
|
|
|
|
})();
|