Skip to content

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

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

Update getBodiesUnsafe() to return a slice of size getMaxBodies()#25
r0ckHopper wants to merge 3 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.
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.

1 participant