UNPKG

arcade-physics

Version:
61 lines 2.35 kB
"use strict"; /** * @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 Vector3_1 = __importDefault(require("../../math/Vector3")); const GetLineToLine_1 = __importDefault(require("./GetLineToLine")); const Line_1 = __importDefault(require("../line/Line")); // Temp calculation segment const segment = new Line_1.default(); // Temp vec3 const tempIntersect = new Vector3_1.default(); /** * Checks for the closest point of intersection between a line segment and an array of points, where each pair * of points are converted to line segments for the intersection tests. * * If no intersection is found, this function returns `null`. * * If intersection was found, a Vector3 is returned with the following properties: * * The `x` and `y` components contain the point of the intersection. * The `z` component contains the closest distance. * * @function Phaser.Geom.Intersects.GetLineToPoints * @since 3.50.0 * * @param {Phaser.Geom.Line} line - The line segment to check. * @param {Phaser.Math.Vector2[] | Phaser.Geom.Point[]} points - An array of points to check. * @param {Phaser.Math.Vector3} [out] - A Vector3 to store the intersection results in. * * @return {Phaser.Math.Vector3} A Vector3 containing the intersection results, or `null`. */ const GetLineToPoints = (line, points, out) => { if (out === undefined) { out = new Vector3_1.default(); } let closestIntersect = false; // Reset our vec3s out.set(); tempIntersect.set(); let prev = points[0]; for (let i = 1; i < points.length; i++) { const current = points[i]; segment.setTo(prev.x, prev.y, current.x, current.y); prev = current; if ((0, GetLineToLine_1.default)(line, segment, tempIntersect)) { if (!closestIntersect || tempIntersect.z < out.z) { out.copy(tempIntersect); closestIntersect = true; } } } return closestIntersect ? out : null; }; exports.default = GetLineToPoints; //# sourceMappingURL=GetLineToPoints.js.map