bevy_silk 0.4.0

Cloth physics implementation in bevy
Documentation
use bevy::math::Vec3;
use bevy::reflect::Reflect;

/// Defines how the cloth will compute sticks from mesh indices.
#[derive(Debug, Copy, Clone, Default, Reflect)]
pub enum StickGeneration {
    #[default]
    /// 2 sticks will be generated by triangle, following the actual quad edges
    Quads,
    /// 3 sticks will be generated by triangle
    Triangles,
}

/// Defines the target length of cloth sticks
#[derive(Debug, Copy, Clone, Default, Reflect)]
pub enum StickLen {
    #[default]
    /// The target length will be the actual distance between the vertices
    Auto,
    /// Custom target length
    Fixed(f32),
    /// Same as [`StickLen::Auto`] with a custom additional offset
    Offset(f32),
    /// Same as [`StickLen::Auto`] with a custom coefficient
    Coefficient(f32),
}

impl StickLen {
    /// Retrieves the stick length from the two points it connects
    #[must_use]
    pub fn get_stick_len(&self, point_a: Vec3, point_b: Vec3) -> f32 {
        match self {
            Self::Auto => point_a.distance(point_b),
            Self::Fixed(v) => *v,
            Self::Offset(offset) => point_a.distance(point_b) + offset,
            Self::Coefficient(coeff) => point_a.distance(point_b) * coeff,
        }
    }
}