Skip to content

refactor(collision): deduplicate rigid body / trigger rebuild#8886

Merged
willeastcott merged 2 commits into
mainfrom
refactor-collision-rebuild-dedup
Jun 12, 2026
Merged

refactor(collision): deduplicate rigid body / trigger rebuild#8886
willeastcott merged 2 commits into
mainfrom
refactor-collision-rebuild-dedup

Conversation

@willeastcott

Copy link
Copy Markdown
Contributor

Continues the component-consistency series (#8871, #8876, #8878-#8882). Sibling of #8883 and #8884.

Changes

The block that rebuilds the rigid body or re-creates the trigger after a collision shape change appeared nearly verbatim in two places:

  • CollisionSystemImpl.recreatePhysicalShapes
  • CollisionMeshSystemImpl.doRecreatePhysicalShape

This extracts the two bodies into _recreateBody(entity) and _recreateTrigger(entity, component) on the base CollisionSystemImpl, while deliberately keeping the branch conditions at each call site.

Why the gating stays at the call sites

The two call sites are not identical: the base implementation skips trigger creation for compound children (else if (!component._compoundParent)), while the mesh implementation creates the trigger unconditionally. That difference is observable behavior - a mesh collider can be a compound child, since _addEachDescendant assigns _compoundParent to any descendant collision without a rigidbody regardless of type. Extracting only the bodies preserves behavior by construction; the asymmetry is now called out with a comment on the mesh side.

Verified with npm test / npm run lint / npm run build / npm run build:types + npm run test:types. The base-implementation block sits behind the Ammo gate, so unit tests cannot reach it; the mesh trigger branch is exercised Ammo-less by the existing model tests.

🤖 Generated with Claude Code

The block that rebuilds the rigid body or re-creates the trigger
appeared nearly verbatim in CollisionSystemImpl.recreatePhysicalShapes
and CollisionMeshSystemImpl.doRecreatePhysicalShape. Extract the two
bodies into _recreateBody and _recreateTrigger on the base
implementation while keeping the branch conditions at each call site,
since they differ observably: the base path skips trigger creation for
compound children, the mesh path does not (a mesh collider can be a
compound child via _addEachDescendant). Behavior is preserved by
construction.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@LeXXik

LeXXik commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

I am not sure I see the value in this PR. Is it to reduce the code base? +25 lines vs -22.

@willeastcott willeastcott requested a review from Copilot June 12, 2026 11:49
@willeastcott willeastcott self-assigned this Jun 12, 2026
@willeastcott willeastcott added the enhancement Request for a new feature label Jun 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors collision shape rebuild logic to remove duplicated rigidbody/trigger recreation blocks across collision system implementations, while preserving behavioral differences in when triggers are created.

Changes:

  • Extracted rigidbody rebuild logic into CollisionSystemImpl._recreateBody(entity).
  • Extracted trigger create/re-init logic into CollisionSystemImpl._recreateTrigger(entity, component).
  • Updated both the base collision implementation and mesh collision implementation to use the shared helpers, with an explicit comment documenting the mesh-vs-base trigger gating difference.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@willeastcott

Copy link
Copy Markdown
Contributor Author

I am not sure I see the value in this PR. Is it to reduce the code base? +25 lines vs -22.

Fair question. The goal isn't line count. The two copies of this block had actually drifted: the base path skips trigger creation for compound children (!component._compoundParent), the mesh path doesn't. Before this change you'd only spot that by diffing two blocks ~400 lines apart, with no way to tell if it was intentional. Now the shared logic lives in one place and the only thing left at each call site is the difference, with a comment marking it as deliberate.

It also gives the disableSimulation → createBody → enableSimulation sequence a single home, so any future change to the rebuild ordering (there's a known latent bug in this area) gets made once instead of twice.

Happy to close it if you don't think that's worth the indirection, though

@mvaligursky

Copy link
Copy Markdown
Contributor

I usually de-duplicate to keep all paths consistent even if its just few lines. I'd merge.

@willeastcott willeastcott merged commit bee7da0 into main Jun 12, 2026
9 checks passed
@willeastcott willeastcott deleted the refactor-collision-rebuild-dedup branch June 12, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Request for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants