Skip to content

Commit

Permalink
added internal edge flagging to Bodies.fromVertices
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Feb 25, 2015
1 parent bc7dfc7 commit ca75fde
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/factory/Bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ var Bodies = {};
Bodies.fromVertices = function(x, y, vertices, options, removeCollinear) {
var canDecompose = true,
body,
i;
i,
j,
k,
z;

options = options || {};
removeCollinear = typeof removeCollinear !== 'undefined' ? removeCollinear : true;
Expand Down Expand Up @@ -228,7 +231,7 @@ var Bodies = {};
chunkVertices = [];

// convert vertices into the correct structure
for (var j = 0; j < chunk.vertices.length; j++) {
for (j = 0; j < chunk.vertices.length; j++) {
chunkVertices.push({ x: chunk.vertices[j][0], y: chunk.vertices[j][1] });
}

Expand All @@ -241,8 +244,46 @@ var Bodies = {};
);
}

// flag internal edges (coincident part edges)
var coincident_max_dist = 1;

for (i = 0; i < parts.length; i++) {
var partA = parts[i];

for (j = i + 1; j < parts.length; j++) {
var partB = parts[j];

if (Bounds.overlaps(partA.bounds, partB.bounds)) {
var pav = partA.vertices,
pbv = partB.vertices;

// iterate vertices of both parts
for (k = 0; k < partA.vertices.length; k++) {
for (z = 0; z < partB.vertices.length; z++) {
// find distances between the vertices
var da = Vector.magnitudeSquared(Vector.sub(pav[(k + 1) % pav.length], pbv[z])),
db = Vector.magnitudeSquared(Vector.sub(pav[k], pbv[(z + 1) % pbv.length]));

// if both vertices are very close, consider the edge concident (internal)
if (da < coincident_max_dist && db < coincident_max_dist) {
pav[k].isInternal = true;
pbv[z].isInternal = true;
}
}
}

}
}
}

// update axes now that we have flagged the internal edges
for (i = 0; i < parts.length; i++) {
var part = parts[i];
part.axes = Axes.fromVertices(part.vertices);
}

// create the parent body to be returned, that contains generated compound parts
body = Body.create(Common.extend({}, { parts: parts }, options));
body = Body.create(Common.extend({ parts: parts.slice(0) }, options));
Body.setPosition(body, { x: x, y: y });

return body;
Expand Down

0 comments on commit ca75fde

Please sign in to comment.