fix(physics2d): stop spurious TriggerEnd for sustained sensor overlaps after rebuild

DE
12h ago
+250-6

Detail bug report: View on Detail

Closes PER-556

Premise

  • Problem: Game code that latches trigger state (e.g. an "in the zone" flag set on TriggerBegin, cleared on TriggerEnd) silently desynchronizes after every snapshot restore / Plugin.Reset performed while a player stands in a trigger volume: the overlap's end transition is consumed while the entity is still inside, and no compensating TriggerBegin fires until the entity physically leaves and re-enters the sensor.
  • Source: Bug report (autonomous run). Root cause traced to diffActiveContactsAfterRebuild in pkg/plugin/physics2d/internal/contact_flush.go, introduced in commit ae574a12 (PR #880).
  • Why this fix: The post-rebuild diff's "gone overlaps" End loop treated any persisted pair absent from the live solid contact list as ended. Box2D sensors never create contacts (broad-phase short-circuit + createContact assert), so persisted sensor pairs are structurally absent from that list and were always spuriously ended. The fix re-derives the live sensor overlap set from World.ShapeSensorData and checks sensor pairs against that set, so a sustained overlap produces no event while a genuinely ended one still emits TriggerEnd.
  • Alternatives considered: A minimal mitigation (skip sensor pairs in the End loop, keep them in the map) was noted in the report. It fixes the spurious End but misses legitimate ends while the world is torn down. The implemented query-based variant handles both cases and uses an API that already exists.
  • Product sign-off: Not a product decision.
  • Interview: Skipped — autonomous run.

Screenshot / Recordings

No visual behavior to capture. This is a physics event-correctness fix with no UI. Verification is via unit and integration tests (see below).

What changed

After a world rebuild (Plugin.Reset / snapshot restore), the physics plugin diffs the persisted ActiveContacts baseline against the live Box2D contact list to synthesize the Begin/End events that were suppressed during the rebuild step. That diff assumed every persisted pair appears in the live contact list — true for solid contacts, false for sensors (Box2D sensors never create contacts), so every still-overlapping sensor pair was emitted as a spurious TriggerEnd and deleted from the baseline.

diffActiveContactsAfterRebuild now gathers the live sensor overlap set separately from each sensor shape's overlap list (World.ShapeSensorCapacity / ShapeSensorData) and routes the End check for IsSensor pairs through that set:

  • Sustained sensor overlap → no event, pair retained (the bug, fixed)
  • Genuinely ended sensor overlap → TriggerEnd still emitted, pair removed (no over-correction)
  • New sensor overlaps during the suppressed step → still not emitted as Begins (existing one-shot suppression preserved)
  • Solid contact path → unchanged

Two reused scratch fields were added to Runtime and are dropped by Reset(), matching the existing scratch convention.

diffActiveContactsAfterRebuild(em):
  liveContacts = gatherLiveContactsInto()        // solid pairs only
  liveSensorOverlaps = gatherLiveSensorOverlapsInto()  // NEW: sensor pairs from ShapeSensorData
  FOR key, info IN ActiveContacts:
    live = info.IsSensor ? liveSensorOverlaps : liveContacts   // FIX
    IF key NOT IN live: emit End, delete

How did you verify?

  • New unit tests (5 tests in contact_flush_internal_test.go): drive the real Step + FlushBufferedContacts pipeline with a co-located zero-gravity sensor/visitor pair and a persisted baseline. The sustained-overlap test asserts zero events, pair retention, and 5 follow-up ticks stay silent; the gone-overlap test asserts exactly one TriggerEnd; a third test pins that new (unsuppressed-baseline) sensor overlaps still do not emit a TriggerBegin; two solid-path tests confirm no regression. All pass. As a negative control, reverting only the IsSensor branch back to the old liveContacts-only lookup makes the sustained test fail with a spurious TriggerEnd (A=1,B=2) while the other four stay green, confirming the test pins the exact bug.
  • Internal package suite passes, including TestRestoreIsDeterministicAcrossRepeatedRuns.
  • Plugin integration suite (pkg/plugin/physics2d/test/) passes, including TestPhysics2D_CardinalIntegration (crash1/crash2 synthetic TriggerEnd assertions — still correct since the ball is genuinely moved out of the sensor) and TestGoldenTrace (incl. sensor_trigger).
  • E2e scenario suite (pkg/plugin/physics2d/test/e2e/) passes, including TestScenarios/reset ("the rebuild invents no TriggerEnd"), TestRestore, TestExhaustiveBodyMatrix, TestDigestIsWorkerInvariant, and TestRandomScenes.
  • E2e CLI (407 checks), the crash-restore check (10 checks), and the worker-invariance digest (407 checks at 8 workers) all pass.
  • Lint, format, and build pass: gofmt clean, go vet clean, go build ./pkg/... and cli build clean, golangci-lint run ./pkg/plugin/physics2d/... reports 0 issues (golangci-lint 2.15.0, the pinned version).

Caveats

None known. The fix only adds a sensor-specific path to the post-rebuild End check; the solid contact path and the one-shot suppression of build-time overlaps are unchanged.

AI Usage

Created with Detail in opencode. Fully autonomous — the human supplied the bug report; implementation, test design, and verification were performed autonomously.


Automatic Fixes PRs can be configured here.


Summary by cubic

Fixes spurious TriggerEnd events after Plugin.Reset / snapshot restore for sensor overlaps that are still active, so game code latching trigger state stays in sync.

The post-rebuild diff treated any persisted pair absent from the live solid contact list as ended. Box2D sensors never create contacts, so every still-overlapping sensor pair was wrongly ended and dropped from the baseline. The diff now also derives the live sensor overlap set from each sensor shape's overlap list (World.ShapeSensorData) and ends a persisted pair only when it appears in neither live set; destroyed visitor shapes are skipped so their pairs emit TriggerEnd instead of resolving to a freed slot.

  • Sustained sensor overlaps stay silent; genuinely ended ones still emit exactly one TriggerEnd.
  • A pair persisted as sensor but rebuilt solid is retained so the next refresh flips its IsSensor flag.
  • Solid contact path unchanged; added unit tests covering sustained and gone sensor overlaps after rebuild.