diff options
| author | Sébastien Crozet <developer@crozet.re> | 2020-11-03 15:33:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-03 15:33:11 +0100 |
| commit | f70a840f79943aa6da49db2590e13dcd3f3a89ed (patch) | |
| tree | 1c743ed924b10ebd0eb0982e5c7d798235c12cee /src/dynamics | |
| parent | 0cc850dc505a4034103d229fdce22f9ec7bc410a (diff) | |
| parent | 71611d3e30ce2fddee20832db3c3e0c8b6ba0d07 (diff) | |
| download | rapier-f70a840f79943aa6da49db2590e13dcd3f3a89ed.tar.gz rapier-f70a840f79943aa6da49db2590e13dcd3f3a89ed.tar.bz2 rapier-f70a840f79943aa6da49db2590e13dcd3f3a89ed.zip | |
Merge pull request #54 from dimforge/idiomatic_clone
Allow cloning all the physics components worth cloning
Diffstat (limited to 'src/dynamics')
| -rw-r--r-- | src/dynamics/joint/joint.rs | 1 | ||||
| -rw-r--r-- | src/dynamics/rigid_body.rs | 25 | ||||
| -rw-r--r-- | src/dynamics/rigid_body_set.rs | 7 |
3 files changed, 17 insertions, 16 deletions
diff --git a/src/dynamics/joint/joint.rs b/src/dynamics/joint/joint.rs index 074f802..9fe6488 100644 --- a/src/dynamics/joint/joint.rs +++ b/src/dynamics/joint/joint.rs @@ -95,6 +95,7 @@ impl From<PrismaticJoint> for JointParams { } #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[derive(Clone)] /// A joint attached to two bodies. pub struct Joint { /// Handle to the first body attached to this joint. diff --git a/src/dynamics/rigid_body.rs b/src/dynamics/rigid_body.rs index 6b897a6..d3bd8d7 100644 --- a/src/dynamics/rigid_body.rs +++ b/src/dynamics/rigid_body.rs @@ -27,7 +27,7 @@ pub enum BodyStatus { /// A rigid body. /// /// To create a new rigid-body, use the `RigidBodyBuilder` structure. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct RigidBody { /// The world-space position of the rigid-body. pub position: Isometry<f32>, @@ -58,20 +58,6 @@ pub struct RigidBody { pub user_data: u128, } -impl Clone for RigidBody { - fn clone(&self) -> Self { - Self { - colliders: Vec::new(), - joint_graph_index: RigidBodyGraphIndex::new(crate::INVALID_U32), - active_island_id: crate::INVALID_USIZE, - active_set_id: crate::INVALID_USIZE, - active_set_offset: crate::INVALID_USIZE, - active_set_timestamp: crate::INVALID_U32, - ..*self - } - } -} - impl RigidBody { fn new() -> Self { Self { @@ -96,6 +82,15 @@ impl RigidBody { } } + pub(crate) fn reset_internal_references(&mut self) { + self.colliders = Vec::new(); + self.joint_graph_index = InteractionGraph::<()>::invalid_graph_index(); + self.active_island_id = 0; + self.active_set_id = 0; + self.active_set_offset = 0; + self.active_set_timestamp = 0; + } + pub(crate) fn integrate_accelerations(&mut self, dt: f32, gravity: Vector<f32>) { if self.mass_properties.inv_mass != 0.0 { self.linvel += (gravity + self.linacc) * dt; diff --git a/src/dynamics/rigid_body_set.rs b/src/dynamics/rigid_body_set.rs index 83f1c51..b857173 100644 --- a/src/dynamics/rigid_body_set.rs +++ b/src/dynamics/rigid_body_set.rs @@ -75,6 +75,7 @@ impl BodyPair { } #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[derive(Clone)] /// A set of rigid bodies that can be handled by a physics pipeline. pub struct RigidBodySet { // NOTE: the pub(crate) are needed by the broad phase @@ -154,7 +155,11 @@ impl RigidBodySet { } /// Insert a rigid body into this set and retrieve its handle. - pub fn insert(&mut self, rb: RigidBody) -> RigidBodyHandle { + pub fn insert(&mut self, mut rb: RigidBody) -> RigidBodyHandle { + // Make sure the internal links are reset, they may not be + // if this rigid-body was obtained by cloning another one. + rb.reset_internal_references(); + let handle = self.bodies.insert(rb); let rb = &mut self.bodies[handle]; |
