From 09714221fd101b541216ceaf920d83609b5591b9 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 27 Jul 2023 20:08:40 -0700 Subject: [PATCH] Unoptimized method, update read mass properties --- src/dynamics/rigid_body.rs | 4 ++++ src/plugin/systems.rs | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/dynamics/rigid_body.rs b/src/dynamics/rigid_body.rs index 209ce648..72ca59b6 100644 --- a/src/dynamics/rigid_body.rs +++ b/src/dynamics/rigid_body.rs @@ -164,6 +164,10 @@ impl ReadMassProperties { pub fn get(&self) -> &MassProperties { &self.0 } + + pub(crate) fn set(&mut self, mass_props: MassProperties) { + self.0 = mass_props; + } } /// Center-of-mass, mass, and angular inertia. diff --git a/src/plugin/systems.rs b/src/plugin/systems.rs index acd9ec04..5f638306 100644 --- a/src/plugin/systems.rs +++ b/src/plugin/systems.rs @@ -42,6 +42,7 @@ pub type RigidBodyWritebackComponents<'a> = ( Option<&'a mut TransformInterpolation>, Option<&'a mut Velocity>, Option<&'a mut Sleeping>, + Option<&'a mut ReadMassProperties>, ); /// Components related to rigid-bodies. @@ -526,7 +527,7 @@ pub fn writeback_rigid_bodies( let scale = context.physics_scale; if config.physics_pipeline_active { - for (entity, parent, transform, mut interpolation, mut velocity, mut sleeping) in + for (entity, parent, transform, mut interpolation, mut velocity, mut sleeping, mut mass_props) in writeback.iter_mut() { // TODO: do this the other way round: iterate through Rapier’s RigidBodySet on the active bodies, @@ -651,6 +652,17 @@ pub fn writeback_rigid_bodies( sleeping.sleeping = rb.is_sleeping(); } } + + if let Some(mass_props) = &mut mass_props { + let new_mass_props = MassProperties::from_rapier(rb.mass_properties().local_mprops, scale); + + // NOTE: we write the new value only if there was an + // actual change, in order to not trigger bevy’s + // change tracking when the values didn’t change. + if mass_props.get() != &new_mass_props { + mass_props.set(new_mass_props); + } + } } } } @@ -873,10 +885,10 @@ pub fn init_colliders( // Inserting the collider changed the rigid-body’s mass properties. // Read them back from the engine. if let Some(parent_body) = context.bodies.get(body_handle) { - mprops.0 = MassProperties::from_rapier( + mprops.set(MassProperties::from_rapier( parent_body.mass_properties().local_mprops, physics_scale, - ); + )); } } handle