mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-26 13:49:01 -05:00
upgraded matter-wrap
This commit is contained in:
parent
48611c51de
commit
c9294ebe6e
1 changed files with 105 additions and 29 deletions
|
@ -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
|
* https://github.com/liabru/matter-wrap
|
||||||
* License MIT
|
* License MIT
|
||||||
*/
|
*/
|
||||||
(function webpackUniversalModuleDefinition(root, factory) {
|
(function webpackUniversalModuleDefinition(root, factory) {
|
||||||
if(typeof exports === 'object' && typeof module === 'object')
|
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)
|
else if(typeof define === 'function' && define.amd)
|
||||||
define(["Matter"], factory);
|
define(["matter-js"], factory);
|
||||||
else if(typeof exports === 'object')
|
else if(typeof exports === 'object')
|
||||||
exports["MatterWrap"] = factory(require("Matter"));
|
exports["MatterWrap"] = factory(require("matter-js"));
|
||||||
else
|
else
|
||||||
root["MatterWrap"] = factory(root["Matter"]);
|
root["MatterWrap"] = factory(root["Matter"]);
|
||||||
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
|
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
|
||||||
|
@ -104,7 +104,7 @@ var Matter = __webpack_require__(0);
|
||||||
var MatterWrap = {
|
var MatterWrap = {
|
||||||
// plugin meta
|
// plugin meta
|
||||||
name: 'matter-wrap', // PLUGIN_NAME
|
name: 'matter-wrap', // PLUGIN_NAME
|
||||||
version: '0.1.0', // PLUGIN_VERSION
|
version: '0.1.3', // PLUGIN_VERSION
|
||||||
for: 'matter-js@^0.12.0',
|
for: 'matter-js@^0.12.0',
|
||||||
|
|
||||||
// installs the plugin where `base` is `Matter`
|
// installs the plugin where `base` is `Matter`
|
||||||
|
@ -117,7 +117,7 @@ var MatterWrap = {
|
||||||
|
|
||||||
Engine: {
|
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.
|
* This is called automatically by the plugin.
|
||||||
* @function MatterWrap.Engine.update
|
* @function MatterWrap.Engine.update
|
||||||
* @param {Matter.Engine} engine The engine to update.
|
* @param {Matter.Engine} engine The engine to update.
|
||||||
|
@ -125,7 +125,8 @@ var MatterWrap = {
|
||||||
*/
|
*/
|
||||||
update: function update(engine) {
|
update: function update(engine) {
|
||||||
var world = engine.world,
|
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) {
|
for (var i = 0; i < bodies.length; i += 1) {
|
||||||
var body = bodies[i];
|
var body = bodies[i];
|
||||||
|
@ -134,46 +135,114 @@ var MatterWrap = {
|
||||||
MatterWrap.Body.wrap(body, body.plugin.wrap);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
Bounds: {
|
||||||
|
/**
|
||||||
|
* 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 {?Matter.Vector} A translation vector (only if wrapping is required).
|
||||||
|
*/
|
||||||
|
wrap: function wrap(objectBounds, bounds) {
|
||||||
|
var x = null,
|
||||||
|
y = null;
|
||||||
|
|
||||||
|
if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
|
||||||
|
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 (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) {
|
||||||
|
return {
|
||||||
|
x: x || 0,
|
||||||
|
y: y || 0
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Body: {
|
Body: {
|
||||||
/**
|
/**
|
||||||
* Wraps the `body` position such that it always stay within the given bounds.
|
* 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,
|
* Upon crossing a boundary the body will appear on the opposite side of the bounds,
|
||||||
* while maintaining its velocity.
|
* while maintaining its velocity.
|
||||||
* This is called automatically by the plugin.
|
* This is called automatically by the plugin.
|
||||||
* @function MatterAttractors.Body.wrap
|
* @function MatterWrap.Body.wrap
|
||||||
* @param {Matter.Body} body The body to wrap.
|
* @param {Matter.Body} body The body to wrap.
|
||||||
* @param {Matter.Bounds} bounds The bounds to wrap the body inside.
|
* @param {Matter.Bounds} bounds The bounds to wrap the body inside.
|
||||||
* @returns {void} No return value.
|
* @returns {?Matter.Vector} The translation vector that was applied (only if wrapping was required).
|
||||||
*/
|
*/
|
||||||
wrap: function wrap(body, bounds) {
|
wrap: function wrap(body, bounds) {
|
||||||
var x = null,
|
var translation = MatterWrap.Bounds.wrap(body.bounds, bounds);
|
||||||
y = null;
|
|
||||||
|
|
||||||
if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
|
if (translation) {
|
||||||
if (body.bounds.min.x > bounds.max.x) {
|
Matter.Body.translate(body, translation);
|
||||||
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') {
|
return translation;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (x !== null || y !== null) {
|
return Matter.Bounds.create(vertices);
|
||||||
Matter.Body.setPosition(body, {
|
},
|
||||||
x: x || body.position.x,
|
|
||||||
y: y || body.position.y
|
/**
|
||||||
});
|
* 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
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
/***/ })
|
/***/ })
|
||||||
/******/ ]);
|
/******/ ]);
|
||||||
});
|
});
|
Loading…
Reference in a new issue