Expand description
Simulation world: create bodies, shapes, and joints; step; query and cast.
Start here with World::new and World::step. Body/shape/joint creation
lives in crate::body, crate::shape, and crate::joint. Use
crate::types::default_world_def (and the other default_*_def helpers)
for C-compatible defaults.
use box3d_rust::types::default_world_def;
use box3d_rust::world::World;
let mut world = World::new(&default_world_def());
world.step(1.0 / 60.0, 1);Port of physics_world.h / physics_world.c (plus b3Profile from types.h).
Porting decisions (mirroring box2d-rust):
- C keeps a global world registry. The Rust
Worldis an owned object;world_id/generationare kept so body/shape/joint ids remain bit-compatible with C. - The arena allocator (
b3Stack) is per-step scratch; the Rust solver allocates scratch asVecs in the step, so there is no stack field. - Manifold block allocators become
Vec-owned manifolds on contacts for now; a pooled allocator can return later if profiling warrants it. - Scheduler/task-system fields are deferred with the single-threaded port;
worker_countis kept and clamped like C. - Hull database is a content-keyed
Rcstore (verstable map in C). - Pre-solve and custom-filter callbacks keep the C shape as
Option<fn>with au64context.
SPDX-FileCopyrightText: 2025 Erin Catto SPDX-License-Identifier: MIT
Structs§
- Profile
- Profiling data. Times are in milliseconds. (b3Profile)
- Task
Context - Per thread task storage. (b3TaskContext)
- World
- The world struct manages all physics entities, dynamic simulation, and asynchronous queries. (b3World)
Functions§
- default_
friction_ callback - Default friction mixing:
sqrt(frictionA * frictionB). (static b3DefaultFrictionCallback — from types.c / physics_world.c) - default_
restitution_ callback - Default restitution mixing:
max(restitutionA, restitutionB). - world_
cast_ mover - Cast a capsule mover through the world. Returns the earliest hit fraction in
[0, 1]. Overlapping shapes (fraction == 0) are ignored. An optional filter callback can reject individual shapes. (b3World_CastMover + static MoverCastCallback) - world_
cast_ ray - Cast a ray into the world. The callback receives
(shape_id, point, normal, fraction, user_material_id, triangle_index, child_index)and controls continuation like C’s b3CastResultFcn: return -1 to ignore, 0 to terminate, a fraction in 0..=1 to clip, or >1 to continue without clipping. (b3World_CastRay + static RayCastCallback) - world_
cast_ ray_ closest - Cast a ray into the world to collect the closest hit. Ignores initial overlap (fraction == 0 returns -1). (b3World_CastRayClosest)
- world_
cast_ shape - Cast a shape through the world. Callback contract matches
world_cast_ray. (b3World_CastShape + static ShapeCastCallback) - world_
collide_ mover - Collide a capsule mover with the world, gathering collision planes via callback.
The callback receives
(shape_id, planes)and returnsfalseto stop. (b3World_CollideMover + static TreeCollideCallback) - world_
draw - (
b3World_Draw) - world_
dump_ awake - Dump awake bodies (and touching statics) as a C++ recreation snippet to
box3d_dump.inl. (b3World_DumpAwake) - world_
dump_ shape_ bounds - Dump leaf AABB bounds for one broad-phase tree to
box3d_bounds.txt. (b3World_DumpShapeBounds) - world_
enable_ continuous - Enable/disable continuous collision. (b3World_EnableContinuous)
- world_
enable_ sleeping - Enable/disable sleep. (b3World_EnableSleeping)
- world_
enable_ speculative - Enable/disable speculative contacts. (b3World_EnableSpeculative)
- world_
enable_ warm_ starting - Enable/disable constraint warm starting. (b3World_EnableWarmStarting)
- world_
explode - Apply a radial explosion. (b3World_Explode + static ExplosionCallback)
- world_
get_ awake_ body_ count - Get the number of awake bodies. (b3World_GetAwakeBodyCount)
- world_
get_ body_ events - Get the body events for the current time step. The event data is transient. Do not store a reference to this data. (b3World_GetBodyEvents)
- world_
get_ bounds - Union of broad-phase tree root bounds. (b3World_GetBounds)
- world_
get_ contact_ events - Get contact events for this current time step. The event data is transient. Do not store a reference to this data. (b3World_GetContactEvents)
- world_
get_ contact_ recycle_ distance - Get the contact recycle distance. (b3World_GetContactRecycleDistance)
- world_
get_ counters - Get world counters and sizes. (b3World_GetCounters)
- world_
get_ gravity - Get the gravity vector. (b3World_GetGravity)
- world_
get_ hit_ event_ threshold - Get the hit event speed threshold. (b3World_GetHitEventThreshold)
- world_
get_ joint_ events - Get the joint events for the current time step. The event data is transient. Do not store a reference to this data. (b3World_GetJointEvents)
- world_
get_ max_ capacity - Get the maximum capacity the world has reached. (b3World_GetMaxCapacity)
- world_
get_ maximum_ linear_ speed - Get the maximum linear speed. (b3World_GetMaximumLinearSpeed)
- world_
get_ profile - Get the current world performance profile. (b3World_GetProfile)
- world_
get_ restitution_ threshold - Get the restitution speed threshold. (b3World_GetRestitutionThreshold)
- world_
get_ sensor_ events - Get sensor events for the current time step. The event data is transient. Do not store a reference to this data. (b3World_GetSensorEvents)
- world_
get_ user_ data - Get the user data pointer. (b3World_GetUserData)
- world_
get_ worker_ count - Get the stored worker count. (b3World_GetWorkerCount)
- world_
is_ continuous_ enabled - Is continuous collision enabled? (b3World_IsContinuousEnabled)
- world_
is_ sleeping_ enabled - Is sleeping enabled? (b3World_IsSleepingEnabled)
- world_
is_ speculative_ enabled - Is speculative contact enabled?
- world_
is_ valid - World id validity. (b3World_IsValid)
- world_
is_ warm_ starting_ enabled - Is constraint warm starting enabled? (b3World_IsWarmStartingEnabled)
- world_
overlap_ aabb - Overlap test for all shapes that potentially overlap the provided AABB. The callback receives each overlapping shape id and returns false to terminate the query. (b3World_OverlapAABB + static TreeQueryCallback)
- world_
overlap_ shape - Overlap test for all shapes that overlap the provided shape proxy. (b3World_OverlapShape + static TreeOverlapCallback)
- world_
rebuild_ static_ tree - Rebuild the static broad-phase tree. (b3World_RebuildStaticTree)
- world_
set_ contact_ recycle_ distance - Set the contact recycle distance. (b3World_SetContactRecycleDistance)
- world_
set_ contact_ tuning - Adjust contact tuning parameters. (b3World_SetContactTuning)
- world_
set_ custom_ filter_ callback - Register the custom filter callback. (b3World_SetCustomFilterCallback)
- world_
set_ friction_ callback - Register the friction callback. Passing
Nonerestores the default. (b3World_SetFrictionCallback) - world_
set_ gravity - Set the gravity vector. (b3World_SetGravity)
- world_
set_ hit_ event_ threshold - Adjust the hit event threshold. (b3World_SetHitEventThreshold)
- world_
set_ maximum_ linear_ speed - Set the maximum linear speed. (b3World_SetMaximumLinearSpeed)
- world_
set_ pre_ solve_ callback - Register the pre-solve callback. (b3World_SetPreSolveCallback)
- world_
set_ restitution_ callback - Register the restitution callback. Passing
Nonerestores the default. (b3World_SetRestitutionCallback) - world_
set_ restitution_ threshold - Adjust the restitution threshold. (b3World_SetRestitutionThreshold)
- world_
set_ user_ data - Set the user data pointer. (b3World_SetUserData)
- world_
set_ worker_ count - Set the stored worker count. Clamped to
[1, MAX_WORKERS]. The serial port does not spawn threads but still resizes task contexts like C. (b3World_SetWorkerCount) - world_
start_ recording - Begin capturing mutations into
recording. (b3World_StartRecording) - world_
stop_ recording - Finalize the active recording. (b3World_StopRecording)
Type Aliases§
- Custom
Filter Fcn - Prototype for a contact filter callback. (b3CustomFilterFcn)
- PreSolve
Fcn - Prototype for a pre-solve callback. (b3PreSolveFcn)