rapier3d/dynamics/
coefficient_combine_rule.rs

1use crate::math::Real;
2
3/// Rules used to combine two coefficients.
4///
5/// This is used to determine the effective restitution and
6/// friction coefficients for a contact between two colliders.
7/// Each collider has its combination rule of type
8/// `CoefficientCombineRule`. And the rule
9/// actually used is given by `max(first_combine_rule as usize, second_combine_rule as usize)`.
10#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
11#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
12pub enum CoefficientCombineRule {
13    /// The two coefficients are averaged.
14    #[default]
15    Average = 0,
16    /// The smallest coefficient is chosen.
17    Min = 1,
18    /// The two coefficients are multiplied.
19    Multiply = 2,
20    /// The greatest coefficient is chosen.
21    Max = 3,
22}
23
24impl CoefficientCombineRule {
25    pub(crate) fn combine(
26        coeff1: Real,
27        coeff2: Real,
28        rule_value1: CoefficientCombineRule,
29        rule_value2: CoefficientCombineRule,
30    ) -> Real {
31        let effective_rule = rule_value1.max(rule_value2);
32
33        match effective_rule {
34            CoefficientCombineRule::Average => (coeff1 + coeff2) / 2.0,
35            CoefficientCombineRule::Min => coeff1.min(coeff2),
36            CoefficientCombineRule::Multiply => coeff1 * coeff2,
37            CoefficientCombineRule::Max => coeff1.max(coeff2),
38        }
39    }
40}