Skip to content

Commit

Permalink
added minimum area check to Bodies.fromVertices
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Mar 9, 2015
1 parent 2b6a8d3 commit bf11ee5
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/factory/Bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 can not be decomposed, the function will use the convex hull.
* 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.
* See the properties section of the `Matter.Body` module for detailed information on what you can pass via the `options` object.
* @method fromVertices
Expand All @@ -174,9 +175,10 @@ var Bodies = {};
* @param [vector] vertices
* @param {object} [options]
* @param {bool} [removeCollinear=true]
* @param {number} [minimumArea=100]
* @return {body}
*/
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear) {
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear, minimumArea) {
var canDecompose = true,
body,
i,
Expand All @@ -186,6 +188,7 @@ var Bodies = {};

options = options || {};
removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : true;
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 100;

if (Vertices.isConvex(vertices)) {
// vertices are convex, so just create a body normally
Expand Down Expand Up @@ -229,6 +232,10 @@ var Bodies = {};
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
parts.push(
Body.create(Common.extend({
Expand Down

0 comments on commit bf11ee5

Please sign in to comment.