pub struct RigidBodySet { /* private fields */ }Expand description
The collection that stores all rigid bodies in your physics world.
This is where you add, remove, and access all your physics objects. Think of it as a “database” of all rigid bodies, where each body gets a unique handle for fast lookup.
§Why use handles?
Instead of storing bodies directly, you get back a RigidBodyHandle when inserting.
This handle is lightweight (just an index + generation) and remains valid even if other
bodies are removed, protecting you from use-after-free bugs.
§Example
let mut bodies = RigidBodySet::new();
// Add a dynamic body
let handle = bodies.insert(RigidBodyBuilder::dynamic());
// Access it later
if let Some(body) = bodies.get_mut(handle) {
body.apply_impulse(vector![0.0, 10.0, 0.0], true);
}Implementations§
Source§impl RigidBodySet
impl RigidBodySet
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty collection of rigid bodies.
Call this once when setting up your physics world. The collection will automatically grow as you add more bodies.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new collection with pre-allocated space for the given number of bodies.
Use this if you know approximately how many bodies you’ll need, to avoid multiple reallocations as the collection grows.
§Example
// You know you'll have ~1000 bodies
let mut bodies = RigidBodySet::with_capacity(1000);Sourcepub fn contains(&self, handle: RigidBodyHandle) -> bool
pub fn contains(&self, handle: RigidBodyHandle) -> bool
Checks if the given handle points to a valid rigid body that still exists.
Returns false if the body was removed or the handle is invalid.
Sourcepub fn insert(&mut self, rb: impl Into<RigidBody>) -> RigidBodyHandle
pub fn insert(&mut self, rb: impl Into<RigidBody>) -> RigidBodyHandle
Adds a rigid body to the world and returns its handle for future access.
The handle is how you’ll refer to this body later (to move it, apply forces, etc.). Keep the handle somewhere accessible - you can’t get the body back without it!
§Example
let handle = bodies.insert(
RigidBodyBuilder::dynamic()
.translation(vector![0.0, 5.0, 0.0])
.build()
);
// Store `handle` to access this body laterSourcepub fn remove(
&mut self,
handle: RigidBodyHandle,
islands: &mut IslandManager,
colliders: &mut ColliderSet,
impulse_joints: &mut ImpulseJointSet,
multibody_joints: &mut MultibodyJointSet,
remove_attached_colliders: bool,
) -> Option<RigidBody>
pub fn remove( &mut self, handle: RigidBodyHandle, islands: &mut IslandManager, colliders: &mut ColliderSet, impulse_joints: &mut ImpulseJointSet, multibody_joints: &mut MultibodyJointSet, remove_attached_colliders: bool, ) -> Option<RigidBody>
Removes a rigid body from the world along with all its attached colliders and joints.
This is a complete cleanup operation that removes:
- The rigid body itself
- All colliders attached to it (if
remove_attached_collidersistrue) - All joints connected to this body
Returns the removed body if it existed, or None if the handle was invalid.
§Parameters
remove_attached_colliders- Iftrue, removes all colliders attached to this body. Iffalse, the colliders are detached and become independent.
§Example
// Remove a body and everything attached to it
if let Some(body) = bodies.remove(
handle,
&mut islands,
&mut colliders,
&mut impulse_joints,
&mut multibody_joints,
true // Remove colliders too
) {
println!("Removed body at {:?}", body.translation());
}Sourcepub fn get_unknown_gen(&self, i: u32) -> Option<(&RigidBody, RigidBodyHandle)>
pub fn get_unknown_gen(&self, i: u32) -> Option<(&RigidBody, RigidBodyHandle)>
Gets a rigid body by its index without knowing the generation number.
⚠️ Advanced/unsafe usage - prefer get() instead!
This bypasses the generation check that normally protects against the ABA problem (where an index gets reused after removal). Only use this if you’re certain the body at this index is the one you expect.
Returns both the body and its current handle (with the correct generation).
Sourcepub fn get_unknown_gen_mut(
&mut self,
i: u32,
) -> Option<(&mut RigidBody, RigidBodyHandle)>
pub fn get_unknown_gen_mut( &mut self, i: u32, ) -> Option<(&mut RigidBody, RigidBodyHandle)>
Gets a mutable reference to a rigid body by its index without knowing the generation.
⚠️ Advanced/unsafe usage - prefer get_mut() instead!
This bypasses the generation check. See get_unknown_gen()
for more details on when this is appropriate (rarely).
Sourcepub fn get(&self, handle: RigidBodyHandle) -> Option<&RigidBody>
pub fn get(&self, handle: RigidBodyHandle) -> Option<&RigidBody>
Gets a read-only reference to the rigid body with the given handle.
Returns None if the handle is invalid or the body was removed.
Use this to read body properties like position, velocity, mass, etc.
Sourcepub fn get_mut(&mut self, handle: RigidBodyHandle) -> Option<&mut RigidBody>
pub fn get_mut(&mut self, handle: RigidBodyHandle) -> Option<&mut RigidBody>
Gets a mutable reference to the rigid body with the given handle.
Returns None if the handle is invalid or the body was removed.
Use this to modify body properties, apply forces/impulses, change velocities, etc.
§Example
if let Some(body) = bodies.get_mut(handle) {
body.set_linvel(vector![1.0, 0.0, 0.0], true);
body.apply_impulse(vector![0.0, 100.0, 0.0], true);
}Sourcepub fn get_pair_mut(
&mut self,
handle1: RigidBodyHandle,
handle2: RigidBodyHandle,
) -> (Option<&mut RigidBody>, Option<&mut RigidBody>)
pub fn get_pair_mut( &mut self, handle1: RigidBodyHandle, handle2: RigidBodyHandle, ) -> (Option<&mut RigidBody>, Option<&mut RigidBody>)
Gets mutable references to two different rigid bodies at once.
This is useful when you need to modify two bodies simultaneously (e.g., when manually
handling collisions between them). If both handles are the same, only the first value
will be Some.
§Example
let (body1, body2) = bodies.get_pair_mut(handle1, handle2);
if let (Some(b1), Some(b2)) = (body1, body2) {
// Can modify both bodies at once
b1.apply_impulse(vector![10.0, 0.0, 0.0], true);
b2.apply_impulse(vector![-10.0, 0.0, 0.0], true);
}Sourcepub fn iter(&self) -> impl Iterator<Item = (RigidBodyHandle, &RigidBody)>
pub fn iter(&self) -> impl Iterator<Item = (RigidBodyHandle, &RigidBody)>
Iterates over all rigid bodies in this collection.
Each iteration yields a (handle, &RigidBody) pair. Use this to read properties
of all bodies (positions, velocities, etc.) without modifying them.
§Example
for (handle, body) in bodies.iter() {
println!("Body {:?} is at {:?}", handle, body.translation());
}Sourcepub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (RigidBodyHandle, &mut RigidBody)>
pub fn iter_mut( &mut self, ) -> impl Iterator<Item = (RigidBodyHandle, &mut RigidBody)>
Iterates over all rigid bodies with mutable access.
Each iteration yields a (handle, &mut RigidBody) pair. Use this to modify
multiple bodies in one pass.
§Example
// Apply gravity manually to all dynamic bodies
for (handle, body) in bodies.iter_mut() {
if body.is_dynamic() {
body.add_force(vector![0.0, -9.81 * body.mass(), 0.0], true);
}
}Sourcepub fn propagate_modified_body_positions_to_colliders(
&self,
colliders: &mut ColliderSet,
)
pub fn propagate_modified_body_positions_to_colliders( &self, colliders: &mut ColliderSet, )
Updates the positions of all colliders attached to bodies that have moved.
Normally you don’t need to call this - it’s automatically handled by PhysicsPipeline::step.
Only call this manually if you’re:
- Moving bodies yourself outside of
step() - Using
QueryPipelinefor raycasts without running physics simulation - Need collider positions to be immediately up-to-date for some custom logic
This synchronizes collider world positions based on their parent bodies’ positions.
Trait Implementations§
Source§impl Clone for RigidBodySet
impl Clone for RigidBodySet
Source§fn clone(&self) -> RigidBodySet
fn clone(&self) -> RigidBodySet
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RigidBodySet
impl Debug for RigidBodySet
Source§impl Default for RigidBodySet
impl Default for RigidBodySet
Source§fn default() -> RigidBodySet
fn default() -> RigidBodySet
Source§impl Index<Index> for RigidBodySet
impl Index<Index> for RigidBodySet
Source§impl Index<RigidBodyHandle> for RigidBodySet
impl Index<RigidBodyHandle> for RigidBodySet
Source§impl IndexMut<RigidBodyHandle> for RigidBodySet
Available on non-crate feature dev-remove-slow-accessors only.
impl IndexMut<RigidBodyHandle> for RigidBodySet
dev-remove-slow-accessors only.Auto Trait Implementations§
impl Freeze for RigidBodySet
impl RefUnwindSafe for RigidBodySet
impl Send for RigidBodySet
impl Sync for RigidBodySet
impl Unpin for RigidBodySet
impl UnwindSafe for RigidBodySet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.