Bullet Physics SDK 3.25 (C++) ---> cbullet v0.2 (C) ---> zbullet v0.2 (Zig)
cbullet
is C API for Bullet Physics SDK which is being developed as a part of zig-gamedev project (source code).
zbullet
is built on top of cbullet
and provides Zig friendly bindings.
Some cbullet
features:
- Most collision shapes
- Rigid bodies
- Most constraint types
- Tries to minimize number of memory allocations
- Multiple rigid bodies and motion states can be created with one memory allocation
- New physics objects can re-use existing memory
- Lots of error checks in debug builds
For an example code please see:
- bullet physics test (wgpu)
- intro 6 (Windows-only)
- virtual physics lab (Windows-only, uses
cbullet
directly) - zbullet tests
Copy zbullet
folder to a libs
subdirectory of the root of your project.
Then in your build.zig
add:
const std = @import("std");
const zbullet = @import("libs/zbullet/build.zig");
pub fn build(b: *std.Build) void {
...
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const zbullet_pkg = zbullet.package(b, target, optimize, .{});
zbullet_pkg.link(exe);
}
Now in your code you may import and use zbullet:
const zbt = @import("zbullet");
pub fn main() !void {
...
zbt.init(allocator);
defer zbt.deinit();
const world = zbt.initWorld();
defer world.deinit();
// Create unit cube shape.
const box_shape = zbt.initBoxShape(&.{ 0.5, 0.5, 0.5 });
defer box_shape.deinit();
// Create rigid body that will use above shape.
const initial_transform = [_]f32{
1.0, 0.0, 0.0, // orientation
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
2.0, 2.0, 2.0, // translation
};
const box_body = zbt.initBody(
1.0, // mass (0.0 for static objects)
&initial_transform,
box_shape.asShape(),
);
defer body.deinit();
// Add body to the physics world.
world.addBody(box_body);
defer world.removeBody(box_body);
while (...) {
...
// Perform a simulation step.
_ = world.stepSimulation(time_step, .{});
...
}
}