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

fix decomp require and improve warning message

This commit is contained in:
liabru 2021-01-31 21:18:25 +00:00
parent 8ee0ebbfee
commit e87f64a2f3

View file

@ -21,6 +21,8 @@ var Vector = require('../geometry/Vector');
(function() { (function() {
Bodies._decompWarned = false;
/** /**
* Creates a new rigid body model with a rectangle hull. * Creates a new rigid body model with a rectangle hull.
* 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.
@ -197,10 +199,12 @@ var Vector = require('../geometry/Vector');
* @return {body} * @return {body}
*/ */
Bodies.fromVertices = function(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea, removeDuplicatePoints) { Bodies.fromVertices = function(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea, removeDuplicatePoints) {
var decomp = require('poly-decomp'), var decomp,
canDecomp,
body, body,
parts, parts,
isConvex, isConvex,
isConcave,
vertices, vertices,
i, i,
j, j,
@ -208,6 +212,16 @@ var Vector = require('../geometry/Vector');
v, v,
z; z;
try {
decomp = require('poly-decomp');
} catch (e) {
// continue without decomp
decomp = null;
}
// check expected decomp module was resolved
canDecomp = Boolean(decomp && decomp.quickDecomp);
options = options || {}; options = options || {};
parts = []; parts = [];
@ -216,10 +230,6 @@ var Vector = require('../geometry/Vector');
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10; minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10;
removeDuplicatePoints = typeof removeDuplicatePoints !== 'undefined' ? removeDuplicatePoints : 0.01; removeDuplicatePoints = typeof removeDuplicatePoints !== 'undefined' ? removeDuplicatePoints : 0.01;
if (!decomp) {
Common.warn('Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.');
}
// ensure vertexSets is an array of arrays // ensure vertexSets is an array of arrays
if (!Common.isArray(vertexSets[0])) { if (!Common.isArray(vertexSets[0])) {
vertexSets = [vertexSets]; vertexSets = [vertexSets];
@ -228,8 +238,19 @@ var Vector = require('../geometry/Vector');
for (v = 0; v < vertexSets.length; v += 1) { for (v = 0; v < vertexSets.length; v += 1) {
vertices = vertexSets[v]; vertices = vertexSets[v];
isConvex = Vertices.isConvex(vertices); isConvex = Vertices.isConvex(vertices);
isConcave = !isConvex;
if (isConvex || !decomp) { if (isConcave && !canDecomp && !Bodies._decompWarned) {
Common.warn(
'Could not resolve the expected \'poly-decomp\' package for concave vertices in \'Bodies.fromVertices\''
);
Common.warn(
'Try \'npm install poly-decomp --save\' or as a global e.g. \'window.decomp\''
);
Bodies._decompWarned = true;
}
if (isConvex || !canDecomp) {
if (isConvex) { if (isConvex) {
vertices = Vertices.clockwiseSort(vertices); vertices = Vertices.clockwiseSort(vertices);
} else { } else {