Skip to content

Commit

Permalink
Allow pointInPolygon to work with small polygons. Fixes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
jriecken committed Sep 11, 2016
1 parent 2160579 commit c60de8b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 98 deletions.
8 changes: 4 additions & 4 deletions SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,11 @@
*/
var T_RESPONSE = new Response();

// Unit square polygon used for polygon hit detection.
// Tiny "point" polygon used for polygon hit detection.
/**
* @type {Polygon}
*/
var UNIT_SQUARE = new Box(new Vector(), 1, 1).toPolygon();
var TEST_POINT = new Box(new Vector(), 0.000001, 0.000001).toPolygon();

// ## Helper Functions

Expand Down Expand Up @@ -736,9 +736,9 @@
* @return {boolean} true if the point is inside the polygon, false if it is not.
*/
function pointInPolygon(p, poly) {
UNIT_SQUARE['pos'].copy(p);
TEST_POINT['pos'].copy(p);
T_RESPONSE.clear();
var result = testPolygonPolygon(UNIT_SQUARE, poly, T_RESPONSE);
var result = testPolygonPolygon(TEST_POINT, poly, T_RESPONSE);
if (result) {
result = T_RESPONSE['aInB'];
}
Expand Down
197 changes: 103 additions & 94 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,103 @@
var SAT = require('..');
var assert = require('assert');

describe("collision of", function(){
it("two circles", function(){
var V = SAT.Vector;
var C = SAT.Circle;

var circle1 = new C(new V(0,0), 20);
var circle2 = new C(new V(30,0), 20);
var response = new SAT.Response();
var collided = SAT.testCircleCircle(circle1, circle2, response);

assert( collided );
assert( response.overlap == 10 );
assert( response.overlapV.x == 10 && response.overlapV.y === 0);
});

it("circle and polygon", function(){

var V = SAT.Vector;
var C = SAT.Circle;
var P = SAT.Polygon;

var circle = new C(new V(50,50), 20);
// A square
var polygon = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
]);
var response = new SAT.Response();
var collided = SAT.testPolygonCircle(polygon, circle, response);

assert(collided);
assert(response.overlap.toFixed(2) == "5.86");
assert(
response.overlapV.x.toFixed(2) == "4.14" &&
response.overlapV.y.toFixed(2) == "4.14"
);
});

it("polygon and polygon", function(){
var V = SAT.Vector;
var P = SAT.Polygon;

// A square
var polygon1 = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
]);
// A triangle
var polygon2 = new P(new V(30,0), [
new V(0,0), new V(30, 0), new V(0, 30)
]);
var response = new SAT.Response();
var collided = SAT.testPolygonPolygon(polygon1, polygon2, response);

assert( collided );
assert( response.overlap == 10 );
assert( response.overlapV.x == 10 && response.overlapV.y === 0);
});
});

describe("No collision between", function(){
it("two boxes", function(){
var V = SAT.Vector;
var B = SAT.Box;

var box1 = new B(new V(0,0), 20, 20).toPolygon();
var box2 = new B(new V(100,100), 20, 20).toPolygon();
var collided = SAT.testPolygonPolygon(box1, box2);
});
});

describe("Hit testing", function(){
it("a circle", function(){
var V = SAT.Vector;
var C = SAT.Circle;

var circle = new C(new V(100,100), 20);

assert(!SAT.pointInCircle(new V(0,0), circle)); // false
assert(SAT.pointInCircle(new V(110,110), circle)); // true
});
it("a polygon", function(){
var V = SAT.Vector;
var C = SAT.Circle;
var P = SAT.Polygon;

var triangle = new P(new V(30,0), [
new V(0,0), new V(30, 0), new V(0, 30)
]);
assert(!SAT.pointInPolygon(new V(0,0), triangle)); // false
assert(SAT.pointInPolygon(new V(35, 5), triangle)); // true
});
});
var SAT = require('..');
var assert = require('assert');

describe("collision of", function(){
it("two circles", function(){
var V = SAT.Vector;
var C = SAT.Circle;

var circle1 = new C(new V(0,0), 20);
var circle2 = new C(new V(30,0), 20);
var response = new SAT.Response();
var collided = SAT.testCircleCircle(circle1, circle2, response);

assert( collided );
assert( response.overlap == 10 );
assert( response.overlapV.x == 10 && response.overlapV.y === 0);
});

it("circle and polygon", function(){

var V = SAT.Vector;
var C = SAT.Circle;
var P = SAT.Polygon;

var circle = new C(new V(50,50), 20);
// A square
var polygon = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
]);
var response = new SAT.Response();
var collided = SAT.testPolygonCircle(polygon, circle, response);

assert(collided);
assert(response.overlap.toFixed(2) == "5.86");
assert(
response.overlapV.x.toFixed(2) == "4.14" &&
response.overlapV.y.toFixed(2) == "4.14"
);
});

it("polygon and polygon", function(){
var V = SAT.Vector;
var P = SAT.Polygon;

// A square
var polygon1 = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
]);
// A triangle
var polygon2 = new P(new V(30,0), [
new V(0,0), new V(30, 0), new V(0, 30)
]);
var response = new SAT.Response();
var collided = SAT.testPolygonPolygon(polygon1, polygon2, response);

assert( collided );
assert( response.overlap == 10 );
assert( response.overlapV.x == 10 && response.overlapV.y === 0);
});
});

describe("No collision between", function(){
it("two boxes", function(){
var V = SAT.Vector;
var B = SAT.Box;

var box1 = new B(new V(0,0), 20, 20).toPolygon();
var box2 = new B(new V(100,100), 20, 20).toPolygon();
var collided = SAT.testPolygonPolygon(box1, box2);
});
});

describe("Hit testing", function(){
it("a circle", function(){
var V = SAT.Vector;
var C = SAT.Circle;

var circle = new C(new V(100,100), 20);

assert(!SAT.pointInCircle(new V(0,0), circle)); // false
assert(SAT.pointInCircle(new V(110,110), circle)); // true
});
it("a polygon", function(){
var V = SAT.Vector;
var C = SAT.Circle;
var P = SAT.Polygon;

var triangle = new P(new V(30,0), [
new V(0,0), new V(30, 0), new V(0, 30)
]);
assert(!SAT.pointInPolygon(new V(0,0), triangle)); // false
assert(SAT.pointInPolygon(new V(35, 5), triangle)); // true
});
it("a small polygon", function () {
var V = SAT.Vector;
var C = SAT.Circle;
var P = SAT.Polygon;

var v1 = new V(1, 1.1);
var p1 = new P(new V(0,0),[new V(2,1), new V(2,2), new V(1,3), new V(0,2),new V(0,1),new V(1,0)]);
assert(SAT.pointInPolygon(v1, p1));
});
});

0 comments on commit c60de8b

Please sign in to comment.