UNPKG

arcade-physics

Version:
66 lines 2.91 kB
"use strict"; /** * @author Richard Davey <rich@photonstorm.com> * @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 ContainsArray_1 = __importDefault(require("../triangle/ContainsArray")); const Decompose_1 = __importDefault(require("../triangle/Decompose")); const LineToLine_1 = __importDefault(require("./LineToLine")); /** * Checks if two Triangles intersect. * * A Triangle intersects another Triangle if any pair of their lines intersects or if any point of one Triangle is within the other Triangle. Thus, the Triangles are considered "solid". * * @function Phaser.Geom.Intersects.TriangleToTriangle * @since 3.0.0 * * @param {Phaser.Geom.Triangle} triangleA - The first Triangle to check for intersection. * @param {Phaser.Geom.Triangle} triangleB - The second Triangle to check for intersection. * * @return {boolean} `true` if the Triangles intersect, otherwise `false`. */ const TriangleToTriangle = (triangleA, triangleB) => { // First the cheapest ones: if (triangleA.left > triangleB.right || triangleA.right < triangleB.left || triangleA.top > triangleB.bottom || triangleA.bottom < triangleB.top) { return false; } const lineAA = triangleA.getLineA(); const lineAB = triangleA.getLineB(); const lineAC = triangleA.getLineC(); const lineBA = triangleB.getLineA(); const lineBB = triangleB.getLineB(); const lineBC = triangleB.getLineC(); // Now check the lines against each line of TriangleB if ((0, LineToLine_1.default)(lineAA, lineBA) || (0, LineToLine_1.default)(lineAA, lineBB) || (0, LineToLine_1.default)(lineAA, lineBC)) { return true; } if ((0, LineToLine_1.default)(lineAB, lineBA) || (0, LineToLine_1.default)(lineAB, lineBB) || (0, LineToLine_1.default)(lineAB, lineBC)) { return true; } if ((0, LineToLine_1.default)(lineAC, lineBA) || (0, LineToLine_1.default)(lineAC, lineBB) || (0, LineToLine_1.default)(lineAC, lineBC)) { return true; } // Nope, so check to see if any of the points of triangleA are within triangleB let points = (0, Decompose_1.default)(triangleA); let within = (0, ContainsArray_1.default)(triangleB, points, true); if (within.length > 0) { return true; } // Finally check to see if any of the points of triangleB are within triangleA points = (0, Decompose_1.default)(triangleB); within = (0, ContainsArray_1.default)(triangleA, points, true); if (within.length > 0) { return true; } return false; }; exports.default = TriangleToTriangle; //# sourceMappingURL=TriangleToTriangle.js.map