2016-09-03 18:27:12 -04:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
var MatterGravity = {
|
|
|
|
name: 'matter-gravity',
|
|
|
|
|
|
|
|
version: '0.1.0',
|
|
|
|
|
|
|
|
for: 'matter-js@^0.10.0',
|
|
|
|
|
|
|
|
uses: [
|
|
|
|
'matter-attractors@^0.1.0'
|
|
|
|
],
|
|
|
|
|
|
|
|
install: function(base) {
|
2016-11-02 20:49:22 -04:00
|
|
|
base.after('Body.create', function() {
|
|
|
|
MatterGravity.Body.init(this);
|
|
|
|
});
|
2016-09-03 18:27:12 -04:00
|
|
|
},
|
|
|
|
|
2016-10-15 18:23:35 -04:00
|
|
|
Body: {
|
|
|
|
init: function(body) {
|
|
|
|
if (body.gravity) {
|
|
|
|
body.attractors.push(MatterGravity.Body.applyGravity);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applyGravity: function(bodyA, bodyB) {
|
2017-01-19 18:36:34 -05:00
|
|
|
var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
|
|
|
|
distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
|
|
|
|
normal = Matter.Vector.normalise(bToA),
|
2016-10-15 18:23:35 -04:00
|
|
|
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
|
2017-01-19 18:36:34 -05:00
|
|
|
force = Matter.Vector.mult(normal, magnitude);
|
2016-10-15 18:23:35 -04:00
|
|
|
|
2017-01-19 18:36:34 -05:00
|
|
|
Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
|
|
|
|
Matter.Body.applyForce(bodyB, bodyB.position, force);
|
2016-09-03 18:27:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Matter.Plugin.register(MatterGravity);
|
|
|
|
|
2016-11-03 20:04:56 -04:00
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window.MatterGravity = MatterGravity;
|
|
|
|
}
|
2016-09-03 18:27:12 -04:00
|
|
|
|
|
|
|
})();
|