Skip to content

fix: Ray direction normalization drift issues#3841

Merged
spydon merged 9 commits intomainfrom
devkage/raytrace-reflection-normalization
Mar 5, 2026
Merged

fix: Ray direction normalization drift issues#3841
spydon merged 9 commits intomainfrom
devkage/raytrace-reflection-normalization

Conversation

@ufrshubham
Copy link
Member

Description

raytrace() reuses the same RaycastResult objects across bounces, so the reflected direction from one bounce becomes the incident direction for the next. Vector2.reflect() is not bit-exact, so each bounce introduces a tiny floating-point rounding error. After enough bounces the direction's length drifts just past the 1e-6 tolerance that Ray2 enforces, causing a direction must be normalized assertion crash.

Two fixes:

  • CircleHitbox and PolygonRayIntersection (used by RectangleHitbox / PolygonHitbox): call normalize() on the reflected direction before passing it to Ray2, so drift is corrected on every bounce.
  • raytrace_example: the initial ray direction was set via a cascade on the Vector2 getter (ray.direction..setValues(...)), which bypasses the direction= setter and leaves the cached inverse values stale. Fixed to assign through the setter.

Regression tests are included that inject a direction whose length² is already above the threshold (by writing directly to the underlying Vector2, bypassing the setter), then assert that raycast() does not throw.

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 ///.
  • [NA] 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.

Related Issues

@ufrshubham ufrshubham requested a review from a team March 4, 2026 17:43
@ufrshubham ufrshubham self-assigned this Mar 5, 2026
@ufrshubham ufrshubham requested a review from spydon March 5, 2026 08:39
@spydon spydon enabled auto-merge (squash) March 5, 2026 09:04
@spydon spydon merged commit b8e2bab into main Mar 5, 2026
68 checks passed
@spydon spydon deleted the devkage/raytrace-reflection-normalization branch March 5, 2026 12:18
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