diff options
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | src/dynamics/rigid_body.rs | 5 | ||||
| -rw-r--r-- | src/geometry/collider.rs | 30 | ||||
| -rw-r--r-- | src/geometry/collider_set.rs | 4 | ||||
| -rw-r--r-- | src_testbed/physx_backend.rs | 2 |
5 files changed, 32 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index df7d819..0f1da52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Remove the deprecated methods `RigidBodyBuilder::mass`, `::principal_angular_inertia`, `::principal_inertia`. - Remove the methods `RigidBodyBuilder::additional_principal_angular_inertia`. Use `RigidBodyBuilder::additional_mass_properties` instead. +- The `Collider::density` method now always returns a `Real` (instead of an `Option<Real>`). ### Added - Add `RigidBody::recompute_mass_properties_from_colliders` to force the immediate computation @@ -20,6 +21,7 @@ computed based on this mass and on its shape. - Add `ColliderBuilder::mass` to set the mass of the collider instead of its density. Its angular inertia tensor will be automatically computed based on this mass and its shape. +- Add `Collider::mass` and `Collider::volume` to retrieve the mass or volume of a collider. ## v0.13.0 (31 May 2022) ### Fixed diff --git a/src/dynamics/rigid_body.rs b/src/dynamics/rigid_body.rs index 494dc6c..a44e335 100644 --- a/src/dynamics/rigid_body.rs +++ b/src/dynamics/rigid_body.rs @@ -4,8 +4,7 @@ use crate::dynamics::{ RigidBodyIds, RigidBodyMassProps, RigidBodyPosition, RigidBodyType, RigidBodyVelocity, }; use crate::geometry::{ - Collider, ColliderHandle, ColliderMassProps, ColliderParent, ColliderPosition, ColliderSet, - ColliderShape, + ColliderHandle, ColliderMassProps, ColliderParent, ColliderPosition, ColliderSet, ColliderShape, }; use crate::math::{AngVector, Isometry, Point, Real, Rotation, Vector}; use crate::utils::WCross; @@ -484,7 +483,7 @@ impl RigidBody { } /// Removes a collider from this rigid-body. - pub(crate) fn remove_collider_internal(&mut self, handle: ColliderHandle, coll: &Collider) { + pub(crate) fn remove_collider_internal(&mut self, handle: ColliderHandle) { if let Some(i) = self.colliders.0.iter().position(|e| *e == handle) { self.changes.set(RigidBodyChanges::COLLIDERS, true); self.colliders.0.swap_remove(i); diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs index 421523f..7b0cc4c 100644 --- a/src/geometry/collider.rs +++ b/src/geometry/collider.rs @@ -237,12 +237,32 @@ impl Collider { &self.material } - /// The density of this collider, if set. - pub fn density(&self) -> Option<Real> { + /// The volume (or surface in 2D) of this collider. + pub fn volume(&self) -> Real { + self.shape.mass_properties(1.0).mass() + } + + /// The density of this collider. + pub fn density(&self) -> Real { + match &self.mprops { + ColliderMassProps::Density(density) => *density, + ColliderMassProps::Mass(mass) => { + let inv_volume = self.shape.mass_properties(1.0).inv_mass; + mass * inv_volume + } + ColliderMassProps::MassProperties(mprops) => { + let inv_volume = self.shape.mass_properties(1.0).inv_mass; + mprops.mass() * inv_volume + } + } + } + + /// The mass of this collider. + pub fn mass(&self) -> Real { match &self.mprops { - ColliderMassProps::Density(density) => Some(*density), - ColliderMassProps::Mass(_) => None, - ColliderMassProps::MassProperties(_) => None, + ColliderMassProps::Density(density) => self.shape.mass_properties(*density).mass(), + ColliderMassProps::Mass(mass) => *mass, + ColliderMassProps::MassProperties(mprops) => mprops.mass(), } } diff --git a/src/geometry/collider_set.rs b/src/geometry/collider_set.rs index b9f4ae3..b141445 100644 --- a/src/geometry/collider_set.rs +++ b/src/geometry/collider_set.rs @@ -137,7 +137,7 @@ impl ColliderSet { if let Some(parent_handle) = curr_parent { if let Some(rb) = bodies.get_mut(parent_handle) { - rb.remove_collider_internal(handle, &*collider); + rb.remove_collider_internal(handle); } } @@ -189,7 +189,7 @@ impl ColliderSet { if let Some(parent_rb) = bodies.get_mut_internal_with_modification_tracking(parent.handle) { - parent_rb.remove_collider_internal(handle, &collider); + parent_rb.remove_collider_internal(handle); if wake_up { islands.wake_up(bodies, parent.handle, true); diff --git a/src_testbed/physx_backend.rs b/src_testbed/physx_backend.rs index 6978848..0a839b9 100644 --- a/src_testbed/physx_backend.rs +++ b/src_testbed/physx_backend.rs @@ -338,7 +338,7 @@ impl PhysxWorld { let densities: Vec<_> = rb .colliders() .iter() - .map(|h| colliders[*h].density().unwrap_or(0.0)) + .map(|h| colliders[*h].density()) .collect(); unsafe { |
