diff --git a/SAT.js b/SAT.js index fab5994..1fffbbe 100644 --- a/SAT.js +++ b/SAT.js @@ -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 @@ -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']; } diff --git a/test/test.js b/test/test.js index 5fd9694..e603c78 100644 --- a/test/test.js +++ b/test/test.js @@ -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)); + }); +});