Skip to content

Commit 06c92f9

Browse files
committed
Geom.Intersects.GetRaysFromPointToPolygon is a new function that emits rays out from the given point and detects for intersection against all given polygons, returning the points of intersection in the results array.
1 parent ceb9666 commit 06c92f9

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Vector4 = require('../../math/Vector4');
8+
var GetLineToPolygon = require('./GetLineToPolygon');
9+
var Line = require('../line/Line');
10+
11+
// Temp calculation segment
12+
var segment = new Line();
13+
14+
function CheckIntersects (angle, x, y, polygons, intersects)
15+
{
16+
var dx = Math.cos(angle);
17+
var dy = Math.sin(angle);
18+
19+
segment.setTo(x, y, x + dx, y + dy);
20+
21+
var closestIntersect = GetLineToPolygon(segment, polygons);
22+
23+
if (closestIntersect)
24+
{
25+
intersects.push(new Vector4(closestIntersect.x, closestIntersect.y, angle, closestIntersect.w));
26+
}
27+
}
28+
29+
function SortIntersects (a, b)
30+
{
31+
return a.z - b.z;
32+
}
33+
34+
/**
35+
* Projects rays out from the given point to each line segment of the polygons.
36+
*
37+
* If the rays intersect with the polygons, the points of intersection are returned in an array.
38+
*
39+
* If no intersections are found, the returned array will be empty.
40+
*
41+
* Each Vector4 intersection result has the following properties:
42+
*
43+
* The `x` and `y` components contain the point of the intersection.
44+
* The `z` component contains the angle of intersection.
45+
* The `w` component contains the index of the polygon, in the given array, that triggered the intersection.
46+
*
47+
* @function Phaser.Geom.Intersects.GetRaysFromPointToPolygon
48+
* @since 3.50.0
49+
*
50+
* @param {number} x - The x coordinate to project the rays from.
51+
* @param {number} y - The y coordinate to project the rays from.
52+
* @param {Phaser.Geom.Polygon | Phaser.Geom.Polygon[]} polygons - A single polygon, or array of polygons, to check against the rays.
53+
*
54+
* @return {Phaser.Math.Vector4[]} An array containing all intersections in Vector4s.
55+
*/
56+
var GetRaysFromPointToPolygon = function (x, y, polygons)
57+
{
58+
if (!Array.isArray(polygons))
59+
{
60+
polygons = [ polygons ];
61+
}
62+
63+
var intersects = [];
64+
65+
for (var i = 0; i < polygons.length; i++)
66+
{
67+
var points = polygons[i].points;
68+
69+
for (var p = 0; p < points.length; p++)
70+
{
71+
var angle = Math.atan2(points[p].y - y, points[p].x - x);
72+
73+
// +- 0.00001 rads to catch lines behind segment corners
74+
CheckIntersects(angle, x, y, polygons, intersects);
75+
CheckIntersects(angle - 0.00001, x, y, polygons, intersects);
76+
CheckIntersects(angle + 0.00001, x, y, polygons, intersects);
77+
}
78+
}
79+
80+
return intersects.sort(SortIntersects);
81+
};
82+
83+
module.exports = GetRaysFromPointToPolygon;

src/geom/intersects/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
GetLineToLine: require('./GetLineToLine'),
1919
GetLineToPolygon: require('./GetLineToPolygon'),
2020
GetLineToRectangle: require('./GetLineToRectangle'),
21+
GetRaysFromPointToPolygon: require('./GetRaysFromPointToPolygon'),
2122
GetRectangleIntersection: require('./GetRectangleIntersection'),
2223
GetRectangleToRectangle: require('./GetRectangleToRectangle'),
2324
GetRectangleToTriangle: require('./GetRectangleToTriangle'),

0 commit comments

Comments
 (0)