Skip to content

Commit

Permalink
use enum variants rather than casting to u8 for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jan 7, 2025
1 parent cf77b5b commit 07b470d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/dynamics/coefficient_combine_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@ use crate::math::Real;
/// Each collider has its combination rule of type
/// `CoefficientCombineRule`. And the rule
/// 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))]
pub enum CoefficientCombineRule {
/// The two coefficients are averaged.
#[default]
Average = 0,
/// The smallest coefficient is chosen.
Min,
Min = 1,
/// The two coefficients are multiplied.
Multiply,
Multiply = 2,
/// The greatest coefficient is chosen.
Max,
Max = 3,
}

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);

match effective_rule {
0 => (coeff1 + coeff2) / 2.0,
1 => coeff1.min(coeff2),
2 => coeff1 * coeff2,
_ => coeff1.max(coeff2),
CoefficientCombineRule::Average => (coeff1 + coeff2) / 2.0,
CoefficientCombineRule::Min => coeff1.min(coeff2),
CoefficientCombineRule::Multiply => coeff1 * coeff2,
CoefficientCombineRule::Max => coeff1.max(coeff2),
}
}
}
8 changes: 4 additions & 4 deletions src/geometry/narrow_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,14 +957,14 @@ impl NarrowPhase {
let friction = CoefficientCombineRule::combine(
co1.material.friction,
co2.material.friction,
co1.material.friction_combine_rule as u8,
co2.material.friction_combine_rule as u8,
co1.material.friction_combine_rule,
co2.material.friction_combine_rule,
);
let restitution = CoefficientCombineRule::combine(
co1.material.restitution,
co2.material.restitution,
co1.material.restitution_combine_rule as u8,
co2.material.restitution_combine_rule as u8,
co1.material.restitution_combine_rule,
co2.material.restitution_combine_rule,
);

let zero = RigidBodyDominance(0); // The value doesn't matter, it will be MAX because of the effective groups.
Expand Down

0 comments on commit 07b470d

Please sign in to comment.