From c9294ebe6e32b9625d6fb421eff77eae8aaa5d98 Mon Sep 17 00:00:00 2001 From: liabru Date: Thu, 6 Jul 2017 21:50:08 +0100 Subject: [PATCH] upgraded matter-wrap --- demo/lib/matter-wrap.js | 132 +++++++++++++++++++++++++++++++--------- 1 file changed, 104 insertions(+), 28 deletions(-) diff --git a/demo/lib/matter-wrap.js b/demo/lib/matter-wrap.js index 5d16c499..933b8bf3 100644 --- a/demo/lib/matter-wrap.js +++ b/demo/lib/matter-wrap.js @@ -1,15 +1,15 @@ /*! - * matter-wrap 0.1.2 by Liam Brummitt 2017-02-12 + * matter-wrap 0.2.0 by Liam Brummitt 2017-07-04 * https://github.com/liabru/matter-wrap * License MIT */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') - module.exports = factory(require("Matter")); + module.exports = factory(require("matter-js")); else if(typeof define === 'function' && define.amd) - define(["Matter"], factory); + define(["matter-js"], factory); else if(typeof exports === 'object') - exports["MatterWrap"] = factory(require("Matter")); + exports["MatterWrap"] = factory(require("matter-js")); else root["MatterWrap"] = factory(root["Matter"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_0__) { @@ -104,7 +104,7 @@ var Matter = __webpack_require__(0); var MatterWrap = { // plugin meta name: 'matter-wrap', // PLUGIN_NAME - version: '0.1.0', // PLUGIN_VERSION + version: '0.1.3', // PLUGIN_VERSION for: 'matter-js@^0.12.0', // installs the plugin where `base` is `Matter` @@ -117,7 +117,7 @@ var MatterWrap = { Engine: { /** - * Updates the engine by wrapping bodies inside `engine.world`. + * Updates the engine by wrapping bodies and composites inside `engine.world`. * This is called automatically by the plugin. * @function MatterWrap.Engine.update * @param {Matter.Engine} engine The engine to update. @@ -125,7 +125,8 @@ var MatterWrap = { */ update: function update(engine) { var world = engine.world, - bodies = Matter.Composite.allBodies(world); + bodies = Matter.Composite.allBodies(world), + composites = Matter.Composite.allComposites(world); for (var i = 0; i < bodies.length; i += 1) { var body = bodies[i]; @@ -134,46 +135,114 @@ var MatterWrap = { MatterWrap.Body.wrap(body, body.plugin.wrap); } } + + for (i = 0; i < composites.length; i += 1) { + var composite = composites[i]; + + if (composite.plugin.wrap) { + MatterWrap.Composite.wrap(composite, composite.plugin.wrap); + } + } } }, - Body: { + Bounds: { /** - * Wraps the `body` position such that it always stay within the given bounds. - * Upon crossing a boundary the body will appear on the opposite side of the bounds, - * while maintaining its velocity. - * This is called automatically by the plugin. - * @function MatterAttractors.Body.wrap - * @param {Matter.Body} body The body to wrap. + * Returns a translation vector that wraps the `objectBounds` inside the `bounds`. + * @function MatterWrap.Bounds.wrap + * @param {Matter.Bounds} objectBounds The bounds of the object to wrap inside the bounds. * @param {Matter.Bounds} bounds The bounds to wrap the body inside. - * @returns {void} No return value. + * @returns {?Matter.Vector} A translation vector (only if wrapping is required). */ - wrap: function wrap(body, bounds) { + wrap: function wrap(objectBounds, 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 (objectBounds.min.x > bounds.max.x) { + x = bounds.min.x - objectBounds.max.x; + } else if (objectBounds.max.x < bounds.min.x) { + x = bounds.max.x - objectBounds.min.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 (objectBounds.min.y > bounds.max.y) { + y = bounds.min.y - objectBounds.max.y; + } else if (objectBounds.max.y < bounds.min.y) { + y = bounds.max.y - objectBounds.min.y; } } if (x !== null || y !== null) { - Matter.Body.setPosition(body, { - x: x || body.position.x, - y: y || body.position.y - }); + return { + x: x || 0, + y: y || 0 + }; + } + } + }, + + Body: { + /** + * Wraps the `body` position such that it always stays within the given bounds. + * Upon crossing a boundary the body will appear on the opposite side of the bounds, + * while maintaining its velocity. + * This is called automatically by the plugin. + * @function MatterWrap.Body.wrap + * @param {Matter.Body} body The body to wrap. + * @param {Matter.Bounds} bounds The bounds to wrap the body inside. + * @returns {?Matter.Vector} The translation vector that was applied (only if wrapping was required). + */ + wrap: function wrap(body, bounds) { + var translation = MatterWrap.Bounds.wrap(body.bounds, bounds); + + if (translation) { + Matter.Body.translate(body, translation); + } + + return translation; + } + }, + + Composite: { + /** + * Returns the union of the bounds of all of the composite's bodies + * (not accounting for constraints). + * @function MatterWrap.Composite.bounds + * @param {Matter.Composite} composite The composite. + * @returns {Matter.Bounds} The composite bounds. + */ + bounds: function bounds(composite) { + var bodies = Matter.Composite.allBodies(composite), + vertices = []; + + for (var i = 0; i < bodies.length; i += 1) { + var body = bodies[i]; + vertices.push(body.bounds.min, body.bounds.max); + } + + return Matter.Bounds.create(vertices); + }, + + /** + * Wraps the `composite` position such that it always stays within the given bounds. + * Upon crossing a boundary the composite will appear on the opposite side of the bounds, + * while maintaining its velocity. + * This is called automatically by the plugin. + * @function MatterWrap.Composite.wrap + * @param {Matter.Composite} composite The composite to wrap. + * @param {Matter.Bounds} bounds The bounds to wrap the composite inside. + * @returns {?Matter.Vector} The translation vector that was applied (only if wrapping was required). + */ + wrap: function wrap(composite, bounds) { + var translation = MatterWrap.Bounds.wrap(MatterWrap.Composite.bounds(composite), bounds); + + if (translation) { + Matter.Composite.translate(composite, translation); } + + return translation; } } }; @@ -194,6 +263,13 @@ module.exports = MatterWrap; * @memberof Matter.Body */ +/** + * This plugin adds a new property `composite.plugin.wrap` to instances of `Matter.Composite`. + * This is a `Matter.Bounds` instance that specifies the wrapping region. + * @property {Matter.Bounds} composite.plugin.wrap + * @memberof Matter.Composite + */ + /***/ }) /******/ ]); }); \ No newline at end of file