diff --git a/v3/src/geom/intersects/CircleVsCircle.js b/v3/src/geom/intersects/CircleToCircle.js similarity index 69% rename from v3/src/geom/intersects/CircleVsCircle.js rename to v3/src/geom/intersects/CircleToCircle.js index 63149a50f..303c2ad6e 100644 --- a/v3/src/geom/intersects/CircleVsCircle.js +++ b/v3/src/geom/intersects/CircleToCircle.js @@ -1,8 +1,8 @@ var DistanceBetween = require('../../math/distance/DistanceBetween'); -var CircleVsCircle = function (circleA, circleB) +var CircleToCircle = function (circleA, circleB) { return (DistanceBetween(circleA.x, circleA.y, circleB.x, circleB.y) <= (circleA.radius + circleB.radius)); }; -module.exports = CircleVsCircle; +module.exports = CircleToCircle; diff --git a/v3/src/geom/intersects/CircleVsRectangle.js b/v3/src/geom/intersects/CircleToRectangle.js similarity index 89% rename from v3/src/geom/intersects/CircleVsRectangle.js rename to v3/src/geom/intersects/CircleToRectangle.js index 985c0a740..5be3d8ceb 100644 --- a/v3/src/geom/intersects/CircleVsRectangle.js +++ b/v3/src/geom/intersects/CircleToRectangle.js @@ -1,4 +1,4 @@ -var CircleVsRectangle = function (circle, rect) +var CircleToRectangle = function (circle, rect) { var halfWidth = rect.width / 2; var halfHeight = rect.height / 2; @@ -28,4 +28,4 @@ var CircleVsRectangle = function (circle, rect) return (xCornerDistSq + yCornerDistSq <= maxCornerDistSq); }; -module.exports = CircleVsRectangle; +module.exports = CircleToRectangle; diff --git a/v3/src/geom/intersects/LineToLine.js b/v3/src/geom/intersects/LineToLine.js new file mode 100644 index 000000000..7c47ca4f2 --- /dev/null +++ b/v3/src/geom/intersects/LineToLine.js @@ -0,0 +1,49 @@ +var Point = require('../point/Point'); + +var LineToLine = function (line1, line2, asSegment, out) +{ + if (asSegment === undefined) { asSegment = true; } + if (out === undefined) { out = new Point(); } + + var a = line1.x1; + var b = line1.y1; + + var e = line1.x2; + var f = line1.y2; + + var a1 = b.y - a.y; + var a2 = f.y - e.y; + var b1 = a.x - b.x; + var b2 = e.x - f.x; + var c1 = (b.x * a.y) - (a.x * b.y); + var c2 = (f.x * e.y) - (e.x * f.y); + var denom = (a1 * b2) - (a2 * b1); + + if (denom === 0) + { + return null; + } + + out.x = ((b1 * c2) - (b2 * c1)) / denom; + out.y = ((a2 * c1) - (a1 * c2)) / denom; + + if (asSegment) + { + var uc = ((f.y - e.y) * (b.x - a.x) - (f.x - e.x) * (b.y - a.y)); + var ua = (((f.x - e.x) * (a.y - e.y)) - (f.y - e.y) * (a.x - e.x)) / uc; + var ub = (((b.x - a.x) * (a.y - e.y)) - ((b.y - a.y) * (a.x - e.x))) / uc; + + if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) + { + return out; + } + else + { + return null; + } + } + + return out; +}; + +module.exports = LineToLine; diff --git a/v3/src/geom/intersects/LineToRectangle.js b/v3/src/geom/intersects/LineToRectangle.js new file mode 100644 index 000000000..e28179c78 --- /dev/null +++ b/v3/src/geom/intersects/LineToRectangle.js @@ -0,0 +1,86 @@ +/** +* Checks for intersection between the Line and a Rectangle shape, or a rectangle-like +* object, with public `x`, `y`, `right` and `bottom` properties, such as a Sprite or Body. +* +* An intersection is considered valid if: +* +* The line starts within, or ends within, the Rectangle. +* The line segment intersects one of the 4 rectangle edges. +* +* The for the purposes of this function rectangles are considered 'solid'. +* +* @method Phaser.Line.intersectsRectangle +* @param {Phaser.Line} line - The line to check for intersection with. +* @param {Phaser.Rectangle|object} rect - The rectangle, or rectangle-like object, to check for intersection with. +* @return {boolean} True if the line intersects with the rectangle edges, or starts or ends within the rectangle. +*/ +var LineToRectangle = function (line, rect) +{ + var x1 = line.x1; + var y1 = line.y2; + + var x2 = line.x2; + var y2 = line.y2; + + var bx1 = rect.x; + var by1 = rect.y; + var bx2 = rect.right; + var by2 = rect.bottom; + + var t = 0; + + // If the start or end of the line is inside the rect then we assume + // collision, as rects are solid for our use-case. + + if ((x1 >= bx1 && x1 <= bx2 && y1 >= by1 && y1 <= by2) || + (x2 >= bx1 && x2 <= bx2 && y2 >= by1 && y2 <= by2)) + { + return true; + } + + if (x1 < bx1 && x2 >= bx1) + { + // Left edge + t = y1 + (y2 - y1) * (bx1 - x1) / (x2 - x1); + + if (t > by1 && t <= by2) + { + return true; + } + } + else if (x1 > bx2 && x2 <= bx2) + { + // Right edge + t = y1 + (y2 - y1) * (bx2 - x1) / (x2 - x1); + + if (t >= by1 && t <= by2) + { + return true; + } + } + + if (y1 < by1 && y2 >= by1) + { + // Top edge + t = x1 + (x2 - x1) * (by1 - y1) / (y2 - y1); + + if (t >= bx1 && t <= bx2) + { + return true; + } + } + else if (y1 > by2 && y2 <= by2) + { + // Bottom edge + t = x1 + (x2 - x1) * (by2 - y1) / (y2 - y1); + + if (t >= bx1 && t <= bx2) + { + return true; + } + } + + return false; +}; + +module.exports = LineToRectangle; diff --git a/v3/src/geom/intersects/RectangleVsRectangle.js b/v3/src/geom/intersects/RectangleToRectangle.js similarity index 72% rename from v3/src/geom/intersects/RectangleVsRectangle.js rename to v3/src/geom/intersects/RectangleToRectangle.js index c5690fc4d..f66cc40a8 100644 --- a/v3/src/geom/intersects/RectangleVsRectangle.js +++ b/v3/src/geom/intersects/RectangleToRectangle.js @@ -1,4 +1,4 @@ -var RectangleVsRectangle = function (rectA, rectB) +var RectangleToRectangle = function (rectA, rectB) { if (rectA.width <= 0 || rectA.height <= 0 || rectB.width <= 0 || rectB.height <= 0) { @@ -8,4 +8,4 @@ var RectangleVsRectangle = function (rectA, rectB) return !(rectA.right < rectB.x || rectA.bottom < rectB.y || rectA.x > rectB.right || rectA.y > rectB.bottom); }; -module.exports = RectangleVsRectangle; +module.exports = RectangleToRectangle; diff --git a/v3/src/geom/intersects/RectangleVsValues.js b/v3/src/geom/intersects/RectangleToValues.js similarity index 71% rename from v3/src/geom/intersects/RectangleVsValues.js rename to v3/src/geom/intersects/RectangleToValues.js index df194b5d8..0a03e1cb1 100644 --- a/v3/src/geom/intersects/RectangleVsValues.js +++ b/v3/src/geom/intersects/RectangleToValues.js @@ -1,4 +1,4 @@ -var RectangleVsValues = function (rect, left, right, top, bottom, tolerance) +var RectangleToValues = function (rect, left, right, top, bottom, tolerance) { if (tolerance === undefined) { tolerance = 0; } @@ -10,4 +10,4 @@ var RectangleVsValues = function (rect, left, right, top, bottom, tolerance) ); }; -module.exports = RectangleVsValues; +module.exports = RectangleToValues;