parry3d/mass_properties/
mass_properties_capsule.rs

1use crate::mass_properties::MassProperties;
2use crate::math::{Point, Real};
3#[cfg(feature = "dim3")]
4use crate::shape::Capsule;
5
6impl MassProperties {
7    /// Computes the mass properties of a capsule.
8    pub fn from_capsule(density: Real, a: Point<Real>, b: Point<Real>, radius: Real) -> Self {
9        let half_height = (b - a).norm() / 2.0;
10        let (cyl_vol, cyl_unit_i) = Self::cylinder_y_volume_unit_inertia(half_height, radius);
11        let (ball_vol, ball_unit_i) = Self::ball_volume_unit_angular_inertia(radius);
12        let cap_vol = cyl_vol + ball_vol;
13        let cap_mass = cap_vol * density;
14        let mut cap_i = (cyl_unit_i * cyl_vol + ball_unit_i * ball_vol) * density;
15        let local_com = na::center(&a, &b);
16
17        #[cfg(feature = "dim2")]
18        {
19            let h = half_height * 2.0;
20            let extra = (h * h * 0.25 + h * radius * 3.0 / 8.0) * ball_vol * density;
21            cap_i += extra;
22            Self::new(local_com, cap_mass, cap_i)
23        }
24
25        #[cfg(feature = "dim3")]
26        {
27            let h = half_height * 2.0;
28            let extra = (h * h * 0.25 + h * radius * 3.0 / 8.0) * ball_vol * density;
29            cap_i.x += extra;
30            cap_i.z += extra;
31            let local_frame = Capsule::new(a, b, radius).rotation_wrt_y();
32            Self::with_principal_inertia_frame(local_com, cap_mass, cap_i, local_frame)
33        }
34    }
35}