Skip to content

Commit

Permalink
Unoptimized method, update read mass properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Jul 28, 2023
1 parent e2e96a9 commit 0971422
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/dynamics/rigid_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions src/plugin/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0971422

Please sign in to comment.