Skip to content

perf: Only test polygon edges inside the hitbox overlap and drop allocations from segment intersection - #4

Merged
adario merged 1 commit into
feat/path-shapesfrom
perf/narrow-phase-overlap-path
Sep 13, 2026
Merged

adario merged 1 commit into
feat/path-shapesfrom
perf/narrow-phase-overlap-path

Conversation

@spydon

@spydon spydon commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The same narrow phase changes as flame-engine#4045, rebased on feat/path-shapes so the path contour hitboxes benefit from them right away. None of it depends on the path work, so this can also just be picked up by merging main once the upstream PR lands.

Only test the edges inside the overlapping area. ShapeHitbox.intersections now passes the overlap of the two AABBs, inflated by the existing 0.01 tolerance, to the intersection systems. They already knew how to filter edges by a rect through possibleIntersectionVertices but never received one.

Allocation-free segment intersection with a bounding box early out. LineSegment.intersections rejects pairs whose bounding boxes do not overlap before doing any math and no longer allocates Line objects or result lists for misses.

Measured with the benchmark from #3 on this branch, milliseconds per 20 ticks in JIT, 50 hitboxes of each kind:

Scene Before After
path vs circle 17.5 10.2
path vs rectangle 12.4 6.8
path vs polygon 16.0 5.9
path vs path 92.0 11.4
polygon vs circle 6.7 5.4
polygon vs rectangle 6.2 5.1
polygon vs polygon 10.3 5.9

The path hitbox is the flame logo sampled at granularity 1.0, 37 vertices at 60 pixels, and the polygon is the same logo hand-written with its 10 anchor points. #5 is stacked on this PR and continues from these numbers.

Note that classifies a concave polygon with multiple crossings correctly in polygon_ray_intersection_test.dart fails on feat/path-shapes before this change as well: it expects the old one-crossing heuristic while the branch switched to odd parity.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • [-] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

…cations from segment intersection

ShapeHitbox.intersections now passes the overlap of the two AABBs to the intersection systems, which already knew how to filter edges by a rect but never received one. LineSegment.intersections rejects segment pairs whose bounding boxes do not overlap before doing any math and no longer allocates Line objects or result lists for misses.
@adario

adario commented Sep 13, 2026

Copy link
Copy Markdown
Owner

Love this!
LGTM

@adario
adario merged commit ee2be95 into feat/path-shapes Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants