Skip to content

Commit d291041

Browse files
authored
use enum variants rather than casting to u8 for comparisons (#781)
1 parent 2f9d9ba commit d291041

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/dynamics/coefficient_combine_rule.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,34 @@ use crate::math::Real;
77
/// Each collider has its combination rule of type
88
/// `CoefficientCombineRule`. And the rule
99
/// actually used is given by `max(first_combine_rule as usize, second_combine_rule as usize)`.
10-
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
10+
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
1111
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
1212
pub enum CoefficientCombineRule {
1313
/// The two coefficients are averaged.
1414
#[default]
1515
Average = 0,
1616
/// The smallest coefficient is chosen.
17-
Min,
17+
Min = 1,
1818
/// The two coefficients are multiplied.
19-
Multiply,
19+
Multiply = 2,
2020
/// The greatest coefficient is chosen.
21-
Max,
21+
Max = 3,
2222
}
2323

2424
impl CoefficientCombineRule {
25-
pub(crate) fn combine(coeff1: Real, coeff2: Real, rule_value1: u8, rule_value2: u8) -> Real {
25+
pub(crate) fn combine(
26+
coeff1: Real,
27+
coeff2: Real,
28+
rule_value1: CoefficientCombineRule,
29+
rule_value2: CoefficientCombineRule,
30+
) -> Real {
2631
let effective_rule = rule_value1.max(rule_value2);
2732

2833
match effective_rule {
29-
0 => (coeff1 + coeff2) / 2.0,
30-
1 => coeff1.min(coeff2),
31-
2 => coeff1 * coeff2,
32-
_ => coeff1.max(coeff2),
34+
CoefficientCombineRule::Average => (coeff1 + coeff2) / 2.0,
35+
CoefficientCombineRule::Min => coeff1.min(coeff2),
36+
CoefficientCombineRule::Multiply => coeff1 * coeff2,
37+
CoefficientCombineRule::Max => coeff1.max(coeff2),
3338
}
3439
}
3540
}

src/geometry/narrow_phase.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,14 @@ impl NarrowPhase {
969969
let friction = CoefficientCombineRule::combine(
970970
co1.material.friction,
971971
co2.material.friction,
972-
co1.material.friction_combine_rule as u8,
973-
co2.material.friction_combine_rule as u8,
972+
co1.material.friction_combine_rule,
973+
co2.material.friction_combine_rule,
974974
);
975975
let restitution = CoefficientCombineRule::combine(
976976
co1.material.restitution,
977977
co2.material.restitution,
978-
co1.material.restitution_combine_rule as u8,
979-
co2.material.restitution_combine_rule as u8,
978+
co1.material.restitution_combine_rule,
979+
co2.material.restitution_combine_rule,
980980
);
981981

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

0 commit comments

Comments
 (0)