mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 05:33:35 +00:00
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.
This commit is contained in:
parent
ceb96665b8
commit
06c92f9af8
2 changed files with 84 additions and 0 deletions
83
src/geom/intersects/GetRaysFromPointToPolygon.js
Normal file
83
src/geom/intersects/GetRaysFromPointToPolygon.js
Normal file
|
@ -0,0 +1,83 @@
|
|||
/**
|
||||
* @author Richard Davey
|
||||
* @copyright 2020 Photon Storm Ltd.
|
||||
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
||||
*/
|
||||
|
||||
var Vector4 = require('../../math/Vector4');
|
||||
var GetLineToPolygon = require('./GetLineToPolygon');
|
||||
var Line = require('../line/Line');
|
||||
|
||||
// Temp calculation segment
|
||||
var segment = new Line();
|
||||
|
||||
function CheckIntersects (angle, x, y, polygons, intersects)
|
||||
{
|
||||
var dx = Math.cos(angle);
|
||||
var dy = Math.sin(angle);
|
||||
|
||||
segment.setTo(x, y, x + dx, y + dy);
|
||||
|
||||
var closestIntersect = GetLineToPolygon(segment, polygons);
|
||||
|
||||
if (closestIntersect)
|
||||
{
|
||||
intersects.push(new Vector4(closestIntersect.x, closestIntersect.y, angle, closestIntersect.w));
|
||||
}
|
||||
}
|
||||
|
||||
function SortIntersects (a, b)
|
||||
{
|
||||
return a.z - b.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects rays out from the given point to each line segment of the polygons.
|
||||
*
|
||||
* If the rays intersect with the polygons, the points of intersection are returned in an array.
|
||||
*
|
||||
* If no intersections are found, the returned array will be empty.
|
||||
*
|
||||
* Each Vector4 intersection result has the following properties:
|
||||
*
|
||||
* The `x` and `y` components contain the point of the intersection.
|
||||
* The `z` component contains the angle of intersection.
|
||||
* The `w` component contains the index of the polygon, in the given array, that triggered the intersection.
|
||||
*
|
||||
* @function Phaser.Geom.Intersects.GetRaysFromPointToPolygon
|
||||
* @since 3.50.0
|
||||
*
|
||||
* @param {number} x - The x coordinate to project the rays from.
|
||||
* @param {number} y - The y coordinate to project the rays from.
|
||||
* @param {Phaser.Geom.Polygon | Phaser.Geom.Polygon[]} polygons - A single polygon, or array of polygons, to check against the rays.
|
||||
*
|
||||
* @return {Phaser.Math.Vector4[]} An array containing all intersections in Vector4s.
|
||||
*/
|
||||
var GetRaysFromPointToPolygon = function (x, y, polygons)
|
||||
{
|
||||
if (!Array.isArray(polygons))
|
||||
{
|
||||
polygons = [ polygons ];
|
||||
}
|
||||
|
||||
var intersects = [];
|
||||
|
||||
for (var i = 0; i < polygons.length; i++)
|
||||
{
|
||||
var points = polygons[i].points;
|
||||
|
||||
for (var p = 0; p < points.length; p++)
|
||||
{
|
||||
var angle = Math.atan2(points[p].y - y, points[p].x - x);
|
||||
|
||||
// +- 0.00001 rads to catch lines behind segment corners
|
||||
CheckIntersects(angle, x, y, polygons, intersects);
|
||||
CheckIntersects(angle - 0.00001, x, y, polygons, intersects);
|
||||
CheckIntersects(angle + 0.00001, x, y, polygons, intersects);
|
||||
}
|
||||
}
|
||||
|
||||
return intersects.sort(SortIntersects);
|
||||
};
|
||||
|
||||
module.exports = GetRaysFromPointToPolygon;
|
|
@ -18,6 +18,7 @@ module.exports = {
|
|||
GetLineToLine: require('./GetLineToLine'),
|
||||
GetLineToPolygon: require('./GetLineToPolygon'),
|
||||
GetLineToRectangle: require('./GetLineToRectangle'),
|
||||
GetRaysFromPointToPolygon: require('./GetRaysFromPointToPolygon'),
|
||||
GetRectangleIntersection: require('./GetRectangleIntersection'),
|
||||
GetRectangleToRectangle: require('./GetRectangleToRectangle'),
|
||||
GetRectangleToTriangle: require('./GetRectangleToTriangle'),
|
||||
|
|
Loading…
Reference in a new issue