Skip to content

Commit 4bc806b

Browse files
committed
tryGetBody fix
1 parent 0c60c1a commit 4bc806b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/zphysics.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,22 @@ pub inline fn isValidBodyPointer(body: *const Body) bool {
5858
/// Use `PhysicsSystem.getBodies()` to get all the bodies.
5959
/// NOTE: This function is *not* protected by a lock, use with care!
6060
pub inline fn tryGetBody(all_bodies: []const *const Body, body_id: BodyId) ?*const Body {
61-
const body = all_bodies[body_id & body_id_index_bits];
61+
const index = body_id.indexBits();
62+
if (index >= all_bodies.len) {
63+
return null;
64+
}
65+
const body = all_bodies[index];
6266
return if (isValidBodyPointer(body) and body.id == body_id) body else null;
6367
}
6468
/// Access a body, will return a `null` if the `body_id` is no longer valid.
6569
/// Use `PhysicsSystem.getBodiesMut()` to get all the bodies.
6670
/// NOTE: This function is *not* protected by a lock, use with care!
6771
pub inline fn tryGetBodyMut(all_bodies: []const *Body, body_id: BodyId) ?*Body {
68-
const body = all_bodies[body_id & body_id_index_bits];
72+
const index = body_id.indexBits();
73+
if (index >= all_bodies.len) {
74+
return null;
75+
}
76+
const body = all_bodies[index];
6977
return if (isValidBodyPointer(body) and body.id == body_id) body else null;
7078
}
7179

0 commit comments

Comments
 (0)