https://github.com/zig-gamedev/zbullet
Zig build package, bindings and C API for Bullet Physics SDK
https://github.com/zig-gamedev/zbullet
Last synced: 8 months ago
JSON representation
Zig build package, bindings and C API for Bullet Physics SDK
- Host: GitHub
- URL: https://github.com/zig-gamedev/zbullet
- Owner: zig-gamedev
- License: mit
- Created: 2024-11-03T19:58:10.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-19T22:27:26.000Z (8 months ago)
- Last Synced: 2025-03-19T23:28:26.807Z (8 months ago)
- Language: C++
- Size: 933 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [zbullet](https://github.com/zig-gamedev/zbullet)
Zig bindings for Bullet Physics SDK
## Overview
Bullet Physics SDK 3.25 (**C++**) ---> cbullet v0.2 (**C**) ---> zbullet (**Zig**)
`cbullet` is C API for Bullet Physics SDK which is being developed as a part of zig-gamedev project ([source code](https://github.com/zig-gamedev/zbullet/tree/main/libs/cbullet)).
`zbullet` is built on top of `cbullet` and provides Zig friendly bindings.
## Features
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)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/bullet_physics_test_wgpu)
* [virtual physics lab](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/bullet_physics_test) (Windows-only, uses `cbullet` directly)
* [zbullet tests](https://github.com/zig-gamedev/zbullet/blob/main/src/zbullet.zig)
## Getting started
Example `build.zig`:
```zig
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{ ... });
const zbullet = b.dependency("zbullet", .{});
exe.root_module.addImport("zbullet", zbullet.module("root"));
exe.linkLibrary(zbullet.artifact("cbullet"));
}
```
Now in your code you may import and use zbullet:
```zig
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, .{});
...
}
}
```