arcade-physics
Version:
Use Arcade Physics without Phaser.
78 lines • 3.08 kB
JavaScript
;
/**
* @author Richard Davey
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Vector4_1 = __importDefault(require("../../math/Vector4"));
const GetLineToPolygon_1 = __importDefault(require("./GetLineToPolygon"));
const Line_1 = __importDefault(require("../line/Line"));
// Temp calculation segment
const segment = new Line_1.default();
/**
* @ignore
*/
function CheckIntersects(angle, x, y, polygons, intersects) {
const dx = Math.cos(angle);
const dy = Math.sin(angle);
segment.setTo(x, y, x + dx, y + dy);
const closestIntersect = (0, GetLineToPolygon_1.default)(segment, polygons);
if (closestIntersect) {
intersects.push(new Vector4_1.default(closestIntersect.x, closestIntersect.y, angle, closestIntersect.w));
}
}
/**
* @ignore
*/
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.
*/
const GetRaysFromPointToPolygon = (x, y, polygons) => {
if (!Array.isArray(polygons)) {
polygons = [polygons];
}
const intersects = [];
const angles = [];
for (let i = 0; i < polygons.length; i++) {
const points = polygons[i].points;
for (let p = 0; p < points.length; p++) {
const angle = Math.atan2(points[p].y - y, points[p].x - x);
if (angles.indexOf(angle) === -1) {
// +- 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);
angles.push(angle);
}
}
}
return intersects.sort(SortIntersects);
};
exports.default = GetRaysFromPointToPolygon;
//# sourceMappingURL=GetRaysFromPointToPolygon.js.map