From 036a24614171eff0f99495b8b6f1c09e58cb4f9a Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 3 Nov 2020 14:29:47 +0100 Subject: Make cloning rigid-bodies and colliders more idiomatic. Fix #53 --- src/geometry/collider.rs | 19 +++++++------------ src/geometry/collider_set.rs | 4 ++++ 2 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/geometry') diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs index f53d75a..524f265 100644 --- a/src/geometry/collider.rs +++ b/src/geometry/collider.rs @@ -188,6 +188,7 @@ impl<'de> serde::Deserialize<'de> for ColliderShape { } #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[derive(Clone)] /// A geometric entity that can be attached to a body so it can be affected by contacts and proximity queries. /// /// To build a new collider, use the `ColliderBuilder` structure. @@ -212,20 +213,14 @@ pub struct Collider { pub user_data: u128, } -impl Clone for Collider { - fn clone(&self) -> Self { - Self { - shape: self.shape.clone(), - parent: RigidBodySet::invalid_handle(), - contact_graph_index: ColliderGraphIndex::new(crate::INVALID_U32), - proximity_graph_index: ColliderGraphIndex::new(crate::INVALID_U32), - proxy_index: crate::INVALID_USIZE, - ..*self - } +impl Collider { + pub(crate) fn reset_internal_links(&mut self) { + self.parent = RigidBodySet::invalid_handle(); + self.contact_graph_index = ColliderGraphIndex::new(crate::INVALID_U32); + self.proximity_graph_index = ColliderGraphIndex::new(crate::INVALID_U32); + self.proxy_index = crate::INVALID_USIZE; } -} -impl Collider { /// The rigid body this collider is attached to. pub fn parent(&self) -> RigidBodyHandle { self.parent diff --git a/src/geometry/collider_set.rs b/src/geometry/collider_set.rs index 5ac9658..d2bb8cc 100644 --- a/src/geometry/collider_set.rs +++ b/src/geometry/collider_set.rs @@ -59,6 +59,10 @@ impl ColliderSet { parent_handle: RigidBodyHandle, bodies: &mut RigidBodySet, ) -> ColliderHandle { + // Make sure the internal links are reset, they may not be + // if this rigid-body was obtained by cloning another one. + coll.reset_internal_links(); + coll.parent = parent_handle; let parent = bodies .get_mut_internal(parent_handle) -- cgit