Box3D's native C17 engine, statically linked into a standalone Bevy ECS plugin.
It depends on the crates.io Bevy 0.20.0 sub-crates, so its components are
the same types as an application's bevy = "0.20.0". An application built on a
local Bevy checkout must [patch.crates-io] these crates to that checkout.
Requires Rust, a C17 compiler (MSVC Build Tools on Windows, GCC/Clang on Unix),
and the sibling ../box3d source checkout. Cargo compiles the native library;
CMake, bindgen, libclang, sample dependencies, and runtime DLLs are unnecessary.
Set BOX3D_SOURCE_DIR to an absolute path to use a different Box3D checkout
compatible with this adapter. Box3D is built in single precision.
cargo test --manifest-path bevy_box3d/Cargo.toml
cargo run --manifest-path bevy_box3d/Cargo.toml --example falling_boxIn an application using Bevy 0.20:
[dependencies]
bevy_box3d = { path = "../crates/bevy_box3d" }use bevy::prelude::*;
use bevy_box3d::{Box3dPlugin, Collider, RigidBody};
fn main() {
App::new()
.add_plugins((DefaultPlugins, Box3dPlugin))
.insert_resource(Time::<Fixed>::from_hz(60.0))
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn((
RigidBody::Static,
Collider::Cuboid { half_extents: Vec3::new(10.0, 0.5, 10.0) },
Transform::from_xyz(0.0, -0.5, 0.0),
));
commands.spawn((
RigidBody::Dynamic,
Collider::Cuboid { half_extents: Vec3::splat(0.5) },
Transform::from_xyz(0.0, 4.0, 0.0),
));
}RigidBodyrequiresTransform,Velocity, andPhysicsMaterial, inserting defaults when absent. A body is simulated once it also has aCollider.Box3dSettingscontrols gravity and solver substeps (default: 4). The step duration followsTime<Fixed>; the plugin preserves your existing timestep. A normal game must install Bevy'sTimePlugin, included inDefaultPluginsandMinimalPlugins. The example drivesFixedUpdatemanually.- Changes to position, rotation, velocity, body type, collider, or material
synchronize before each step. After stepping, only the bodies Box3D reports
as moved (awake, or just fallen asleep) are read back, so sleeping and static
bodies cost nothing and their
Transformis never marked changed. Direct transform edits teleport and wake the body. ExternalForcepersists each step.ExternalImpulseapplies once and is zeroed. Zero forces/impulses do not keep sleeping bodies awake.- Put gameplay input systems
.before(Box3dSet)and result readers.after(Box3dSet)withinFixedUpdate. - Removing an entity,
RigidBody,Collider, or a required component destroys its native body at the next fixed step. DroppingPhysicsWorldfrees the native world immediately. Replacing it rebuilds bodies on the next step. - Coordinates use meters, kilograms, seconds, and unit quaternions. Entities
must be unparented:
Transformis world space. Transform scale is visual only; set collider dimensions explicitly. Invalid input panics in Rust before entering the native engine. - Each entity supports one centered cuboid, sphere, Y-aligned capsule,
ConvexHull, orTriMesh. Build a hull or mesh once (ConvexHull::new,TriMesh::new) and clone it onto many bodies: clones share Box3D's processed data and compare by identity. Box3D only creates mesh contacts on static bodies. Both expose their planes or tree and triangles for renderers. Compound shapes, height fields, joints, sensors, collision events, spatial queries, interpolation, and debug rendering are not wrapped yet. - The native engine uses one worker. Native calls are serialized across worlds
because Box3D has shared global state. The only unsafe
Send/Syncimplementations are for hull and mesh data, which is immutable after creation; no native handles are user-visible.
ffi exposes the unsafe C adapter for low-level use. Its integer handles are
opaque; callers must manage lifetimes and serialize all native calls. Safe
applications should use the ECS interface. The adapter compiles against
Box3D's headers, keeping its definition structs and inline ID helpers in C.
This crate is not configured for crates.io publishing because it depends on
a local Box3D checkout. Box3D retains its upstream MIT license in
../box3d/LICENSE.