From b4a34536595a6b4327e285752e87ee6e31b2def5 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 15 Oct 2016 23:23:35 +0100 Subject: [PATCH] refactored plugins --- examples/attractors.js | 14 +++---- examples/attractorsPlugin.js | 55 +++++++++++++++------------ examples/gravityPlugin.js | 34 +++++++++-------- examples/worldWrapPlugin.js | 58 ---------------------------- examples/wrapPlugin.js | 73 ++++++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 107 deletions(-) delete mode 100644 examples/worldWrapPlugin.js create mode 100644 examples/wrapPlugin.js diff --git a/examples/attractors.js b/examples/attractors.js index 3366317..25a4d54 100644 --- a/examples/attractors.js +++ b/examples/attractors.js @@ -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; @@ -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 } + } } ); @@ -46,7 +45,6 @@ var renderOptions = demo.render.options; renderOptions.showAngleIndicator = false; - renderOptions.showPositions = true; }; })(); \ No newline at end of file diff --git a/examples/attractorsPlugin.js b/examples/attractorsPlugin.js index 07ec567..01bcbcd 100644 --- a/examples/attractorsPlugin.js +++ b/examples/attractorsPlugin.js @@ -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; + Engine: { + update: function(engine) { + var world = engine.world, + bodies = Composite.allBodies(world); - var world = engine.world, - bodies = Composite.allBodies(world); + for (var i = 0; i < bodies.length; i += 1) { + var bodyA = bodies[i], + attractors = bodyA.attractors; - 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]; - 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; - 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); + if (Common.isFunction(attractor)) { + forceVector = attractor(bodyA, bodyB); + } + + if (forceVector) { + Body.applyForce(bodyB, bodyB.position, forceVector); + } } } } diff --git a/examples/gravityPlugin.js b/examples/gravityPlugin.js index 4f7afb4..5818e89 100644 --- a/examples/gravityPlugin.js +++ b/examples/gravityPlugin.js @@ -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; + Body: { + init: function(body) { + if (body.gravity) { + body.attractors.push(MatterGravity.Body.applyGravity); + } + }, - if (body.gravity) { - body.attractors.push(MatterGravity.applyForce); + 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); } }; diff --git a/examples/worldWrapPlugin.js b/examples/worldWrapPlugin.js deleted file mode 100644 index 4953d55..0000000 --- a/examples/worldWrapPlugin.js +++ /dev/null @@ -1,58 +0,0 @@ -(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; - -})(); diff --git a/examples/wrapPlugin.js b/examples/wrapPlugin.js new file mode 100644 index 0000000..f9a25fb --- /dev/null +++ b/examples/wrapPlugin.js @@ -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; + +})();