mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 21:24:09 +00:00
Renamed from Vs to To.
This commit is contained in:
parent
196f57c869
commit
425252627f
6 changed files with 143 additions and 8 deletions
|
@ -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;
|
|
@ -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;
|
49
v3/src/geom/intersects/LineToLine.js
Normal file
49
v3/src/geom/intersects/LineToLine.js
Normal file
|
@ -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;
|
86
v3/src/geom/intersects/LineToRectangle.js
Normal file
86
v3/src/geom/intersects/LineToRectangle.js
Normal file
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
Loading…
Reference in a new issue