Skip to content

Update getBodiesUnsafe() to return a slice of size getMaxBodies()#25

Open
r0ckHopper wants to merge 6 commits into
zig-gamedev:mainfrom
r0ckHopper:main
Open

Update getBodiesUnsafe() to return a slice of size getMaxBodies()#25
r0ckHopper wants to merge 6 commits into
zig-gamedev:mainfrom
r0ckHopper:main

Conversation

@r0ckHopper

Copy link
Copy Markdown

Currently getBodiesUnsafe() returns a slice of size getNumBodies() but getBodiesUnsafe() actually returns a pointer to slice of size max_bodies.

The issue comes when you delete bodies from the middle and try to access bodies of higher index

getBodiesUnsafe does not reorganize the elements before giving a slice so you can get an out of bounds error

example of the issue is:

let bodies be
[b0, b1, b2]

if you delete b1 jolt physics updates bodies to
[b0, invalid, b2]

but if you call getBodiesUnsafe you only get
[b0, invalid]

so if you do tryGetBody( all_bodies, b2) then it will look for b2 at index 2 which is out of bounds

getBodiesUnsafe() returned ptr[0..getNumBodies()] but its values can go
upto 1024 (getMaxBodies)
After destroying bodies, the active count shrinks but remaining bodies stay at the same slots, causing out-of-bounds exceptions.
The underlying Jolt array also uses max_bodies, so slicing to getMaxBodies() is safe.
Same issue as addressed in commit 77a907d
getBodiesMutUnsafe() returned ptr[0..getNumBodies()] but its values can go
upto 1024 (getMaxBodies)
After destroying bodies, the active count shrinks but remaining bodies stay at the same slots, causing out-of-bounds exceptions.
The underlying Jolt array also uses max_bodies, so slicing to getMaxBodies() is safe.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an API mismatch where PhysicsSystem.getBodiesUnsafe() / getBodiesMutUnsafe() returned a slice sized to getNumBodies() even though the underlying Jolt buffer is sized to max_bodies, which could cause out-of-bounds indexing when accessing bodies by BodyId after deletions create holes.

Changes:

  • Return a getMaxBodies()-sized slice from getBodiesUnsafe() and getBodiesMutUnsafe().
  • Add a regression test covering the “destroy middle body then index by BodyId” scenario.

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

Comment thread src/zphysics.zig
Comment thread src/zphysics.zig
Comment thread src/zphysics.zig
Comment thread src/zphysics.zig
Comment thread src/zphysics.zig Outdated
@r0ckHopper

r0ckHopper commented Jun 7, 2026

Copy link
Copy Markdown
Author

@hazeycode I think my response to the comment will need human input.
Zig doesn't have a clean try catch for panic and copilot's suggestion would not be testing the issue. Current options are to
1.Keep test as it is and test only happy path
2.Create a subprocess and check exit code (this might be a bit overkill for this project's test code)
3.Delete the test

@hazeycode

Copy link
Copy Markdown
Member

Yeah it's fine as it is for now.

@r0ckHopper

Copy link
Copy Markdown
Author

Updates the the doc comments as well. Should be good to merge now unless you wanna tweak the wording.

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.

3 participants