From 6e520fdbb93b893ae194918e7430182dc478d91f Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Wed, 26 Mar 2025 20:10:01 +0100 Subject: [PATCH] Restore collision optimization --- packages/element/src/collision.ts | 79 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/packages/element/src/collision.ts b/packages/element/src/collision.ts index cfd7cdb04a..0fabe98392 100644 --- a/packages/element/src/collision.ts +++ b/packages/element/src/collision.ts @@ -212,28 +212,28 @@ const intersectRectanguloidWithLineSegment = ( const [sides, corners] = deconstructRectanguloidElement(element, offset); return ( - [ - // Test intersection against the sides, keep only the valid - // intersection points and rotate them back to scene space - ...sides - .map((s) => - lineSegmentIntersectionPoints( - lineSegment(rotatedA, rotatedB), - s, - ), - ) - .filter((x) => x != null) - .map((j) => pointRotateRads(j!, center, element.angle)), + // Test intersection against the sides, keep only the valid + // intersection points and rotate them back to scene space + sides + .map((s) => + lineSegmentIntersectionPoints( + lineSegment(rotatedA, rotatedB), + s, + ), + ) + .filter((x) => x != null) + .map((j) => pointRotateRads(j!, center, element.angle)) // Test intersection against the corners which are cubic bezier curves, // keep only the valid intersection points and rotate them back to scene // space - ...corners - .flatMap((t) => - curveIntersectLineSegment(t, lineSegment(rotatedA, rotatedB)), - ) - .filter((i) => i != null) - .map((j) => pointRotateRads(j, center, element.angle)), - ] + .concat( + corners + .flatMap((t) => + curveIntersectLineSegment(t, lineSegment(rotatedA, rotatedB)), + ) + .filter((i) => i != null) + .map((j) => pointRotateRads(j, center, element.angle)), + ) // Remove duplicates .filter( (p, idx, points) => points.findIndex((d) => pointsEqual(p, d)) === idx, @@ -266,29 +266,28 @@ const intersectDiamondWithLineSegment = ( const [sides, curves] = deconstructDiamondElement(element, offset); return ( - [ - ...sides - .map((s) => - lineSegmentIntersectionPoints( - lineSegment(rotatedA, rotatedB), - s, - ), - ) - .filter((p): p is GlobalPoint => p != null) - // Rotate back intersection points - .map((p) => pointRotateRads(p!, center, element.angle)), - ...curves - .flatMap((p) => - curveIntersectLineSegment(p, lineSegment(rotatedA, rotatedB)), - ) - .filter((p) => p != null) - // Rotate back intersection points - .map((p) => pointRotateRads(p, center, element.angle)), - ] + sides + .map((s) => + lineSegmentIntersectionPoints( + lineSegment(rotatedA, rotatedB), + s, + ), + ) + .filter((p): p is GlobalPoint => p != null) + // Rotate back intersection points + .map((p) => pointRotateRads(p!, center, element.angle)) + .concat( + curves + .flatMap((p) => + curveIntersectLineSegment(p, lineSegment(rotatedA, rotatedB)), + ) + .filter((p) => p != null) + // Rotate back intersection points + .map((p) => pointRotateRads(p, center, element.angle)), + ) // Remove duplicates .filter( - (p, idx, points) => - points.findIndex((d) => pointsEqual(p, d, 1e-3)) === idx, + (p, idx, points) => points.findIndex((d) => pointsEqual(p, d)) === idx, ) ); };