crcbl_phys/material.rs
1//! Surface materials: the friction and restitution a contact reads.
2//!
3//! `docs/plan/37-materials.md` names a **surface material** as its own asset
4//! on colliders, beside the render material, with friction and restitution
5//! among its properties; the contact solver's plan asked for the pair's
6//! values to come from per-property combination rules — "multiply / average /
7//! max" — so ice on rubber has an answer that is data rather than code.
8//!
9//! **The contact solver is the one consumer.** Rung 1 of
10//! the contact solver calls [`SurfaceMaterial::combine`] on a contact's two
11//! bodies each tick, and a plane carries a material of its own. The asset form,
12//! the link from a render material and the other consumers `37-materials.md`
13//! lists are not built.
14
15/// How two surfaces' values for one property become the contact's.
16///
17/// When the two surfaces ask for different rules, the one later in this list
18/// wins — [`Average`](Self::Average) gives way to anything, and
19/// [`Max`](Self::Max) to nothing. That precedence is PhysX's `PxCombineMode`,
20/// which Unity's physics materials expose the same way; taking it rather than
21/// inventing one means a content author's expectation carries over.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
23pub enum CombineRule {
24 /// The mean of the two values.
25 #[default]
26 Average,
27 /// The smaller value.
28 Min,
29 /// The product of the values.
30 Multiply,
31 /// The larger value.
32 Max,
33}
34
35impl CombineRule {
36 /// `a` and `b` combined under this rule.
37 #[must_use]
38 pub fn apply(self, a: f64, b: f64) -> f64 {
39 match self {
40 Self::Average => 0.5 * (a + b),
41 Self::Min => a.min(b),
42 Self::Multiply => a * b,
43 Self::Max => a.max(b),
44 }
45 }
46}
47
48/// A surface's friction and restitution, and how each combines with the
49/// surface it touches.
50#[derive(Debug, Clone, Copy, PartialEq)]
51pub struct SurfaceMaterial {
52 /// The coefficient of friction, zero or more: the ratio of the tangential
53 /// force a contact can resist to the normal force pressing it.
54 pub friction: f64,
55 /// The coefficient of restitution, from zero (no bounce) to one (a
56 /// perfectly elastic bounce): the ratio of separating to approaching speed.
57 pub restitution: f64,
58 /// How [`friction`](Self::friction) combines with the other surface's.
59 pub friction_combine: CombineRule,
60 /// How [`restitution`](Self::restitution) combines with the other
61 /// surface's.
62 pub restitution_combine: CombineRule,
63}
64
65impl SurfaceMaterial {
66 /// Friction 0.6 and restitution 0, both averaged: Unity's default physics
67 /// material, and Box2D's default friction.
68 pub const DEFAULT: Self = Self::new(0.6, 0.0);
69
70 /// A material with these coefficients and both combined by
71 /// [`CombineRule::Average`].
72 #[must_use]
73 pub const fn new(friction: f64, restitution: f64) -> Self {
74 Self {
75 friction,
76 restitution,
77 friction_combine: CombineRule::Average,
78 restitution_combine: CombineRule::Average,
79 }
80 }
81
82 /// The friction and restitution a contact between `self` and `other`
83 /// uses, each under the rule of higher precedence the two ask for.
84 ///
85 /// Symmetric: which surface is `self` does not change the answer.
86 #[must_use]
87 pub fn combine(&self, other: &Self) -> ContactMaterial {
88 let friction_rule = self.friction_combine.max(other.friction_combine);
89 let restitution_rule = self.restitution_combine.max(other.restitution_combine);
90 ContactMaterial {
91 friction: friction_rule.apply(self.friction, other.friction),
92 restitution: restitution_rule.apply(self.restitution, other.restitution),
93 }
94 }
95}
96
97impl Default for SurfaceMaterial {
98 fn default() -> Self {
99 Self::DEFAULT
100 }
101}
102
103/// The friction and restitution one contact between two surfaces uses.
104#[derive(Debug, Clone, Copy, PartialEq)]
105pub struct ContactMaterial {
106 /// The combined coefficient of friction.
107 pub friction: f64,
108 /// The combined coefficient of restitution.
109 pub restitution: f64,
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn each_rule_combines_as_named() {
118 assert_eq!(CombineRule::Average.apply(0.2, 0.6), 0.4);
119 assert_eq!(CombineRule::Min.apply(0.2, 0.6), 0.2);
120 assert_eq!(CombineRule::Multiply.apply(0.5, 0.6), 0.3);
121 assert_eq!(CombineRule::Max.apply(0.2, 0.6), 0.6);
122 }
123
124 /// Ice on rubber: rubber asks for its grip to win and ice asks for an
125 /// average, and the rule of higher precedence decides — whichever surface
126 /// is asked first.
127 #[test]
128 fn the_higher_precedence_rule_wins_whichever_surface_asks() {
129 let ice = SurfaceMaterial::new(0.05, 0.1);
130 let rubber = SurfaceMaterial {
131 friction_combine: CombineRule::Max,
132 restitution_combine: CombineRule::Min,
133 ..SurfaceMaterial::new(1.0, 0.8)
134 };
135 let want = ContactMaterial {
136 friction: 1.0,
137 restitution: 0.1,
138 };
139 assert_eq!(ice.combine(&rubber), want);
140 assert_eq!(rubber.combine(&ice), want);
141
142 // And with both asking for the default, the average.
143 let got = ice.combine(&SurfaceMaterial::new(0.25, 0.5));
144 assert_eq!(got.friction, 0.15);
145 assert_eq!(got.restitution, 0.3);
146 }
147
148 #[test]
149 fn the_precedence_is_average_min_multiply_max() {
150 use CombineRule::{Average, Max, Min, Multiply};
151 assert!(Average < Min && Min < Multiply && Multiply < Max);
152 }
153}