0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00

added minimum area check to Bodies.fromVertices

This commit is contained in:
liabru 2015-03-09 20:51:07 +00:00
parent 2b6a8d38af
commit bf11ee5504

View file

@ -166,6 +166,7 @@ var Bodies = {};
* If the vertices are not convex, they will be decomposed if [poly-decomp.js](https://github.com/schteppe/poly-decomp.js) is available. * If the vertices are not convex, they will be decomposed if [poly-decomp.js](https://github.com/schteppe/poly-decomp.js) is available.
* If the vertices can not be decomposed, the function will use the convex hull. * If the vertices can not be decomposed, the function will use the convex hull.
* By default the decomposition will discard collinear edges (to improve performance). * By default the decomposition will discard collinear edges (to improve performance).
* It will also discard any particularly small parts that have an area less than `minimumArea` (to improve stability).
* The options parameter is an object that specifies any properties you wish to override the defaults. * The options parameter is an object that specifies any properties you wish to override the defaults.
* See the properties section of the `Matter.Body` module for detailed information on what you can pass via the `options` object. * See the properties section of the `Matter.Body` module for detailed information on what you can pass via the `options` object.
* @method fromVertices * @method fromVertices
@ -174,9 +175,10 @@ var Bodies = {};
* @param [vector] vertices * @param [vector] vertices
* @param {object} [options] * @param {object} [options]
* @param {bool} [removeCollinear=true] * @param {bool} [removeCollinear=true]
* @param {number} [minimumArea=100]
* @return {body} * @return {body}
*/ */
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear) { Bodies.fromVertices = function(x, y, vertices, options, removeCollinear, minimumArea) {
var canDecompose = true, var canDecompose = true,
body, body,
i, i,
@ -186,6 +188,7 @@ var Bodies = {};
options = options || {}; options = options || {};
removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : true; removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : true;
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 100;
if (Vertices.isConvex(vertices)) { if (Vertices.isConvex(vertices)) {
// vertices are convex, so just create a body normally // vertices are convex, so just create a body normally
@ -229,6 +232,10 @@ var Bodies = {};
chunkVertices.push({ x: chunk.vertices[j][0], y: chunk.vertices[j][1] }); chunkVertices.push({ x: chunk.vertices[j][0], y: chunk.vertices[j][1] });
} }
// skip small chunks
if (minimumArea > 0 && Vertices.area(chunkVertices) < minimumArea)
continue;
// create a compound part // create a compound part
parts.push( parts.push(
Body.create(Common.extend({ Body.create(Common.extend({