Module assembly

Source
Expand description

Bounded mechanisms inserted only after every ordinary error is preflighted.

Descriptors own existing dimensional bodies, colliders and local JointDescs; no second geometry representation or solver is involved. IDs are zero-based indices in one assembly’s insertion order, not generational world handles or cross-plan tokens. A clone retains the same local indexing. Do not mix IDs from unrelated plans: equal indices intentionally name the local entry. Existing-world joint endpoints are excluded. Plans are consumed on insertion, including on error; clone a template or call validate_assembly first when needed. Validation is a snapshot, not a reservation against later world edits.

A lab can author an unpowered compound rotor on a fixed support, validate it, publish its visual handles only after success, and rebuild from the template on reset. Checked constructors handle untrusted numeric input before staging:

use ferravex_physics::{Collider, assembly::Assembly, d2::{
    ColliderBuilder, JointBuilder, PhysicsWorld, RigidBody, RigidBodyBuilder,
}};
let mut template = Assembly::<RigidBody>::new();
let support = template.add_body(RigidBodyBuilder::fixed(), [] as [Collider; 0]).unwrap();
let rotor = template.add_body(RigidBodyBuilder::dynamic(), [
    ColliderBuilder::try_cuboid(0.7, 0.1).unwrap(),
    ColliderBuilder::try_ball(0.2).unwrap().translation([0.5, 0.].into()),
]).unwrap();
let hinge = template.add_joint(support, rotor, JointBuilder::hinge()).unwrap();
let mut world = PhysicsWorld::default();
world.validate_assembly(&template).unwrap();
let handles = world.insert_assembly(template.clone()).unwrap();
assert_eq!(world.bodies[handles.body(rotor).unwrap()].colliders(),
    handles.colliders(rotor).unwrap());
assert_eq!(handles.collider(rotor, 1), Some(handles.colliders(rotor).unwrap()[1]));
assert_eq!(world.joints[handles.joint(hinge).unwrap()].bodies(),
    [handles.body(support).unwrap(), handles.body(rotor).unwrap()]);
world.step(1. / 120.); // Insertion itself executes no simulation tick.
for &body in handles.bodies() { assert!(world.remove_body(body)); }
let reset = world.insert_assembly(template).unwrap();
assert_ne!(reset.body(rotor), handles.body(rotor)); // Ordinary generation reuse.
assert_eq!(reset.joints().len(), 1);

Spatial assemblies use the same container with a different existing body type:

use ferravex_physics::{Collider, assembly::Assembly, d3::{
    ColliderBuilder, JointBuilder, PhysicsWorld, RigidBody, RigidBodyBuilder,
}};
let mut assembly = Assembly::<RigidBody>::default();
let anchor = assembly.add_body(RigidBodyBuilder::fixed(), [] as [Collider; 0]).unwrap();
let bob = assembly.add_body(RigidBodyBuilder::dynamic()
    .try_translation([0., -2., 0.].into()).unwrap(),
    [ColliderBuilder::try_ball(0.3).unwrap()]).unwrap();
let spring = assembly.add_joint(anchor, bob, JointBuilder::spring(2., 20., 2.)).unwrap();
assert_eq!((assembly.body_count(), assembly.collider_count(), assembly.joint_count()),
    (2, 1, 1));
let mut world = PhysicsWorld::default();
let handles = world.insert_assembly(assembly).unwrap();
assert!(world.joints.get(handles.joint(spring).unwrap()).is_some());
world.step(1. / 120.);
assert!((world.bodies[handles.body(bob).unwrap()].translation().y + 2.).abs() < 1e-5);

Structs§

Assembly
Owned, bounded authoring plan; see the executable module recipes. At most 4096 bodies, 16384 total colliders, 32 children per body and 4096 joints. Storage grows on authoring; there is no steady-tick allocation promise.
AssemblyHandles
Mappings in deterministic plan order, not sorted by recycled world slot. These are ordinary same-world generational handles; this result neither owns nor removes world objects. After removal, its copied handles are stale. Local collider IDs are (LocalBodyId, child_index) in the original child order. See the module recipe for lookups and template-based reset.
LocalBodyId
Zero-based body index in an assembly. See the module recipe and lifetime rules.
LocalJointId
Zero-based joint index in an assembly. See the module recipe.

Enums§

AssemblyError
Ordinary preflight failure. No world state or arena identity is changed. Forward references must resolve inside the final plan:

Constants§

MAX_ASSEMBLY_BODIES
Maximum staged bodies, matching the existing world’s 4096-body limit.