mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 15:12:18 +00:00
Added Intersection functions and PointsAABB.
This commit is contained in:
parent
b5b86595d9
commit
5805adf214
5 changed files with 105 additions and 13 deletions
|
@ -1,36 +1,31 @@
|
||||||
var CircleVsRectangle = function (circle, rectangle)
|
var CircleVsRectangle = function (circle, rect)
|
||||||
{
|
{
|
||||||
var halfWidth = rectangle.width / 2;
|
var halfWidth = rect.width / 2;
|
||||||
var halfHeight = rectangle.height / 2;
|
var halfHeight = rect.height / 2;
|
||||||
|
|
||||||
var cx = Math.abs(circle.x - rectangle.x - halfWidth);
|
var cx = Math.abs(circle.x - rect.x - halfWidth);
|
||||||
var xDist = halfWidth + circle.radius;
|
var xDist = halfWidth + circle.radius;
|
||||||
|
|
||||||
if (cx > xDist)
|
if (cx <= halfWidth || cx > xDist)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cy = Math.abs(circle.y - rectangle.y - halfHeight);
|
var cy = Math.abs(circle.y - rect.y - halfHeight);
|
||||||
var yDist = halfHeight + circle.radius;
|
var yDist = halfHeight + circle.radius;
|
||||||
|
|
||||||
if (cy > yDist)
|
if (cy <= halfHeight || cy > yDist)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cx <= halfWidth || cy <= halfHeight)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var xCornerDist = cx - halfWidth;
|
var xCornerDist = cx - halfWidth;
|
||||||
var yCornerDist = cy - halfHeight;
|
var yCornerDist = cy - halfHeight;
|
||||||
var xCornerDistSq = xCornerDist * xCornerDist;
|
var xCornerDistSq = xCornerDist * xCornerDist;
|
||||||
var yCornerDistSq = yCornerDist * yCornerDist;
|
var yCornerDistSq = yCornerDist * yCornerDist;
|
||||||
var maxCornerDistSq = circle.radius * circle.radius;
|
var maxCornerDistSq = circle.radius * circle.radius;
|
||||||
|
|
||||||
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
|
return (xCornerDistSq + yCornerDistSq <= maxCornerDistSq);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = CircleVsRectangle;
|
module.exports = CircleVsRectangle;
|
||||||
|
|
19
v3/src/geom/intersects/GetRectangleIntersection.js
Normal file
19
v3/src/geom/intersects/GetRectangleIntersection.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
var Rectangle = require('../rectangle/Rectangle');
|
||||||
|
var RectangleVsRectangle = require('./RectangleVsRectangle');
|
||||||
|
|
||||||
|
var GetRectangleIntersection = function (rectA, rectB, output)
|
||||||
|
{
|
||||||
|
if (output === undefined) { output = new Rectangle(); }
|
||||||
|
|
||||||
|
if (RectangleVsRectangle(rectA, rectB))
|
||||||
|
{
|
||||||
|
output.x = Math.max(rectA.x, rectB.x);
|
||||||
|
output.y = Math.max(rectA.y, rectB.y);
|
||||||
|
output.width = Math.min(rectA.right, rectB.right) - output.x;
|
||||||
|
output.height = Math.min(rectA.bottom, rectB.bottom) - output.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = GetRectangleIntersection;
|
11
v3/src/geom/intersects/RectangleVsRectangle.js
Normal file
11
v3/src/geom/intersects/RectangleVsRectangle.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
var RectangleVsRectangle = function (rectA, rectB)
|
||||||
|
{
|
||||||
|
if (rectA.width <= 0 || rectA.height <= 0 || rectB.width <= 0 || rectB.height <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !(rectA.right < rectB.x || rectA.bottom < rectB.y || rectA.x > rectB.right || rectA.y > rectB.bottom);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = RectangleVsRectangle;
|
13
v3/src/geom/intersects/RectangleVsValues.js
Normal file
13
v3/src/geom/intersects/RectangleVsValues.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
var RectangleVsValues = function (rect, left, right, top, bottom, tolerance)
|
||||||
|
{
|
||||||
|
if (tolerance === undefined) { tolerance = 0; }
|
||||||
|
|
||||||
|
return !(
|
||||||
|
left > rect.right + tolerance ||
|
||||||
|
right < rect.left - tolerance ||
|
||||||
|
top > rect.bottom + tolerance ||
|
||||||
|
bottom < rect.top - tolerance
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = RectangleVsValues;
|
54
v3/src/geom/point/GetRectangleFromPoints.js
Normal file
54
v3/src/geom/point/GetRectangleFromPoints.js
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
var Rectangle = require('../rectangle/Rectangle');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
|
||||||
|
*
|
||||||
|
* @method Phaser.Rectangle#aabb
|
||||||
|
* @param {Phaser.Point[]} points - The array of one or more points.
|
||||||
|
* @param {Phaser.Rectangle} [out] - Optional Rectangle to store the value in, if not supplied a new Rectangle object will be created.
|
||||||
|
* @return {Phaser.Rectangle} The new Rectangle object.
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
var GetRectangleFromPoints = function (points, out)
|
||||||
|
{
|
||||||
|
if (out === undefined) { out = new Rectangle(); }
|
||||||
|
|
||||||
|
var xMax = Number.NEGATIVE_INFINITY;
|
||||||
|
var xMin = Number.POSITIVE_INFINITY;
|
||||||
|
var yMax = Number.NEGATIVE_INFINITY;
|
||||||
|
var yMin = Number.POSITIVE_INFINITY;
|
||||||
|
|
||||||
|
for (var i = 0; i < points.length; i++)
|
||||||
|
{
|
||||||
|
var point = points[i];
|
||||||
|
|
||||||
|
if (point.x > xMax)
|
||||||
|
{
|
||||||
|
xMax = point.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (point.x < xMin)
|
||||||
|
{
|
||||||
|
xMin = point.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (point.y > yMax)
|
||||||
|
{
|
||||||
|
yMax = point.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (point.y < yMin)
|
||||||
|
{
|
||||||
|
yMin = point.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out.x = xMin;
|
||||||
|
out.y = yMin;
|
||||||
|
out.width = xMax - xMin;
|
||||||
|
out.height = yMax - yMin;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = GetRectangleFromPoints;
|
Loading…
Reference in a new issue