diff --git a/examples/attractorsPlugin.js b/examples/attractorsPlugin.js new file mode 100644 index 00000000..07ec5678 --- /dev/null +++ b/examples/attractorsPlugin.js @@ -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; + +})(); diff --git a/examples/gravityPlugin.js b/examples/gravityPlugin.js new file mode 100644 index 00000000..4f7afb49 --- /dev/null +++ b/examples/gravityPlugin.js @@ -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; + +})(); diff --git a/examples/plugin.js b/examples/plugin.js deleted file mode 100644 index d3657118..00000000 --- a/examples/plugin.js +++ /dev/null @@ -1,48 +0,0 @@ -(function() { - - var Common = Matter.Common; - - var MatterPlugin = { - name: 'matter-plugin', - - version: '0.2.0', - - for: 'matter-js@^0.10.0', - - uses: [ - 'matter-plugin-2@^0.1.1', - 'matter-plugin-3@^0.10.0' - ], - - options: { - thing: 1 - }, - - install: function(base) { - base.Engine.create = Common.chain( - Matter.Engine.create, - MatterPlugin._engineCreate - ); - - base.Body.create = Common.chain( - MatterPlugin._bodyCreate, - Matter.Body.create - ); - }, - - _engineCreate: function(element, options) { - var engine = this; - - console.log('2nd patched engine create!', engine); - }, - - _bodyCreate: function(options) { - console.log('patched body create!', options); - } - }; - - Matter.Plugin.register(MatterPlugin); - - window.MatterPlugin = MatterPlugin; - -})(); diff --git a/examples/plugin2.js b/examples/plugin2.js deleted file mode 100644 index e773d59d..00000000 --- a/examples/plugin2.js +++ /dev/null @@ -1,36 +0,0 @@ -(function() { - - var Common = Matter.Common; - - var MatterPlugin2 = { - name: 'matter-plugin-2', - - version: '0.1.0', - - for: 'matter-js@^0.10.0', - - uses: ['matter-plugin-fake'], - - options: { - thing: 1 - }, - - install: function(matter) { - matter.Engine.create = Common.chain( - matter.Engine.create, - MatterPlugin2._engineCreate - ); - }, - - _engineCreate: function(element, options) { - var engine = this; - - console.log('patched engine create!', engine); - } - }; - - Matter.Plugin.register(MatterPlugin2); - - window.MatterPlugin2 = MatterPlugin2; - -})(); diff --git a/examples/worldWrapPlugin.js b/examples/worldWrapPlugin.js new file mode 100644 index 00000000..4953d553 --- /dev/null +++ b/examples/worldWrapPlugin.js @@ -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; + +})();