Skip to content

Commit 4a2c6e7

Browse files
committed
refactor Bodies.fromVertices
1 parent d0fdd29 commit 4a2c6e7

1 file changed

Lines changed: 49 additions & 60 deletions

File tree

src/factory/Bodies.js

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -174,50 +174,51 @@ var Bodies = {};
174174
* @param {number} y
175175
* @param [vector] vertices
176176
* @param {object} [options]
177-
* @param {bool} [removeCollinear=true]
177+
* @param {number} [removeCollinear=0.01]
178178
* @param {number} [minimumArea=100]
179+
* @param {bool} [flagInternal=false]
179180
* @return {body}
180181
*/
181-
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear, minimumArea) {
182-
var canDecompose = true,
183-
body,
182+
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear, minimumArea, flagInternal) {
183+
var body,
184+
isConvex,
184185
i,
185186
j,
186187
k,
187188
z;
188189

189190
options = options || {};
190-
removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : true;
191-
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 100;
191+
removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : 0.01;
192+
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10;
193+
flagInternal = typeof flagInternal !== 'undefined' ? flagInternal : false;
194+
isConvex = Vertices.isConvex(vertices);
195+
196+
if (isConvex || !window.decomp) {
197+
if (isConvex) {
198+
vertices = Vertices.clockwiseSort(vertices);
199+
} else {
200+
// fallback to convex hull when decomposition is not possible
201+
vertices = Vertices.hull(vertices);
202+
Common.log('Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.', 'warn');
203+
}
192204

193-
if (Vertices.isConvex(vertices)) {
194-
// vertices are convex, so just create a body normally
195205
body = {
196206
position: { x: x, y: y },
197-
vertices: Vertices.clockwiseSort(vertices)
207+
vertices: vertices
198208
};
199209

200210
return Body.create(Common.extend({}, body, options));
201-
}
202-
203-
// check for poly-decomp.js
204-
if (!window.decomp) {
205-
Common.log('Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.', 'warn');
206-
canDecompose = false;
207-
}
208-
209-
// initialise a decomposition
210-
var concave = new decomp.Polygon();
211-
for (i = 0; i < vertices.length; i++) {
212-
concave.vertices.push([vertices[i].x, vertices[i].y]);
213-
}
211+
} else {
212+
// initialise a decomposition
213+
var concave = new decomp.Polygon();
214+
for (i = 0; i < vertices.length; i++) {
215+
concave.vertices.push([vertices[i].x, vertices[i].y]);
216+
}
214217

215-
// try to decompose
216-
if (canDecompose) {
217218
// vertices are concave and simple, we can decompose into parts
218219
concave.makeCCW();
219-
if (removeCollinear)
220-
concave.removeCollinearPoints(0.001);
220+
if (removeCollinear !== false)
221+
concave.removeCollinearPoints(removeCollinear);
221222

222223
var decomposed = concave.quickDecomp(),
223224
parts = [];
@@ -245,57 +246,45 @@ var Bodies = {};
245246
);
246247
}
247248

248-
// flag internal edges (coincident part edges)
249-
var coincident_max_dist = 1;
249+
if (flagInternal) {
250+
// flag internal edges (coincident part edges)
251+
var coincident_max_dist = 1;
250252

251-
for (i = 0; i < parts.length; i++) {
252-
var partA = parts[i];
253+
for (i = 0; i < parts.length; i++) {
254+
var partA = parts[i];
253255

254-
for (j = i + 1; j < parts.length; j++) {
255-
var partB = parts[j];
256+
for (j = i + 1; j < parts.length; j++) {
257+
var partB = parts[j];
256258

257-
if (Bounds.overlaps(partA.bounds, partB.bounds)) {
258-
var pav = partA.vertices,
259-
pbv = partB.vertices;
259+
if (Bounds.overlaps(partA.bounds, partB.bounds)) {
260+
var pav = partA.vertices,
261+
pbv = partB.vertices;
260262

261-
// iterate vertices of both parts
262-
for (k = 0; k < partA.vertices.length; k++) {
263-
for (z = 0; z < partB.vertices.length; z++) {
264-
// find distances between the vertices
265-
var da = Vector.magnitudeSquared(Vector.sub(pav[(k + 1) % pav.length], pbv[z])),
266-
db = Vector.magnitudeSquared(Vector.sub(pav[k], pbv[(z + 1) % pbv.length]));
263+
// iterate vertices of both parts
264+
for (k = 0; k < partA.vertices.length; k++) {
265+
for (z = 0; z < partB.vertices.length; z++) {
266+
// find distances between the vertices
267+
var da = Vector.magnitudeSquared(Vector.sub(pav[(k + 1) % pav.length], pbv[z])),
268+
db = Vector.magnitudeSquared(Vector.sub(pav[k], pbv[(z + 1) % pbv.length]));
267269

268-
// if both vertices are very close, consider the edge concident (internal)
269-
if (da < coincident_max_dist && db < coincident_max_dist) {
270-
pav[k].isInternal = true;
271-
pbv[z].isInternal = true;
270+
// if both vertices are very close, consider the edge concident (internal)
271+
if (da < coincident_max_dist && db < coincident_max_dist) {
272+
pav[k].isInternal = true;
273+
pbv[z].isInternal = true;
274+
}
272275
}
273276
}
274-
}
275277

278+
}
276279
}
277280
}
278281
}
279282

280-
// update axes now that we have flagged the internal edges
281-
for (i = 0; i < parts.length; i++) {
282-
var part = parts[i];
283-
part.axes = Axes.fromVertices(part.vertices);
284-
}
285-
286283
// create the parent body to be returned, that contains generated compound parts
287284
body = Body.create(Common.extend({ parts: parts.slice(0) }, options));
288285
Body.setPosition(body, { x: x, y: y });
289286

290287
return body;
291-
} else {
292-
// fallback to convex hull when decomposition is not possible
293-
body = {
294-
position: { x: x, y: y },
295-
vertices: Vertices.hull(vertices)
296-
};
297-
298-
return Body.create(Common.extend({}, body, options));
299288
}
300289
};
301290

0 commit comments

Comments
 (0)