```  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
```
```use downcast_rs::Downcast;
use na::{self, RealField};
use std::ops::Deref;
use std::sync::Arc;

use ncollide::query::TrackedContact;
use ncollide::shape::Shape;

use crate::material::MaterialsCoefficientsTable;
use crate::math::{Isometry, Vector};

/// The context for determining the local material properties at a contact.
#[derive(Copy, Clone)]
pub struct MaterialContext<'a, N: RealField> {
/// The shape of the collider involved in the contact.
pub shape: &'a dyn Shape<N>,
/// The position of the collider involved in the contact.
pub position: &'a Isometry<N>,
/// The contact.
pub contact: &'a TrackedContact<N>,
/// Whether the bodies (and collider) in this structure are the first one involved in the
/// contact.
///
/// This is `false` if the body involved in the contact is the second one.
pub is_first: bool,
}

impl<'a, N: RealField> MaterialContext<'a, N> {
pub(crate) fn new(
shape: &'a dyn Shape<N>,
position: &'a Isometry<N>,
contact: &'a TrackedContact<N>,
is_first: bool,
) -> Self {
MaterialContext {
shape,
position,
contact,
is_first,
}
}
}

/// The way the friction and restitution coefficients of two materials should be combined.
#[derive(Copy, Clone, Debug)]
pub enum MaterialCombineMode {
/// Combination by averaging the coefficients from both materials.
Average,
/// Combination by taking the min of the coefficients from both materials.
///
/// Has precedence over the `Average` combine mode.
Min,
/// Combination by multiplying the coefficients from both materials.
///
/// Has precedence over the `Min` and `Average` combine modes.
Multiply,
/// Combination by taking the max the coefficients from both materials.
///
/// Has precedence over all other combine mode.
Max,
/// Should not be used directly. This is set as a result of the `combine` method
/// if the combination was performed by a lookup on the `MaterialsCoefficientsTable`.
Lookup, // Same as Average if specified by the user.
}

impl MaterialCombineMode {
/// Combines two coefficients using their associated MaterialCombineMode.
///
/// The combine mode with the highest precedence among the two provided determines
/// the actual formula used. Precedences are described on the `MaterialCombineMode` enum.
#[inline]
pub fn combine<N: RealField>(a: (N, Self), b: (N, Self)) -> (N, MaterialCombineMode) {
match (a.1, b.1) {
(MaterialCombineMode::Max, _) | (_, MaterialCombineMode::Max) => {
(a.0.max(b.0), MaterialCombineMode::Max)
}
(MaterialCombineMode::Multiply, _) | (_, MaterialCombineMode::Multiply) => {
(a.0 * b.0, MaterialCombineMode::Multiply)
}
(MaterialCombineMode::Min, _) | (_, MaterialCombineMode::Min) => {
(a.0.min(b.0), MaterialCombineMode::Min)
}
// Average
_ => ((a.0 + b.0) * na::convert(0.5), MaterialCombineMode::Average),
}
}
}

/// Computed material properties at a contact point.
pub struct LocalMaterialProperties<N: RealField> {
/// The optional material identifier used for pairwise material coefficient lookup table.
pub id: Option<MaterialId>,
/// The friction coefficient and its combination mode.
pub friction: (N, MaterialCombineMode),
/// The restitution coefficient and its combination mode.
pub restitution: (N, MaterialCombineMode),
/// The surface velocity at this point.
pub surface_velocity: Vector<N>,
}

/// An utility trait to clone material trait-objects.
pub trait MaterialClone<N: RealField> {
/// Clone a material trait-object.
fn clone_box(&self) -> Box<dyn Material<N>> {
unimplemented!()
}
}

/// The identifier of a material.
pub type MaterialId = u32;

impl<N: RealField, T: 'static + Material<N> + Clone> MaterialClone<N> for T {
fn clone_box(&self) -> Box<dyn Material<N>> {
Box::new(self.clone())
}
}

/// An abstract material.
pub trait Material<N: RealField>: Downcast + Send + Sync + MaterialClone<N> {
/// Retrieve the local material properties of a collider at the given contact point.
fn local_properties(&self, context: MaterialContext<N>) -> LocalMaterialProperties<N>;
}

impl_downcast!(Material<N> where N: RealField);

impl<N: RealField> Clone for Box<dyn Material<N>> {
fn clone(&self) -> Box<dyn Material<N>> {
self.clone_box()
}
}

impl<N: RealField> dyn Material<N> {
/// Combine two materials given their contexts and a material lookup table.
pub fn combine<M1, M2>(
table: &MaterialsCoefficientsTable<N>,
material1: &M1,
context1: MaterialContext<N>,
material2: &M2,
context2: MaterialContext<N>,
) -> LocalMaterialProperties<N>
where
M1: ?Sized + Material<N>,
M2: ?Sized + Material<N>,
{
let props1 = material1.local_properties(context1);
let props2 = material2.local_properties(context2);
let restitution;
let friction;

match (props1.id, props2.id) {
(Some(id1), Some(id2)) => {
restitution = table
.restitution_coefficient(id1, id2)
.map(|coeff| (coeff, MaterialCombineMode::Lookup))
.unwrap_or_else(|| {
MaterialCombineMode::combine(props1.restitution, props2.restitution)
});
friction = table
.friction_coefficient(id1, id2)
.map(|coeff| (coeff, MaterialCombineMode::Lookup))
.unwrap_or_else(|| {
MaterialCombineMode::combine(props1.friction, props2.friction)
});
}
_ => {
restitution = MaterialCombineMode::combine(props1.restitution, props2.restitution);
friction = MaterialCombineMode::combine(props1.friction, props2.friction);
}
}

LocalMaterialProperties {
id: None,
friction,
restitution,
surface_velocity: props1.surface_velocity - props2.surface_velocity,
}
}
}

/// A shared handle to an abstract shape.
///
/// This can be mutated using COW.
#[derive(Clone)]
pub struct MaterialHandle<N: RealField>(Arc<Box<dyn Material<N>>>);

impl<N: RealField> MaterialHandle<N> {
/// Creates a sharable shape handle from a shape.
#[inline]
pub fn new<S: Material<N> + Clone>(material: S) -> MaterialHandle<N> {
MaterialHandle(Arc::new(Box::new(material)))
}

pub(crate) fn make_mut(&mut self) -> &mut dyn Material<N> {
&mut **Arc::make_mut(&mut self.0)
}
}

impl<N: RealField> AsRef<dyn Material<N>> for MaterialHandle<N> {
#[inline]
fn as_ref(&self) -> &dyn Material<N> {
&*self.deref()
}
}

impl<N: RealField> Deref for MaterialHandle<N> {
type Target = dyn Material<N>;

#[inline]
fn deref(&self) -> &dyn Material<N> {
&**self.0.deref()
}
}
```