pub struct RapierPlugin { /* private fields */ }Expand description
User-facing handle to a Rapier physics simulation running inside viewport-lib.
Holds an Arc<Mutex<RapierState>> shared with the two RuntimePlugin
implementors returned by into_plugins. Cloning the
handle is cheap.
All methods on this handle take the state mutex. They block while the simulate plugin is running; keep handle calls off the hot path or batch them between frames.
Mutex poisoning: if a previous holder of the lock panicked (most
commonly: a closure passed to with_body_mut / with_state_mut
panicked), every subsequent lock acquisition transparently recovers
the underlying state and continues. The plugin never panics on a
poisoned mutex.
§Usage
use rapier_viewport_plugin::{RapierPlugin, rapier3d::prelude::*};
let mut rapier = RapierPlugin::new();
// Build bodies and colliders using rapier's own builders.
let floor_body = RigidBodyBuilder::fixed().build();
let floor_collider = ColliderBuilder::halfspace(Vector::z_axis()).build();
rapier.add_body(floor_id, floor_body, floor_collider);
let box_body = RigidBodyBuilder::dynamic()
.translation(glam::Vec3::new(0.0, 0.0, 5.0))
.build();
let box_collider = ColliderBuilder::cuboid(0.5, 0.5, 0.5)
.restitution(0.4)
.active_events(ActiveEvents::COLLISION_EVENTS)
.build();
rapier.add_body(box_id, box_body, box_collider);
let (prepare, simulate) = rapier.clone().into_plugins();For anything not in the curated API (joints, locked axes, sensor flags,
collision groups, the query pipeline, etc.) use the escape hatches:
with_body_mut, with_collider_mut,
with_state_mut, and with_state.
Implementations§
Source§impl RapierPlugin
impl RapierPlugin
pub fn with_gravity(self, g: Vec3) -> Self
Sourcepub fn add_body(&mut self, node_id: NodeId, body: RigidBody, collider: Collider)
pub fn add_body(&mut self, node_id: NodeId, body: RigidBody, collider: Collider)
Register a body and its collider, binding both to a scene node.
Construct body and collider with rapier’s RigidBodyBuilder and
ColliderBuilder. Initial transform, mass, restitution, friction,
collision groups, sensor flag, CCD, locked axes, and every other
rapier property are set on the builders.
Sourcepub fn remove_body(&mut self, node_id: NodeId)
pub fn remove_body(&mut self, node_id: NodeId)
Remove the body bound to node_id. No-op if the node was never registered.
Sourcepub fn clear_bodies(&mut self)
pub fn clear_bodies(&mut self)
Remove every registered body and collider.
Sourcepub fn body_handle(&self, node_id: NodeId) -> Option<RigidBodyHandle>
pub fn body_handle(&self, node_id: NodeId) -> Option<RigidBodyHandle>
Look up the rapier RigidBodyHandle for a registered body.
Sourcepub fn collider_handle(&self, node_id: NodeId) -> Option<ColliderHandle>
pub fn collider_handle(&self, node_id: NodeId) -> Option<ColliderHandle>
Look up the rapier ColliderHandle for a registered body.
Sourcepub fn with_body_mut<R>(
&mut self,
node_id: NodeId,
f: impl FnOnce(&mut RigidBody) -> R,
) -> Option<R>
pub fn with_body_mut<R>( &mut self, node_id: NodeId, f: impl FnOnce(&mut RigidBody) -> R, ) -> Option<R>
Sourcepub fn with_collider_mut<R>(
&mut self,
node_id: NodeId,
f: impl FnOnce(&mut Collider) -> R,
) -> Option<R>
pub fn with_collider_mut<R>( &mut self, node_id: NodeId, f: impl FnOnce(&mut Collider) -> R, ) -> Option<R>
Run a closure with mutable access to a registered body’s Collider.
Returns None if the node has no collider.
Sourcepub fn with_state_mut<R>(&mut self, f: impl FnOnce(&mut RapierState) -> R) -> R
pub fn with_state_mut<R>(&mut self, f: impl FnOnce(&mut RapierState) -> R) -> R
Run a closure with mutable access to the entire RapierState. Use
this for anything the curated API does not cover: joints, spatial
queries against the broad phase, character controllers, etc.
Note: the internal mutex is held for the duration of f. Do not call
back into other RapierPlugin methods from inside the closure – that
will deadlock.
Sourcepub fn with_state<R>(&self, f: impl FnOnce(&RapierState) -> R) -> R
pub fn with_state<R>(&self, f: impl FnOnce(&RapierState) -> R) -> R
Read-only counterpart to with_state_mut.
Sourcepub fn into_plugins(self) -> (RapierPreparePlugin, RapierSimulatePlugin)
pub fn into_plugins(self) -> (RapierPreparePlugin, RapierSimulatePlugin)
Split into the two RuntimePlugin objects the runtime needs. Bodies
can still be added or removed at runtime through the cloned handle
after this is called.
Trait Implementations§
Source§impl Clone for RapierPlugin
impl Clone for RapierPlugin
Source§fn clone(&self) -> RapierPlugin
fn clone(&self) -> RapierPlugin
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RapierPlugin
impl RefUnwindSafe for RapierPlugin
impl Send for RapierPlugin
impl Sync for RapierPlugin
impl Unpin for RapierPlugin
impl UnsafeUnpin for RapierPlugin
impl UnwindSafe for RapierPlugin
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§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.