Socket
Socket
Sign inDemoInstall

sat

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.6.0 to 0.7.0

5

CHANGELOG.md
# Changelog
## In development
## O.7.0 (Feb 17, 2018)
- Add `getCentroid` method to `Polygon` that computes the [centroid](https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon). (Fixes #50)
- Useful for computing the center of a polygon if you want to rotate around it.
## 0.6.0 (Sept 11, 2016)

@@ -6,0 +9,0 @@

6

package.json
{
"name": "sat",
"description": "Library for performing 2D collision detection",
"version": "0.6.0",
"version": "0.7.0",
"author": "Jim Riecken <jriecken@gmail.com>",

@@ -22,4 +22,4 @@ "keywords": [

},
"scripts":{
"test": "mocha"
"scripts": {
"test": "mocha"
},

@@ -26,0 +26,0 @@ "main": "SAT.js",

@@ -21,3 +21,3 @@ SAT.js

Current version: `0.6.0`. [Annotated source code](http://jriecken.github.io/sat-js/docs/SAT.html) is available.
Current version: `0.7.0`. [Annotated source code](http://jriecken.github.io/sat-js/docs/SAT.html) is available.

@@ -24,0 +24,0 @@ Nicely compresses with the [Google Closure Compiler](https://developers.google.com/closure/compiler/) in **Advanced** mode to about 6KB (2KB gzipped)

@@ -1,2 +0,2 @@

// Version 0.6.0 - Copyright 2012 - 2016 - Jim Riecken <jimr@jimr.ca>
// Version 0.7.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca>
//

@@ -7,7 +7,7 @@ // Released under the MIT License - https://github.com/jriecken/sat-js

// polygons using the Separating Axis Theorem.
/** @preserve SAT.js - Version 0.6.0 - Copyright 2012 - 2016 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
/** @preserve SAT.js - Version 0.7.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
/*global define: false, module: false*/
/*jshint shadow:true, sub:true, forin:true, noarg:true, noempty:true,
eqeqeq:true, bitwise:true, strict:true, undef:true,
/*jshint shadow:true, sub:true, forin:true, noarg:true, noempty:true,
eqeqeq:true, bitwise:true, strict:true, undef:true,
curly:true, browser:true */

@@ -49,3 +49,3 @@

// a coordinate is not specified, it will be set to `0`
/**
/**
* @param {?number=} x The x position.

@@ -117,4 +117,4 @@ * @param {?number=} y The y position.

};
// Normalize this vector. (make it have length of `1`)

@@ -132,3 +132,3 @@ /**

};
// Add another vector to this one.

@@ -144,3 +144,3 @@ /**

};
// Subtract another vector from this one.

@@ -156,3 +156,3 @@ /**

};
// Scale this vector. An independant scaling factor can be provided

@@ -169,5 +169,5 @@ // for each axis, or a single scaling factor that will scale both `x` and `y`.

this['y'] *= y || x;
return this;
return this;
};
// Project this vector on to another vector.

@@ -184,3 +184,3 @@ /**

};
// Project this vector onto a vector of unit length. This is slightly more efficient

@@ -198,3 +198,3 @@ // than `project` when dealing with unit vectors.

};
// Reflect this vector on an arbitrary axis.

@@ -213,3 +213,3 @@ /**

};
// Reflect this vector on an arbitrary axis (represented by a unit vector). This is

@@ -229,3 +229,3 @@ // slightly more efficient than `reflect` when dealing with an axis that is a unit vector.

};
// Get the dot product of this vector and another.

@@ -239,3 +239,3 @@ /**

};
// Get the squared length of this vector.

@@ -248,3 +248,3 @@ /**

};
// Get the length of this vector.

@@ -257,3 +257,3 @@ /**

};
// ## Circle

@@ -276,3 +276,3 @@ //

SAT['Circle'] = Circle;
// Compute the axis-aligned bounding box (AABB) of this Circle.

@@ -316,3 +316,3 @@ //

SAT['Polygon'] = Polygon;
// Set the points of the polygon.

@@ -404,4 +404,4 @@ //

for (var i = 0; i < len; i++) {
points[i].x += x;
points[i].y += y;
points[i]["x"] += x;
points[i]["y"] += y;
}

@@ -438,4 +438,4 @@ this._recalc();

var calcPoint = calcPoints[i].copy(points[i]);
calcPoint.x += offset.x;
calcPoint.y += offset.y;
calcPoint["x"] += offset["x"];
calcPoint["y"] += offset["y"];
if (angle !== 0) {

@@ -454,4 +454,4 @@ calcPoint.rotate(angle);

};
// Compute the axis-aligned bounding box. Any current state

@@ -488,4 +488,27 @@ // (translations/rotations) will be applied before constructing the AABB.

};
// Compute the centroid (geometric center) of the polygon
//
// See https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon
Polygon.prototype["getCentroid"] = Polygon.prototype.getCentroid = function() {
var points = this["calcPoints"];
var len = points.length;
var cx = 0;
var cy = 0;
var ar = 0;
for (var i = 0; i < len; i++) {
var p1 = points[i];
var p2 = i === len - 1 ? points[0] : points[i+1]; // Loop around if last point
var a = p1["x"] * p2["y"] - p2["x"] * p1["y"];
cx += (p1["x"] + p2["x"]) * a;
cy += (p1["y"] + p2["y"]) * a;
ar += a;
}
ar = ar * 3; // we want 1 / 6 the area and we currently hae 2*area
cx = cx / ar;
cy = cy / ar;
return new Vector(cx, cy);
};
// ## Box

@@ -521,7 +544,7 @@ //

return new Polygon(new Vector(pos['x'], pos['y']), [
new Vector(), new Vector(w, 0),
new Vector(), new Vector(w, 0),
new Vector(w,h), new Vector(0,h)
]);
};
// ## Response

@@ -537,3 +560,3 @@ //

* @constructor
*/
*/
function Response() {

@@ -570,3 +593,3 @@ this['a'] = null;

for (var i = 0; i < 10; i++) { T_VECTORS.push(new Vector()); }
// A pool of arrays of numbers used in calculations to avoid allocating

@@ -616,3 +639,3 @@ // memory.

}
// Check whether two convex polygons are separated by the specified

@@ -647,4 +670,4 @@ // axis (must be a unit vector).

if (rangeA[0] > rangeB[1] || rangeB[0] > rangeA[1]) {
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_ARRAYS.push(rangeB);

@@ -660,3 +683,3 @@ return true;

// A ends before B does. We have to pull A out of B
if (rangeA[1] < rangeB[1]) {
if (rangeA[1] < rangeB[1]) {
overlap = rangeA[1] - rangeB[0];

@@ -674,3 +697,3 @@ response['bInA'] = false;

// B ends before A ends. We have to push A out of B
if (rangeA[1] > rangeB[1]) {
if (rangeA[1] > rangeB[1]) {
overlap = rangeA[0] - rangeB[1];

@@ -693,6 +716,6 @@ response['aInB'] = false;

}
}
}
}
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_ARRAYS.push(rangeB);

@@ -702,3 +725,3 @@ return false;

SAT['isSeparatingAxis'] = isSeparatingAxis;
// Calculates which Voronoi region a point is on a line segment.

@@ -742,3 +765,3 @@ // It is assumed that both the line and the point are relative to `(0,0)`

var RIGHT_VORONOI_REGION = 1;
// ## Collision Tests

@@ -785,3 +808,3 @@

* the circles intersect.
* @return {boolean} true if the circles intersect, false if they don't.
* @return {boolean} true if the circles intersect, false if they don't.
*/

@@ -801,3 +824,3 @@ function testCircleCircle(a, b, response) {

// They intersect. If we're calculating a response, calculate the overlap.
if (response) {
if (response) {
var dist = Math.sqrt(distanceSq);

@@ -816,3 +839,3 @@ response['a'] = a;

SAT['testCircleCircle'] = testCircleCircle;
// Check if a polygon and a circle collide.

@@ -835,3 +858,3 @@ /**

var point = T_VECTORS.pop();
// For each edge in the polygon:

@@ -843,3 +866,3 @@ for (var i = 0; i < len; i++) {

var overlapN = null;
// Get the edge.

@@ -849,3 +872,3 @@ edge.copy(polygon['edges'][i]);

point.copy(circlePos).sub(points[i]);
// If the distance between the center of the circle and the point

@@ -857,3 +880,3 @@ // is bigger than the radius, the polygon is definitely not fully in

}
// Calculate which Voronoi region the center of the circle is in.

@@ -873,5 +896,5 @@ var region = voronoiRegion(edge, point);

// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);
T_VECTORS.push(point);
T_VECTORS.push(point2);

@@ -899,6 +922,6 @@ return false;

// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);
return false;
return false;
} else if (response) {

@@ -916,3 +939,3 @@ // It intersects, calculate the overlap.

var normal = edge.perp().normalize();
// Find the perpendicular distance between the center of the
// Find the perpendicular distance between the center of the
// circle and the edge.

@@ -924,4 +947,4 @@ var dist = point.dot(normal);

// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(normal);
T_VECTORS.push(circlePos);
T_VECTORS.push(normal);
T_VECTORS.push(point);

@@ -940,4 +963,4 @@ return false;

}
// If this is the smallest overlap we've seen, keep it.
// If this is the smallest overlap we've seen, keep it.
// (overlapN may be null if the circle was in the wrong Voronoi region).

@@ -949,3 +972,3 @@ if (overlapN && response && Math.abs(overlap) < Math.abs(response['overlap'])) {

}
// Calculate the final overlap vector - based on the smallest overlap.

@@ -957,4 +980,4 @@ if (response) {

}
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);

@@ -964,3 +987,3 @@ return true;

SAT['testPolygonCircle'] = testPolygonCircle;
// Check if a circle and a polygon collide.

@@ -994,3 +1017,3 @@ //

SAT['testCirclePolygon'] = testCirclePolygon;
// Checks whether polygons collide.

@@ -997,0 +1020,0 @@ /**

@@ -1,14 +0,15 @@

/* SAT.js - Version 0.6.0 - Copyright 2012 - 2016 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
function x(){function c(a,e){this.x=a||0;this.y=e||0}function B(a,e){this.pos=a||new c;this.r=e||0}function n(a,e){this.pos=a||new c;this.angle=0;this.offset=new c;this.u(e||[])}function q(a,e,b){this.pos=a||new c;this.w=e||0;this.h=b||0}function w(){this.b=this.a=null;this.overlapN=new c;this.overlapV=new c;this.clear()}function C(a,e,b){for(var h=Number.MAX_VALUE,c=-Number.MAX_VALUE,k=a.length,g=0;g<k;g++){var d=a[g].f(e);d<h&&(h=d);d>c&&(c=d)}b[0]=h;b[1]=c}function y(a,e,b,h,c,k){var g=r.pop(),
d=r.pop();a=m.pop().c(e).sub(a);e=a.f(c);C(b,c,g);C(h,c,d);d[0]+=e;d[1]+=e;if(g[0]>d[1]||d[0]>g[1])return m.push(a),r.push(g),r.push(d),!0;k&&(g[0]<d[0]?(k.aInB=!1,g[1]<d[1]?(b=g[1]-d[0],k.bInA=!1):(b=g[1]-d[0],h=d[1]-g[0],b=b<h?b:-h)):(k.bInA=!1,g[1]>d[1]?(b=g[0]-d[1],k.aInB=!1):(b=g[1]-d[0],h=d[1]-g[0],b=b<h?b:-h)),h=Math.abs(b),h<k.overlap&&(k.overlap=h,k.overlapN.c(c),0>b&&k.overlapN.reverse()));m.push(a);r.push(g);r.push(d);return!1}function z(a,e){var b=a.g(),c=e.f(a);return 0>c?-1:c>b?1:0}
function D(a,e,b){for(var c=m.pop().c(e.pos).sub(a.pos),l=e.r,k=l*l,g=a.calcPoints,d=g.length,u=m.pop(),f=m.pop(),n=0;n<d;n++){var v=n===d-1?0:n+1,r=0===n?d-1:n-1,q=0,t=null;u.c(a.edges[n]);f.c(c).sub(g[n]);b&&f.g()>k&&(b.aInB=!1);var p=z(u,f);if(-1===p){u.c(a.edges[r]);v=m.pop().c(c).sub(g[r]);p=z(u,v);if(1===p){p=f.j();if(p>l)return m.push(c),m.push(u),m.push(f),m.push(v),!1;b&&(b.bInA=!1,t=f.normalize(),q=l-p)}m.push(v)}else if(1===p){if(u.c(a.edges[v]),f.c(c).sub(g[v]),p=z(u,f),-1===p){p=f.j();
if(p>l)return m.push(c),m.push(u),m.push(f),!1;b&&(b.bInA=!1,t=f.normalize(),q=l-p)}}else{v=u.m().normalize();p=f.f(v);r=Math.abs(p);if(0<p&&r>l)return m.push(c),m.push(v),m.push(f),!1;b&&(t=v,q=l-p,0<=p||q<2*l)&&(b.bInA=!1)}t&&b&&Math.abs(q)<Math.abs(b.overlap)&&(b.overlap=q,b.overlapN.c(t))}b&&(b.a=a,b.b=e,b.overlapV.c(b.overlapN).scale(b.overlap));m.push(c);m.push(u);m.push(f);return!0}function E(a,e,b){for(var c=a.calcPoints,l=c.length,k=e.calcPoints,g=k.length,d=0;d<l;d++)if(y(a.pos,e.pos,c,
k,a.normals[d],b))return!1;for(d=0;d<g;d++)if(y(a.pos,e.pos,c,k,e.normals[d],b))return!1;b&&(b.a=a,b.b=e,b.overlapV.c(b.overlapN).scale(b.overlap));return!0}var f={};f.Vector=c;f.V=c;c.prototype.copy=c.prototype.c=function(a){this.x=a.x;this.y=a.y;return this};c.prototype.clone=c.prototype.clone=function(){return new c(this.x,this.y)};c.prototype.perp=c.prototype.m=function(){var a=this.x;this.x=this.y;this.y=-a;return this};c.prototype.rotate=c.prototype.rotate=function(a){var e=this.x,b=this.y;
this.x=e*Math.cos(a)-b*Math.sin(a);this.y=e*Math.sin(a)+b*Math.cos(a);return this};c.prototype.reverse=c.prototype.reverse=function(){this.x=-this.x;this.y=-this.y;return this};c.prototype.normalize=c.prototype.normalize=function(){var a=this.j();0<a&&(this.x/=a,this.y/=a);return this};c.prototype.add=c.prototype.add=function(a){this.x+=a.x;this.y+=a.y;return this};c.prototype.sub=c.prototype.sub=function(a){this.x-=a.x;this.y-=a.y;return this};c.prototype.scale=c.prototype.scale=function(a,e){this.x*=
a;this.y*=e||a;return this};c.prototype.project=c.prototype.o=function(a){var e=this.f(a)/a.g();this.x=e*a.x;this.y=e*a.y;return this};c.prototype.projectN=c.prototype.s=function(a){var e=this.f(a);this.x=e*a.x;this.y=e*a.y;return this};c.prototype.reflect=function(a){var e=this.x,b=this.y;this.o(a).scale(2);this.x-=e;this.y-=b;return this};c.prototype.reflectN=function(a){var e=this.x,b=this.y;this.s(a).scale(2);this.x-=e;this.y-=b;return this};c.prototype.dot=c.prototype.f=function(a){return this.x*
a.x+this.y*a.y};c.prototype.len2=c.prototype.g=function(){return this.f(this)};c.prototype.len=c.prototype.j=function(){return Math.sqrt(this.g())};f.Circle=B;B.prototype.getAABB=function(){var a=this.r,e=this.pos.clone().sub(new c(a,a));return(new q(e,2*a,2*a)).l()};f.Polygon=n;n.prototype.setPoints=n.prototype.u=function(a){if(!this.points||this.points.length!==a.length){var e,b=this.calcPoints=[],h=this.edges=[],l=this.normals=[];for(e=0;e<a.length;e++)b.push(new c),h.push(new c),l.push(new c)}this.points=
a;this.i();return this};n.prototype.setAngle=function(a){this.angle=a;this.i();return this};n.prototype.setOffset=function(a){this.offset=a;this.i();return this};n.prototype.rotate=n.prototype.rotate=function(a){for(var e=this.points,b=e.length,c=0;c<b;c++)e[c].rotate(a);this.i();return this};n.prototype.translate=n.prototype.translate=function(a,c){for(var b=this.points,h=b.length,l=0;l<h;l++)b[l].x+=a,b[l].y+=c;this.i();return this};n.prototype.i=function(){var a=this.calcPoints,c=this.edges,b=
this.normals,h=this.points,l=this.offset,k=this.angle,g=h.length,d;for(d=0;d<g;d++){var f=a[d].c(h[d]);f.x+=l.x;f.y+=l.y;0!==k&&f.rotate(k)}for(d=0;d<g;d++)h=a[d],h=c[d].c(d<g-1?a[d+1]:a[0]).sub(h),b[d].c(h).m().normalize()};n.prototype.getAABB=function(){for(var a=this.calcPoints,e=a.length,b=a[0].x,h=a[0].y,f=a[0].x,k=a[0].y,g=1;g<e;g++){var d=a[g];d.x<b?b=d.x:d.x>f&&(f=d.x);d.y<h?h=d.y:d.y>k&&(k=d.y)}return(new q(this.pos.clone().add(new c(b,h)),f-b,k-h)).l()};f.Box=q;q.prototype.toPolygon=q.prototype.l=
function(){var a=this.pos,e=this.w,b=this.h;return new n(new c(a.x,a.y),[new c,new c(e,0),new c(e,b),new c(0,b)])};f.Response=w;w.prototype.clear=w.prototype.clear=function(){this.bInA=this.aInB=!0;this.overlap=Number.MAX_VALUE;return this};for(var m=[],t=0;10>t;t++)m.push(new c);for(var r=[],t=0;5>t;t++)r.push([]);var A=new w,F=(new q(new c,1E-6,1E-6)).l();f.isSeparatingAxis=y;f.pointInCircle=function(a,c){var b=m.pop().c(a).sub(c.pos),h=c.r*c.r,f=b.g();m.push(b);return f<=h};f.pointInPolygon=function(a,
c){F.pos.c(a);A.clear();var b=E(F,c,A);b&&(b=A.aInB);return b};f.testCircleCircle=function(a,c,b){var f=m.pop().c(c.pos).sub(a.pos),l=a.r+c.r,k=f.g();if(k>l*l)return m.push(f),!1;b&&(k=Math.sqrt(k),b.a=a,b.b=c,b.overlap=l-k,b.overlapN.c(f.normalize()),b.overlapV.c(f).scale(b.overlap),b.aInB=a.r<=c.r&&k<=c.r-a.r,b.bInA=c.r<=a.r&&k<=a.r-c.r);m.push(f);return!0};f.testPolygonCircle=D;f.testCirclePolygon=function(a,c,b){if((a=D(c,a,b))&&b){c=b.a;var f=b.aInB;b.overlapN.reverse();b.overlapV.reverse();
b.a=b.b;b.b=c;b.aInB=b.bInA;b.bInA=f}return a};f.testPolygonPolygon=E;return f}"function"===typeof define&&define.amd?define(x):"object"===typeof exports?module.exports=x():this.SAT=x();
/*
SAT.js - Version 0.7.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
function x(){function c(a,d){this.x=a||0;this.y=d||0}function B(a,d){this.pos=a||new c;this.r=d||0}function m(a,d){this.pos=a||new c;this.angle=0;this.offset=new c;this.u(d||[])}function p(a,d,b){this.pos=a||new c;this.w=d||0;this.h=b||0}function w(){this.b=this.a=null;this.overlapN=new c;this.overlapV=new c;this.clear()}function C(a,d,b){for(var g=Number.MAX_VALUE,c=-Number.MAX_VALUE,l=a.length,v=0;v<l;v++){var e=a[v].f(d);e<g&&(g=e);e>c&&(c=e)}b[0]=g;b[1]=c}function y(a,d,b,g,c,l){var k=q.pop(),
e=q.pop();a=h.pop().c(d).sub(a);d=a.f(c);C(b,c,k);C(g,c,e);e[0]+=d;e[1]+=d;if(k[0]>e[1]||e[0]>k[1])return h.push(a),q.push(k),q.push(e),!0;l&&(k[0]<e[0]?(l.aInB=!1,k[1]<e[1]?(b=k[1]-e[0],l.bInA=!1):(b=k[1]-e[0],g=e[1]-k[0],b=b<g?b:-g)):(l.bInA=!1,k[1]>e[1]?(b=k[0]-e[1],l.aInB=!1):(b=k[1]-e[0],g=e[1]-k[0],b=b<g?b:-g)),g=Math.abs(b),g<l.overlap&&(l.overlap=g,l.overlapN.c(c),0>b&&l.overlapN.reverse()));h.push(a);q.push(k);q.push(e);return!1}function z(a,d){var b=a.g(),g=d.f(a);return 0>g?-1:g>b?1:0}
function D(a,d,b){for(var g=h.pop().c(d.pos).sub(a.pos),c=d.r,l=c*c,v=a.calcPoints,e=v.length,t=h.pop(),f=h.pop(),m=0;m<e;m++){var u=m===e-1?0:m+1,q=0===m?e-1:m-1,p=0,r=null;t.c(a.edges[m]);f.c(g).sub(v[m]);b&&f.g()>l&&(b.aInB=!1);var n=z(t,f);if(-1===n){t.c(a.edges[q]);u=h.pop().c(g).sub(v[q]);n=z(t,u);if(1===n){n=f.j();if(n>c)return h.push(g),h.push(t),h.push(f),h.push(u),!1;b&&(b.bInA=!1,r=f.normalize(),p=c-n)}h.push(u)}else if(1===n){if(t.c(a.edges[u]),f.c(g).sub(v[u]),n=z(t,f),-1===n){n=f.j();
if(n>c)return h.push(g),h.push(t),h.push(f),!1;b&&(b.bInA=!1,r=f.normalize(),p=c-n)}}else{u=t.m().normalize();n=f.f(u);q=Math.abs(n);if(0<n&&q>c)return h.push(g),h.push(u),h.push(f),!1;b&&(r=u,p=c-n,0<=n||p<2*c)&&(b.bInA=!1)}r&&b&&Math.abs(p)<Math.abs(b.overlap)&&(b.overlap=p,b.overlapN.c(r))}b&&(b.a=a,b.b=d,b.overlapV.c(b.overlapN).scale(b.overlap));h.push(g);h.push(t);h.push(f);return!0}function E(a,d,b){for(var c=a.calcPoints,k=c.length,l=d.calcPoints,f=l.length,e=0;e<k;e++)if(y(a.pos,d.pos,c,
l,a.normals[e],b))return!1;for(e=0;e<f;e++)if(y(a.pos,d.pos,c,l,d.normals[e],b))return!1;b&&(b.a=a,b.b=d,b.overlapV.c(b.overlapN).scale(b.overlap));return!0}var f={};f.Vector=c;f.V=c;c.prototype.copy=c.prototype.c=function(a){this.x=a.x;this.y=a.y;return this};c.prototype.clone=c.prototype.clone=function(){return new c(this.x,this.y)};c.prototype.perp=c.prototype.m=function(){var a=this.x;this.x=this.y;this.y=-a;return this};c.prototype.rotate=c.prototype.rotate=function(a){var d=this.x,b=this.y;
this.x=d*Math.cos(a)-b*Math.sin(a);this.y=d*Math.sin(a)+b*Math.cos(a);return this};c.prototype.reverse=c.prototype.reverse=function(){this.x=-this.x;this.y=-this.y;return this};c.prototype.normalize=c.prototype.normalize=function(){var a=this.j();0<a&&(this.x/=a,this.y/=a);return this};c.prototype.add=c.prototype.add=function(a){this.x+=a.x;this.y+=a.y;return this};c.prototype.sub=c.prototype.sub=function(a){this.x-=a.x;this.y-=a.y;return this};c.prototype.scale=c.prototype.scale=function(a,d){this.x*=
a;this.y*=d||a;return this};c.prototype.project=c.prototype.o=function(a){var d=this.f(a)/a.g();this.x=d*a.x;this.y=d*a.y;return this};c.prototype.projectN=c.prototype.s=function(a){var d=this.f(a);this.x=d*a.x;this.y=d*a.y;return this};c.prototype.reflect=function(a){var d=this.x,b=this.y;this.o(a).scale(2);this.x-=d;this.y-=b;return this};c.prototype.reflectN=function(a){var d=this.x,b=this.y;this.s(a).scale(2);this.x-=d;this.y-=b;return this};c.prototype.dot=c.prototype.f=function(a){return this.x*
a.x+this.y*a.y};c.prototype.len2=c.prototype.g=function(){return this.f(this)};c.prototype.len=c.prototype.j=function(){return Math.sqrt(this.g())};f.Circle=B;B.prototype.getAABB=function(){var a=this.r,d=this.pos.clone().sub(new c(a,a));return(new p(d,2*a,2*a)).l()};f.Polygon=m;m.prototype.setPoints=m.prototype.u=function(a){if(!this.points||this.points.length!==a.length){var d,b=this.calcPoints=[],g=this.edges=[],k=this.normals=[];for(d=0;d<a.length;d++)b.push(new c),g.push(new c),k.push(new c)}this.points=
a;this.i()};m.prototype.setAngle=function(a){this.angle=a;this.i();return this};m.prototype.setOffset=function(a){this.offset=a;this.i();return this};m.prototype.rotate=m.prototype.rotate=function(a){for(var d=this.points,b=d.length,c=0;c<b;c++)d[c].rotate(a);this.i();return this};m.prototype.translate=m.prototype.translate=function(a,d){for(var b=this.points,c=b.length,k=0;k<c;k++)b[k].x+=a,b[k].y+=d;this.i();return this};m.prototype.i=function(){var a=this.calcPoints,c=this.edges,b=this.normals,
g=this.points,k=this.offset,l=this.angle,f=g.length,e;for(e=0;e<f;e++){var h=a[e].c(g[e]);h.x+=k.x;h.y+=k.y;0!==l&&h.rotate(l)}for(e=0;e<f;e++)g=a[e],g=c[e].c(e<f-1?a[e+1]:a[0]).sub(g),b[e].c(g).m().normalize()};m.prototype.getAABB=function(){for(var a=this.calcPoints,d=a.length,b=a[0].x,g=a[0].y,k=a[0].x,l=a[0].y,f=1;f<d;f++){var e=a[f];e.x<b?b=e.x:e.x>k&&(k=e.x);e.y<g?g=e.y:e.y>l&&(l=e.y)}return(new p(this.pos.clone().add(new c(b,g)),k-b,l-g)).l()};m.prototype.getCentroid=function(){for(var a=this.calcPoints,
d=a.length,b=0,g=0,k=0,f=0;f<d;f++){var h=a[f],e=f===d-1?a[0]:a[f+1],m=h.x*e.y-e.x*h.y;b+=(h.x+e.x)*m;g+=(h.y+e.y)*m;k+=m}k*=3;return new c(b/k,g/k)};f.Box=p;p.prototype.toPolygon=p.prototype.l=function(){var a=this.pos,d=this.w,b=this.h;return new m(new c(a.x,a.y),[new c,new c(d,0),new c(d,b),new c(0,b)])};f.Response=w;w.prototype.clear=w.prototype.clear=function(){this.bInA=this.aInB=!0;this.overlap=Number.MAX_VALUE;return this};for(var h=[],r=0;10>r;r++)h.push(new c);var q=[];for(r=0;5>r;r++)q.push([]);
var A=new w,F=(new p(new c,1E-6,1E-6)).l();f.isSeparatingAxis=y;f.pointInCircle=function(a,c){var b=h.pop().c(a).sub(c.pos),d=c.r*c.r,f=b.g();h.push(b);return f<=d};f.pointInPolygon=function(a,c){F.pos.c(a);A.clear();var b=E(F,c,A);b&&(b=A.aInB);return b};f.testCircleCircle=function(a,c,b){var d=h.pop().c(c.pos).sub(a.pos),f=a.r+c.r,l=d.g();if(l>f*f)return h.push(d),!1;b&&(l=Math.sqrt(l),b.a=a,b.b=c,b.overlap=f-l,b.overlapN.c(d.normalize()),b.overlapV.c(d).scale(b.overlap),b.aInB=a.r<=c.r&&l<=c.r-
a.r,b.bInA=c.r<=a.r&&l<=a.r-c.r);h.push(d);return!0};f.testPolygonCircle=D;f.testCirclePolygon=function(a,c,b){if((a=D(c,a,b))&&b){c=b.a;var d=b.aInB;b.overlapN.reverse();b.overlapV.reverse();b.a=b.b;b.b=c;b.aInB=b.bInA;b.bInA=d}return a};f.testPolygonPolygon=E;return f}"function"===typeof define&&define.amd?define(x):"object"===typeof exports?module.exports=x():this.SAT=x();
var SAT = require('..');
var assert = require('assert');
describe("collision of", function(){
it("two circles", function(){
describe("Polygon.getCentroid", function() {
it("should calculate the correct value for a square", function() {
var V = SAT.Vector;
var P = SAT.Polygon;
// 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 c = polygon.getCentroid();
assert( c.x === 20 );
assert( c.y === 20 );
});
it("should calculate the correct value for a triangle", function() {
var V = SAT.Vector;
var P = SAT.Polygon;
// A triangle
var polygon = new P(new V(0,0), [
new V(0,0), new V(100,0), new V(50,99)
]);
var c = polygon.getCentroid();
assert( c.x === 50 );
assert( c.y === 33 );
});
});
describe("Collision", function() {
it("testCircleCircle", function() {
var V = SAT.Vector;
var C = SAT.Circle;

@@ -19,3 +47,3 @@

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

@@ -29,3 +57,3 @@ var V = SAT.Vector;

var polygon = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
]);

@@ -43,3 +71,3 @@ var response = new SAT.Response();

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

@@ -50,7 +78,7 @@ var P = SAT.Polygon;

var polygon1 = new P(new V(0,0), [
new V(0,0), new V(40,0), new V(40,40), new V(0,40)
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)
new V(0,0), new V(30, 0), new V(0, 30)
]);

@@ -66,4 +94,4 @@ var response = new SAT.Response();

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

@@ -78,4 +106,4 @@ var B = SAT.Box;

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

@@ -89,3 +117,4 @@ var C = SAT.Circle;

});
it("a polygon", function(){
it("pointInPolygon", function() {
var V = SAT.Vector;

@@ -96,3 +125,3 @@ var C = SAT.Circle;

var triangle = new P(new V(30,0), [
new V(0,0), new V(30, 0), new V(0, 30)
new V(0,0), new V(30, 0), new V(0, 30)
]);

@@ -102,3 +131,4 @@ assert(!SAT.pointInPolygon(new V(0,0), triangle)); // false

});
it("a small polygon", function () {
it("pointInPolygon (small)", function () {
var V = SAT.Vector;

@@ -105,0 +135,0 @@ var C = SAT.Circle;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc