Skip to content

Commit

Permalink
added plugins matter-attractors, matter-gravity, matter-world-wrap, r…
Browse files Browse the repository at this point in the history
…emoved test plugins
  • Loading branch information
liabru committed Sep 3, 2016
1 parent 83ae546 commit 59bfa0b
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 84 deletions.
67 changes: 67 additions & 0 deletions examples/attractorsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(function() {

var Body = Matter.Body,
Common = Matter.Common,
Composite = Matter.Composite;

var MatterAttractors = {
name: 'matter-attractors',

version: '0.1.0',

for: 'matter-js@^0.10.0',

install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterAttractors.init
);

base.Engine.update = Common.chain(
Matter.Engine.update,
MatterAttractors.update
);
},

init: function(body) {
body = this || body;
body.attractors = body.attractors || [];
},

update: function(engine) {
engine = this || engine;

var world = engine.world,
bodies = Composite.allBodies(world);

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;

if (Common.isFunction(attractor)) {
forceVector = attractor(bodyA, bodyB);
}

if (forceVector) {
Body.applyForce(bodyB, bodyB.position, forceVector);
}
}
}
}
}
}
};

Matter.Plugin.register(MatterAttractors);

window.MatterAttractors = MatterAttractors;

})();
49 changes: 49 additions & 0 deletions examples/gravityPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function() {

var Body = Matter.Body,
Common = Matter.Common,
Vector = Matter.Vector;

var MatterGravity = {
name: 'matter-gravity',

version: '0.1.0',

for: 'matter-js@^0.10.0',

uses: [
'matter-attractors@^0.1.0'
],

install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterGravity.addAttractor
);
},

addAttractor: function(body) {
body = this || body;

if (body.gravity) {
body.attractors.push(MatterGravity.applyForce);
}
},

applyForce: function(bodyA, bodyB) {
var bToA = Vector.sub(bodyB.position, bodyA.position),
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
normal = Vector.normalise(bToA),
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
force = Vector.mult(normal, magnitude);

Body.applyForce(bodyA, bodyA.position, Vector.neg(force));
Body.applyForce(bodyB, bodyB.position, force);
}
};

Matter.Plugin.register(MatterGravity);

window.MatterGravity = MatterGravity;

})();
48 changes: 0 additions & 48 deletions examples/plugin.js

This file was deleted.

36 changes: 0 additions & 36 deletions examples/plugin2.js

This file was deleted.

58 changes: 58 additions & 0 deletions examples/worldWrapPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(function() {

var Body = Matter.Body,
Common = Matter.Common,
Composite = Matter.Composite;

var MatterWorldWrap = {
name: 'matter-world-wrap',

version: '0.1.0',

for: 'matter-js@^0.10.0',

install: function(base) {
base.Engine.update = Common.chain(
Matter.Engine.update,
MatterWorldWrap.update
);
},

update: function(engine) {
engine = this || engine;

var world = engine.world,
bodies = Composite.allBodies(world);

for (var i = 0; i < bodies.length; i += 1) {
var body = bodies[i],
x = null,
y = null;

if (body.bounds.min.x > world.bounds.max.x) {
x = world.bounds.min.x - (body.bounds.max.x - body.position.x);
} else if (body.bounds.max.x < world.bounds.min.x) {
x = world.bounds.max.x - (body.bounds.min.x - body.position.x);
}

if (body.bounds.min.y > world.bounds.max.y) {
y = world.bounds.min.y - (body.bounds.max.y - body.position.y);
} else if (body.bounds.max.y < world.bounds.min.y) {
y = world.bounds.max.y - (body.bounds.min.y - body.position.y);
}

if (x !== null || y !== null) {
Body.setPosition(body, {
x: x || body.position.x,
y: y || body.position.y
});
}
}
}
};

Matter.Plugin.register(MatterWorldWrap);

window.MatterWorldWrap = MatterWorldWrap;

})();

0 comments on commit 59bfa0b

Please sign in to comment.