Skip to content

Commit 7cae797

Browse files
authored
Combine consecutive duplicate points in polygons to remove zero-length edges. Fixes #55 (#59)
1 parent 1e298e5 commit 7cae797

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

SAT.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
}
311311
SAT['Polygon'] = Polygon;
312312

313-
// Set the points of the polygon.
313+
// Set the points of the polygon. Any consecutive duplicate points will be combined.
314314
//
315315
// Note: The points are counter-clockwise *with respect to the coordinate system*.
316316
// If you directly draw the points on a screen that has the origin at the top-left corner
@@ -331,6 +331,14 @@
331331
var normals = this['normals'] = [];
332332
// Allocate the vector arrays for the calculated properties
333333
for (i = 0; i < points.length; i++) {
334+
// Remove consecutive duplicate points
335+
var p1 = points[i];
336+
var p2 = i < points.length - 1 ? points[i + 1] : points[0];
337+
if (p1 !== p2 && p1.x === p2.x && p1.y === p2.y) {
338+
points.splice(i, 1);
339+
i -= 1;
340+
continue;
341+
}
334342
calcPoints.push(new Vector());
335343
edges.push(new Vector());
336344
normals.push(new Vector());

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
"sat",
99
"game"
1010
],
11-
"license": {
12-
"type": "MIT",
13-
"url": "http://github.com/jriecken/sat-js/raw/master/LICENSE"
14-
},
11+
"license": "MIT",
1512
"repository": {
1613
"type": "git",
1714
"url": "git://github.com/jriecken/sat-js.git"

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ describe("Collision", function() {
9292
assert(!collided);
9393
});
9494

95+
it('testPolygonCircle - line - not collide', function () {
96+
var V = SAT.Vector;
97+
var C = SAT.Circle;
98+
var B = SAT.Box;
99+
100+
var circle = new C(new V(50,50), 20);
101+
var polygon = new B(new V(1000,1000), 100, 0).toPolygon();
102+
var response = new SAT.Response();
103+
var collided = SAT.testPolygonCircle(polygon, circle, response);
104+
assert(!collided);
105+
})
106+
107+
it('testPolygonCircle - line - collide', function () {
108+
var V = SAT.Vector;
109+
var C = SAT.Circle;
110+
var B = SAT.Box;
111+
112+
var circle = new C(new V(50,50), 20);
113+
var polygon = new B(new V(50,50), 100, 0).toPolygon();
114+
var response = new SAT.Response();
115+
var collided = SAT.testPolygonCircle(polygon, circle, response);
116+
117+
assert(collided);
118+
assert(response.overlap.toFixed(2) == "20.00");
119+
})
120+
95121
it("testPolygonPolygon", function() {
96122
var V = SAT.Vector;
97123
var P = SAT.Polygon;

0 commit comments

Comments
 (0)