use enum variants rather than casting to u8 for comparisons (#781)

This commit is contained in:
Thierry Berger
2025-03-28 12:19:42 +01:00
committed by GitHub
parent 2f9d9ba94b
commit d291041278
2 changed files with 18 additions and 13 deletions

View File

@@ -7,29 +7,34 @@ use crate::math::Real;
/// Each collider has its combination rule of type /// Each collider has its combination rule of type
/// `CoefficientCombineRule`. And the rule /// `CoefficientCombineRule`. And the rule
/// actually used is given by `max(first_combine_rule as usize, second_combine_rule as usize)`. /// actually used is given by `max(first_combine_rule as usize, second_combine_rule as usize)`.
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)] #[derive(Default, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub enum CoefficientCombineRule { pub enum CoefficientCombineRule {
/// The two coefficients are averaged. /// The two coefficients are averaged.
#[default] #[default]
Average = 0, Average = 0,
/// The smallest coefficient is chosen. /// The smallest coefficient is chosen.
Min, Min = 1,
/// The two coefficients are multiplied. /// The two coefficients are multiplied.
Multiply, Multiply = 2,
/// The greatest coefficient is chosen. /// The greatest coefficient is chosen.
Max, Max = 3,
} }
impl CoefficientCombineRule { impl CoefficientCombineRule {
pub(crate) fn combine(coeff1: Real, coeff2: Real, rule_value1: u8, rule_value2: u8) -> Real { pub(crate) fn combine(
coeff1: Real,
coeff2: Real,
rule_value1: CoefficientCombineRule,
rule_value2: CoefficientCombineRule,
) -> Real {
let effective_rule = rule_value1.max(rule_value2); let effective_rule = rule_value1.max(rule_value2);
match effective_rule { match effective_rule {
0 => (coeff1 + coeff2) / 2.0, CoefficientCombineRule::Average => (coeff1 + coeff2) / 2.0,
1 => coeff1.min(coeff2), CoefficientCombineRule::Min => coeff1.min(coeff2),
2 => coeff1 * coeff2, CoefficientCombineRule::Multiply => coeff1 * coeff2,
_ => coeff1.max(coeff2), CoefficientCombineRule::Max => coeff1.max(coeff2),
} }
} }
} }

View File

@@ -969,14 +969,14 @@ impl NarrowPhase {
let friction = CoefficientCombineRule::combine( let friction = CoefficientCombineRule::combine(
co1.material.friction, co1.material.friction,
co2.material.friction, co2.material.friction,
co1.material.friction_combine_rule as u8, co1.material.friction_combine_rule,
co2.material.friction_combine_rule as u8, co2.material.friction_combine_rule,
); );
let restitution = CoefficientCombineRule::combine( let restitution = CoefficientCombineRule::combine(
co1.material.restitution, co1.material.restitution,
co2.material.restitution, co2.material.restitution,
co1.material.restitution_combine_rule as u8, co1.material.restitution_combine_rule,
co2.material.restitution_combine_rule as u8, co2.material.restitution_combine_rule,
); );
let zero = RigidBodyDominance(0); // The value doesn't matter, it will be MAX because of the effective groups. let zero = RigidBodyDominance(0); // The value doesn't matter, it will be MAX because of the effective groups.