Add methods to read the mass or volume of a collider.

This commit is contained in:
Sébastien Crozet
2022-07-05 15:15:17 +02:00
parent ba081fb6f5
commit 7831ebfc31
5 changed files with 32 additions and 11 deletions

View File

@@ -7,6 +7,7 @@
- Remove the deprecated methods `RigidBodyBuilder::mass`, `::principal_angular_inertia`, `::principal_inertia`. - Remove the deprecated methods `RigidBodyBuilder::mass`, `::principal_angular_inertia`, `::principal_inertia`.
- Remove the methods `RigidBodyBuilder::additional_principal_angular_inertia`. Use - Remove the methods `RigidBodyBuilder::additional_principal_angular_inertia`. Use
`RigidBodyBuilder::additional_mass_properties` instead. `RigidBodyBuilder::additional_mass_properties` instead.
- The `Collider::density` method now always returns a `Real` (instead of an `Option<Real>`).
### Added ### Added
- Add `RigidBody::recompute_mass_properties_from_colliders` to force the immediate computation - 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. 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 - 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. 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) ## v0.13.0 (31 May 2022)
### Fixed ### Fixed

View File

@@ -4,8 +4,7 @@ use crate::dynamics::{
RigidBodyIds, RigidBodyMassProps, RigidBodyPosition, RigidBodyType, RigidBodyVelocity, RigidBodyIds, RigidBodyMassProps, RigidBodyPosition, RigidBodyType, RigidBodyVelocity,
}; };
use crate::geometry::{ use crate::geometry::{
Collider, ColliderHandle, ColliderMassProps, ColliderParent, ColliderPosition, ColliderSet, ColliderHandle, ColliderMassProps, ColliderParent, ColliderPosition, ColliderSet, ColliderShape,
ColliderShape,
}; };
use crate::math::{AngVector, Isometry, Point, Real, Rotation, Vector}; use crate::math::{AngVector, Isometry, Point, Real, Rotation, Vector};
use crate::utils::WCross; use crate::utils::WCross;
@@ -484,7 +483,7 @@ impl RigidBody {
} }
/// Removes a collider from this rigid-body. /// 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) { if let Some(i) = self.colliders.0.iter().position(|e| *e == handle) {
self.changes.set(RigidBodyChanges::COLLIDERS, true); self.changes.set(RigidBodyChanges::COLLIDERS, true);
self.colliders.0.swap_remove(i); self.colliders.0.swap_remove(i);

View File

@@ -237,12 +237,32 @@ impl Collider {
&self.material &self.material
} }
/// The density of this collider, if set. /// The volume (or surface in 2D) of this collider.
pub fn density(&self) -> Option<Real> { 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 { match &self.mprops {
ColliderMassProps::Density(density) => Some(*density), ColliderMassProps::Density(density) => *density,
ColliderMassProps::Mass(_) => None, ColliderMassProps::Mass(mass) => {
ColliderMassProps::MassProperties(_) => None, 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) => self.shape.mass_properties(*density).mass(),
ColliderMassProps::Mass(mass) => *mass,
ColliderMassProps::MassProperties(mprops) => mprops.mass(),
} }
} }

View File

@@ -137,7 +137,7 @@ impl ColliderSet {
if let Some(parent_handle) = curr_parent { if let Some(parent_handle) = curr_parent {
if let Some(rb) = bodies.get_mut(parent_handle) { 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) = if let Some(parent_rb) =
bodies.get_mut_internal_with_modification_tracking(parent.handle) 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 { if wake_up {
islands.wake_up(bodies, parent.handle, true); islands.wake_up(bodies, parent.handle, true);

View File

@@ -338,7 +338,7 @@ impl PhysxWorld {
let densities: Vec<_> = rb let densities: Vec<_> = rb
.colliders() .colliders()
.iter() .iter()
.map(|h| colliders[*h].density().unwrap_or(0.0)) .map(|h| colliders[*h].density())
.collect(); .collect();
unsafe { unsafe {