Skip to content

fix(physics): clear cached collisions when a collision component is removed#8898

Merged
willeastcott merged 1 commit into
mainfrom
fix/5797-triggerleave-scene-reload
Jun 14, 2026
Merged

fix(physics): clear cached collisions when a collision component is removed#8898
willeastcott merged 1 commit into
mainfrom
fix/5797-triggerleave-scene-reload

Conversation

@willeastcott

Copy link
Copy Markdown
Contributor

Description

The rigid-body collisions map is keyed by entity GUID and was never cleared when an entity is destroyed. Reloading the same scene recreates entities with identical GUIDs (from resource_id) within a single task — no physics step runs in between — so the stale entry, still referencing the destroyed entity, was reused by the new same-GUID entity.

From then on triggerleave / collisionend never fired, because _cleanOldCollisions decides which leave event to fire by inspecting the cached (now destroyed) entity, whose trigger / body had already been deleted on destroy.

This clears the entity's cached collision on collision beforeremove. Every physics body entity has a collision component, so this single hook covers both triggers and rigid bodies. Added a regression test.

Fixes #5797

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

…emoved

The rigid-body `collisions` map is keyed by entity GUID and was never cleared on entity destroy. Reloading the same scene recreates entities with identical GUIDs in a single task, so the stale entry (still referencing the destroyed entity) was reused, and triggerleave/collisionend stopped firing because `_cleanOldCollisions` inspects the cached, now-destroyed entity.

Clear the entity's entry on collision `beforeremove`; every physics body entity has a collision component, so one hook covers triggers and rigid bodies.

Fixes #5797

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@willeastcott

Copy link
Copy Markdown
Contributor Author

Credit to @defektu, who diagnosed the root cause in the issue thread — that reloading the same scene recreates entities with the same GUIDs and leaves stale entries in the rigid-body collision tracking ("a flaw with collision objects' others list… not propagating").

This PR fixes that at the source (clearing the cached collision on beforeremove) rather than via the changeScene timing change proposed there. That keeps it robust for loadSceneHierarchy and programmatic destroy/recreate too, and avoids regressing changeScene's behavior of applying scene settings and keeping the old scene visible until the new data is ready.

@willeastcott willeastcott merged commit 6261a49 into main Jun 14, 2026
9 checks passed
@willeastcott willeastcott deleted the fix/5797-triggerleave-scene-reload branch June 14, 2026 16:36
@willeastcott willeastcott added the area: physics Physics related issue label Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: physics Physics related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The triggerleave event doesn't work as expected after reloading scene

1 participant