Skip to content

Commit

Permalink
refactored plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Oct 15, 2016
1 parent 4b5837e commit b4a3453
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 112 deletions.
14 changes: 6 additions & 8 deletions examples/attractors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@

Matter.use(
'matter-gravity',
'matter-world-wrap'
'matter-wrap'
);

world.bounds = {
min: { x: 0, y: 0 },
max: { x: 800, y: 600 }
};

world.bodies = [];
world.gravity.scale = 0;

Expand All @@ -32,7 +27,11 @@
{
mass: Common.random(10, 20),
gravity: G,
frictionAir: 0
frictionAir: 0,
wrap: {
min: { x: 0, y: 0 },
max: { x: 800, y: 600 }
}
}
);

Expand All @@ -46,7 +45,6 @@

var renderOptions = demo.render.options;
renderOptions.showAngleIndicator = false;
renderOptions.showPositions = true;
};

})();
63 changes: 34 additions & 29 deletions examples/attractorsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,49 @@
install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterAttractors.init
function() {
MatterAttractors.Body.init(this);
}
);

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

init: function(body) {
body = this || body;
body.attractors = body.attractors || [];
Body: {
init: function(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);
Engine: {
update: function(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);
}
}
}
}
Expand Down
36 changes: 19 additions & 17 deletions examples/gravityPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,29 @@
install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterGravity.addAttractor
function() {
MatterGravity.Body.init(this);
}
);
},

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

if (body.gravity) {
body.attractors.push(MatterGravity.applyForce);
Body: {
init: function(body) {
if (body.gravity) {
body.attractors.push(MatterGravity.Body.applyGravity);
}
},

applyGravity: 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);
}
},

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);
}
};

Expand Down
58 changes: 0 additions & 58 deletions examples/worldWrapPlugin.js

This file was deleted.

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

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

var MatterWrap = {
name: 'matter-wrap',

version: '0.1.0',

for: 'matter-js@^0.10.0',

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

Engine: {
update: function(engine) {
var world = engine.world,
bodies = Composite.allBodies(world);

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

if (body.wrap) {
MatterWrap.Body.wrap(body, body.wrap);
}
}
}
},

Body: {
wrap: function(body, bounds) {
var x = null,
y = null;

if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
if (body.bounds.min.x > bounds.max.x) {
x = bounds.min.x - (body.bounds.max.x - body.position.x);
} else if (body.bounds.max.x < bounds.min.x) {
x = bounds.max.x - (body.bounds.min.x - body.position.x);
}
}

if (typeof bounds.min.y !== 'undefined' && typeof bounds.max.y !== 'undefined') {
if (body.bounds.min.y > bounds.max.y) {
y = bounds.min.y - (body.bounds.max.y - body.position.y);
} else if (body.bounds.max.y < bounds.min.y) {
y = 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(MatterWrap);

window.MatterWrap = MatterWrap;

})();

0 comments on commit b4a3453

Please sign in to comment.