Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enum variants rather than casting to u8 for comparisons #781

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a changelog line ?

#[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
Loading