fix(physics2d): stop spurious TriggerEnd for sustained sensor overlaps after rebuild
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 onTriggerEnd) silently desynchronizes after every snapshot restore /Plugin.Resetperformed while a player stands in a trigger volume: the overlap's end transition is consumed while the entity is still inside, and no compensatingTriggerBeginfires until the entity physically leaves and re-enters the sensor. - Source: Bug report (autonomous run). Root cause traced to
diffActiveContactsAfterRebuildinpkg/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 +
createContactassert), so persisted sensor pairs are structurally absent from that list and were always spuriously ended. The fix re-derives the live sensor overlap set fromWorld.ShapeSensorDataand checks sensor pairs against that set, so a sustained overlap produces no event while a genuinely ended one still emitsTriggerEnd. - 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 →
TriggerEndstill 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 realStep+FlushBufferedContactspipeline 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 oneTriggerEnd; a third test pins that new (unsuppressed-baseline) sensor overlaps still do not emit aTriggerBegin; two solid-path tests confirm no regression. All pass. As a negative control, reverting only theIsSensorbranch back to the oldliveContacts-only lookup makes the sustained test fail with a spuriousTriggerEnd (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, includingTestPhysics2D_CardinalIntegration(crash1/crash2 syntheticTriggerEndassertions — still correct since the ball is genuinely moved out of the sensor) andTestGoldenTrace(incl.sensor_trigger). - E2e scenario suite (
pkg/plugin/physics2d/test/e2e/) passes, includingTestScenarios/reset("the rebuild invents no TriggerEnd"),TestRestore,TestExhaustiveBodyMatrix,TestDigestIsWorkerInvariant, andTestRandomScenes. - 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:
gofmtclean,go vetclean,go build ./pkg/...andclibuild 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
IsSensorflag. - Solid contact path unchanged; added unit tests covering sustained and gone sensor overlaps after rebuild.