Comparing version 0.8.0 to 0.9.0
# Changelog | ||
## 0.9.0 (April 4, 2021) | ||
- Add `getAABBAsBox` methods to `Polygon` and `Circle` that returns a `Box` (`getAABB` returns a `Polygon`) - thanks [getkey](https://github.com/getkey)! | ||
## 0.8.0 (Sept 7, 2019) | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "sat", | ||
"description": "Library for performing 2D collision detection", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"author": "Jim Riecken <jriecken@gmail.com>", | ||
@@ -26,2 +26,2 @@ "keywords": [ | ||
} | ||
} | ||
} |
@@ -19,3 +19,3 @@ # SAT.js | ||
Current version: `0.8.0`. | ||
Current version: `0.9.0`. | ||
@@ -79,2 +79,4 @@ Nicely compresses with the [Google Closure Compiler](https://developers.google.com/closure/compiler/) in **Advanced** mode to about 6KB (2KB gzipped) | ||
- `setOffset(offset)` - Set the current offset | ||
- `getAABB()` - Compute the axis-aligned bounding box. Returns a new Polygon every time it is called. | ||
- `getAABBAsBox()` - Compute the axis-aligned bounding box. Returns a new Box every time it is called. | ||
@@ -119,2 +121,3 @@ ### SAT.Polygon | ||
- `getAABB()` - Compute the axis-aligned bounding box. Returns a new Polygon every time it is called. Is performed based on the `calcPoints`. | ||
- `getAABBAsBox()` - Compute the axis-aligned bounding box. Returns a new Box every time it is called. Is performed based on the `calcPoints`. | ||
- `getCentroid()` - Compute the [Centroid](https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon) of the polygon. Is performed based on the `calcPoints`. | ||
@@ -121,0 +124,0 @@ |
152
SAT.js
@@ -1,2 +0,2 @@ | ||
// Version 0.8.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> | ||
// Version 0.9.0 - Copyright 2012 - 2021 - Jim Riecken <jimr@jimr.ca> | ||
// | ||
@@ -7,3 +7,3 @@ // Released under the MIT License - https://github.com/jriecken/sat-js | ||
// polygons using the Separating Axis Theorem. | ||
/** @preserve SAT.js - Version 0.8.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */ | ||
/** @preserve SAT.js - Version 0.9.0 - Copyright 2012 - 2021 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */ | ||
@@ -68,3 +68,3 @@ /*global define: false, module: false*/ | ||
*/ | ||
Vector.prototype['copy'] = Vector.prototype.copy = function(other) { | ||
Vector.prototype['copy'] = Vector.prototype.copy = function (other) { | ||
this['x'] = other['x']; | ||
@@ -79,3 +79,3 @@ this['y'] = other['y']; | ||
*/ | ||
Vector.prototype['clone'] = Vector.prototype.clone = function() { | ||
Vector.prototype['clone'] = Vector.prototype.clone = function () { | ||
return new Vector(this['x'], this['y']); | ||
@@ -89,3 +89,3 @@ }; | ||
*/ | ||
Vector.prototype['perp'] = Vector.prototype.perp = function() { | ||
Vector.prototype['perp'] = Vector.prototype.perp = function () { | ||
var x = this['x']; | ||
@@ -114,3 +114,3 @@ this['x'] = this['y']; | ||
*/ | ||
Vector.prototype['reverse'] = Vector.prototype.reverse = function() { | ||
Vector.prototype['reverse'] = Vector.prototype.reverse = function () { | ||
this['x'] = -this['x']; | ||
@@ -126,5 +126,5 @@ this['y'] = -this['y']; | ||
*/ | ||
Vector.prototype['normalize'] = Vector.prototype.normalize = function() { | ||
Vector.prototype['normalize'] = Vector.prototype.normalize = function () { | ||
var d = this.len(); | ||
if(d > 0) { | ||
if (d > 0) { | ||
this['x'] = this['x'] / d; | ||
@@ -141,3 +141,3 @@ this['y'] = this['y'] / d; | ||
*/ | ||
Vector.prototype['add'] = Vector.prototype.add = function(other) { | ||
Vector.prototype['add'] = Vector.prototype.add = function (other) { | ||
this['x'] += other['x']; | ||
@@ -153,3 +153,3 @@ this['y'] += other['y']; | ||
*/ | ||
Vector.prototype['sub'] = Vector.prototype.sub = function(other) { | ||
Vector.prototype['sub'] = Vector.prototype.sub = function (other) { | ||
this['x'] -= other['x']; | ||
@@ -168,3 +168,3 @@ this['y'] -= other['y']; | ||
*/ | ||
Vector.prototype['scale'] = Vector.prototype.scale = function(x,y) { | ||
Vector.prototype['scale'] = Vector.prototype.scale = function (x, y) { | ||
this['x'] *= x; | ||
@@ -180,3 +180,3 @@ this['y'] *= typeof y != 'undefined' ? y : x; | ||
*/ | ||
Vector.prototype['project'] = Vector.prototype.project = function(other) { | ||
Vector.prototype['project'] = Vector.prototype.project = function (other) { | ||
var amt = this.dot(other) / other.len2(); | ||
@@ -194,3 +194,3 @@ this['x'] = amt * other['x']; | ||
*/ | ||
Vector.prototype['projectN'] = Vector.prototype.projectN = function(other) { | ||
Vector.prototype['projectN'] = Vector.prototype.projectN = function (other) { | ||
var amt = this.dot(other); | ||
@@ -207,3 +207,3 @@ this['x'] = amt * other['x']; | ||
*/ | ||
Vector.prototype['reflect'] = Vector.prototype.reflect = function(axis) { | ||
Vector.prototype['reflect'] = Vector.prototype.reflect = function (axis) { | ||
var x = this['x']; | ||
@@ -223,3 +223,3 @@ var y = this['y']; | ||
*/ | ||
Vector.prototype['reflectN'] = Vector.prototype.reflectN = function(axis) { | ||
Vector.prototype['reflectN'] = Vector.prototype.reflectN = function (axis) { | ||
var x = this['x']; | ||
@@ -238,3 +238,3 @@ var y = this['y']; | ||
*/ | ||
Vector.prototype['dot'] = Vector.prototype.dot = function(other) { | ||
Vector.prototype['dot'] = Vector.prototype.dot = function (other) { | ||
return this['x'] * other['x'] + this['y'] * other['y']; | ||
@@ -247,3 +247,3 @@ }; | ||
*/ | ||
Vector.prototype['len2'] = Vector.prototype.len2 = function() { | ||
Vector.prototype['len2'] = Vector.prototype.len2 = function () { | ||
return this.dot(this); | ||
@@ -256,3 +256,3 @@ }; | ||
*/ | ||
Vector.prototype['len'] = Vector.prototype.len = function() { | ||
Vector.prototype['len'] = Vector.prototype.len = function () { | ||
return Math.sqrt(this.len2()); | ||
@@ -282,12 +282,22 @@ }; | ||
// | ||
// Note: Returns a _new_ `Polygon` each time you call this. | ||
// Note: Returns a _new_ `Box` each time you call this. | ||
/** | ||
* @return {Polygon} The AABB | ||
*/ | ||
Circle.prototype['getAABB'] = Circle.prototype.getAABB = function() { | ||
Circle.prototype['getAABBAsBox'] = Circle.prototype.getAABBAsBox = function () { | ||
var r = this['r']; | ||
var corner = this['pos'].clone().add(this['offset']).sub(new Vector(r, r)); | ||
return new Box(corner, r*2, r*2).toPolygon(); | ||
return new Box(corner, r * 2, r * 2); | ||
}; | ||
// Compute the axis-aligned bounding box (AABB) of this Circle. | ||
// | ||
// Note: Returns a _new_ `Polygon` each time you call this. | ||
/** | ||
* @return {Polygon} The AABB | ||
*/ | ||
Circle.prototype['getAABB'] = Circle.prototype.getAABB = function () { | ||
return this.getAABBAsBox().toPolygon(); | ||
}; | ||
// Set the current offset to apply to the radius. | ||
@@ -298,3 +308,3 @@ /** | ||
*/ | ||
Circle.prototype['setOffset'] = Circle.prototype.setOffset = function(offset) { | ||
Circle.prototype['setOffset'] = Circle.prototype.setOffset = function (offset) { | ||
this['offset'] = offset; | ||
@@ -342,3 +352,3 @@ return this; | ||
*/ | ||
Polygon.prototype['setPoints'] = Polygon.prototype.setPoints = function(points) { | ||
Polygon.prototype['setPoints'] = Polygon.prototype.setPoints = function (points) { | ||
// Only re-allocate if this is a new polygon or the number of points has changed. | ||
@@ -376,3 +386,3 @@ var lengthChanged = !this['points'] || this['points'].length !== points.length; | ||
*/ | ||
Polygon.prototype['setAngle'] = Polygon.prototype.setAngle = function(angle) { | ||
Polygon.prototype['setAngle'] = Polygon.prototype.setAngle = function (angle) { | ||
this['angle'] = angle; | ||
@@ -388,3 +398,3 @@ this._recalc(); | ||
*/ | ||
Polygon.prototype['setOffset'] = Polygon.prototype.setOffset = function(offset) { | ||
Polygon.prototype['setOffset'] = Polygon.prototype.setOffset = function (offset) { | ||
this['offset'] = offset; | ||
@@ -402,3 +412,3 @@ this._recalc(); | ||
*/ | ||
Polygon.prototype['rotate'] = Polygon.prototype.rotate = function(angle) { | ||
Polygon.prototype['rotate'] = Polygon.prototype.rotate = function (angle) { | ||
var points = this['points']; | ||
@@ -429,4 +439,4 @@ var len = points.length; | ||
for (var i = 0; i < len; i++) { | ||
points[i]["x"] += x; | ||
points[i]["y"] += y; | ||
points[i]['x'] += x; | ||
points[i]['y'] += y; | ||
} | ||
@@ -443,3 +453,3 @@ this._recalc(); | ||
*/ | ||
Polygon.prototype._recalc = function() { | ||
Polygon.prototype._recalc = function () { | ||
// Calculated points - this is what is used for underlying collisions and takes into account | ||
@@ -464,4 +474,4 @@ // the angle/offset set on the polygon. | ||
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) { | ||
@@ -485,31 +495,43 @@ calcPoint.rotate(angle); | ||
// | ||
// Note: Returns a _new_ `Polygon` each time you call this. | ||
// Note: Returns a _new_ `Box` each time you call this. | ||
/** | ||
* @return {Polygon} The AABB | ||
*/ | ||
Polygon.prototype["getAABB"] = Polygon.prototype.getAABB = function() { | ||
var points = this["calcPoints"]; | ||
Polygon.prototype['getAABBAsBox'] = Polygon.prototype.getAABBAsBox = function () { | ||
var points = this['calcPoints']; | ||
var len = points.length; | ||
var xMin = points[0]["x"]; | ||
var yMin = points[0]["y"]; | ||
var xMax = points[0]["x"]; | ||
var yMax = points[0]["y"]; | ||
var xMin = points[0]['x']; | ||
var yMin = points[0]['y']; | ||
var xMax = points[0]['x']; | ||
var yMax = points[0]['y']; | ||
for (var i = 1; i < len; i++) { | ||
var point = points[i]; | ||
if (point["x"] < xMin) { | ||
xMin = point["x"]; | ||
if (point['x'] < xMin) { | ||
xMin = point['x']; | ||
} | ||
else if (point["x"] > xMax) { | ||
xMax = point["x"]; | ||
else if (point['x'] > xMax) { | ||
xMax = point['x']; | ||
} | ||
if (point["y"] < yMin) { | ||
yMin = point["y"]; | ||
if (point['y'] < yMin) { | ||
yMin = point['y']; | ||
} | ||
else if (point["y"] > yMax) { | ||
yMax = point["y"]; | ||
else if (point['y'] > yMax) { | ||
yMax = point['y']; | ||
} | ||
} | ||
return new Box(this['pos'].clone().add(new Vector(xMin, yMin)), xMax - xMin, yMax - yMin).toPolygon(); | ||
return new Box(this['pos'].clone().add(new Vector(xMin, yMin)), xMax - xMin, yMax - yMin); | ||
}; | ||
// Compute the axis-aligned bounding box. Any current state | ||
// (translations/rotations) will be applied before constructing the AABB. | ||
// | ||
// Note: Returns a _new_ `Polygon` each time you call this. | ||
/** | ||
* @return {Polygon} The AABB | ||
*/ | ||
Polygon.prototype['getAABB'] = Polygon.prototype.getAABB = function () { | ||
return this.getAABBAsBox().toPolygon(); | ||
}; | ||
// Compute the centroid (geometric center) of the polygon. Any current state | ||
@@ -524,4 +546,4 @@ // (translations/rotations) will be applied before computing the centroid. | ||
*/ | ||
Polygon.prototype["getCentroid"] = Polygon.prototype.getCentroid = function() { | ||
var points = this["calcPoints"]; | ||
Polygon.prototype['getCentroid'] = Polygon.prototype.getCentroid = function () { | ||
var points = this['calcPoints']; | ||
var len = points.length; | ||
@@ -533,6 +555,6 @@ var cx = 0; | ||
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; | ||
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; | ||
@@ -572,3 +594,3 @@ } | ||
*/ | ||
Box.prototype['toPolygon'] = Box.prototype.toPolygon = function() { | ||
Box.prototype['toPolygon'] = Box.prototype.toPolygon = function () { | ||
var pos = this['pos']; | ||
@@ -578,4 +600,4 @@ var w = this['w']; | ||
return new Polygon(new Vector(pos['x'], pos['y']), [ | ||
new Vector(), new Vector(w, 0), | ||
new Vector(w,h), new Vector(0,h) | ||
new Vector(), new Vector(w, 0), | ||
new Vector(w, h), new Vector(0, h) | ||
]); | ||
@@ -610,3 +632,3 @@ }; | ||
*/ | ||
Response.prototype['clear'] = Response.prototype.clear = function() { | ||
Response.prototype['clear'] = Response.prototype.clear = function () { | ||
this['aInB'] = true; | ||
@@ -664,3 +686,3 @@ this['bInA'] = true; | ||
var len = points.length; | ||
for (var i = 0; i < len; i++ ) { | ||
for (var i = 0; i < len; i++) { | ||
// The magnitude of the projection of the point onto the normal | ||
@@ -718,3 +740,3 @@ var dot = points[i].dot(normal); | ||
response['bInA'] = false; | ||
// B is fully inside A. Pick the shortest way out. | ||
// B is fully inside A. Pick the shortest way out. | ||
} else { | ||
@@ -725,3 +747,3 @@ var option1 = rangeA[1] - rangeB[0]; | ||
} | ||
// B starts further left than A | ||
// B starts further left than A | ||
} else { | ||
@@ -733,3 +755,3 @@ response['bInA'] = false; | ||
response['aInB'] = false; | ||
// A is fully inside B. Pick the shortest way out. | ||
// A is fully inside B. Pick the shortest way out. | ||
} else { | ||
@@ -860,3 +882,3 @@ var option1 = rangeA[1] - rangeB[0]; | ||
response['overlapV'].copy(differenceV).scale(response['overlap']); | ||
response['aInB']= a['r'] <= b['r'] && dist <= b['r'] - a['r']; | ||
response['aInB'] = a['r'] <= b['r'] && dist <= b['r'] - a['r']; | ||
response['bInA'] = b['r'] <= a['r'] && dist <= a['r'] - b['r']; | ||
@@ -933,3 +955,3 @@ } | ||
T_VECTORS.push(point2); | ||
// If it's the right region: | ||
// If it's the right region: | ||
} else if (region === RIGHT_VORONOI_REGION) { | ||
@@ -957,3 +979,3 @@ // We need to make sure we're in the left region on the next edge | ||
} | ||
// Otherwise, it's the middle region: | ||
// Otherwise, it's the middle region: | ||
} else { | ||
@@ -1056,3 +1078,3 @@ // Need to check if the circle is intersecting the edge, | ||
// If any of the edge normals of B is a separating axis, no intersection. | ||
for (var i = 0;i < bLen; i++) { | ||
for (var i = 0; i < bLen; i++) { | ||
if (isSeparatingAxis(a['pos'], b['pos'], aPoints, bPoints, b['normals'][i], response)) { | ||
@@ -1059,0 +1081,0 @@ return false; |
69684
1312
316