Skip to content

Commit ee92d1d

Browse files
committed
Geom.Intersects.GetLineToPolygon is a new function that checks for the closest point of intersection between a line segment and an array of polygons.
1 parent 771509d commit ee92d1d

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @author Richard Davey
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Vector3 = require('../../math/Vector3');
8+
var Vector4 = require('../../math/Vector4');
9+
var GetLineToLine = require('./GetLineToLine');
10+
var Line = require('../line/Line');
11+
12+
// Temp calculation segment
13+
var segment = new Line();
14+
15+
// Temp vec3
16+
var tempIntersect = new Vector3();
17+
18+
/**
19+
* Checks for the closest point of intersection between a line segment and an array of polygons.
20+
*
21+
* If no intersection is found, this function returns `null`.
22+
*
23+
* If intersection was found, a Vector4 is returned with the following properties:
24+
*
25+
* The `x` and `y` components contain the point of the intersection.
26+
* The `z` component contains the closest distance.
27+
* The `w` component contains the index of the polygon, in the given array, that triggered the intersection.
28+
*
29+
* @function Phaser.Geom.Intersects.GetLineToPolygon
30+
* @since 3.25.0
31+
*
32+
* @param {Phaser.Geom.Line} line - The line segment to check.
33+
* @param {Phaser.Geom.Polygon | Phaser.Geom.Polygon[]} polygons - A single polygon, or array of polygons, to check.
34+
* @param {Phaser.Math.Vector4} [out] - A Vector4 to store the intersection results in.
35+
*
36+
* @return {Phaser.Math.Vector4} A Vector4 containing the intersection results, or `null`.
37+
*/
38+
var GetLineToPolygon = function (line, polygons, out)
39+
{
40+
if (out === undefined)
41+
{
42+
out = new Vector4();
43+
}
44+
45+
if (!Array.isArray(polygons))
46+
{
47+
polygons = [ polygons ];
48+
}
49+
50+
var closestIntersect = false;
51+
52+
// Reset our temporary vec4
53+
tempIntersect.set();
54+
55+
for (var p = 0; p < polygons.length; p++)
56+
{
57+
var points = polygons[p].points;
58+
59+
var prev = points[0];
60+
61+
for (var i = 1; i < points.length; i++)
62+
{
63+
var current = points[i];
64+
65+
segment.setTo(prev.x, prev.y, current.x, current.y);
66+
67+
prev = current;
68+
69+
if (!GetLineToLine(line, segment, tempIntersect))
70+
{
71+
// No intersection? Carry on ...
72+
continue;
73+
}
74+
75+
if (!closestIntersect || tempIntersect.z < out.z)
76+
{
77+
out.set(tempIntersect.x, tempIntersect.y, tempIntersect.z, p);
78+
79+
closestIntersect = true;
80+
}
81+
}
82+
}
83+
84+
return (closestIntersect) ? out : null;
85+
};
86+
87+
module.exports = GetLineToPolygon;

src/geom/intersects/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
GetCircleToRectangle: require('./GetCircleToRectangle'),
1717
GetLineToCircle: require('./GetLineToCircle'),
1818
GetLineToLine: require('./GetLineToLine'),
19+
GetLineToPolygon: require('./GetLineToPolygon'),
1920
GetLineToRectangle: require('./GetLineToRectangle'),
2021
GetRectangleIntersection: require('./GetRectangleIntersection'),
2122
GetRectangleToRectangle: require('./GetRectangleToRectangle'),

0 commit comments

Comments
 (0)