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
use downcast_rs::Downcast;
use std::sync::Arc;
use std::ops::Deref;
use na::{self, RealField};
use ncollide::query::TrackedContact;
use crate::object::{Body, BodyPart, Collider};
use crate::material::MaterialsCoefficientsTable;
use crate::math::Vector;
#[derive(Copy, Clone)]
pub struct MaterialContext<'a, N: RealField> {
pub body: &'a Body<N>,
pub body_part: &'a BodyPart<N>,
pub collider: &'a Collider<N>,
pub contact: &'a TrackedContact<N>,
pub is_first: bool,
}
impl<'a, N: RealField> MaterialContext<'a, N> {
pub(crate) fn new(body: &'a Body<N>, body_part: &'a BodyPart<N>, collider: &'a Collider<N>, contact: &'a TrackedContact<N>, is_first: bool) -> Self {
MaterialContext {
body,
body_part,
collider,
contact,
is_first
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum MaterialCombineMode {
Average,
Min,
Multiply,
Max,
Lookup
}
impl MaterialCombineMode {
#[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),
_ => ((a.0 + b.0) * na::convert(0.5), MaterialCombineMode::Average)
}
}
}
pub struct LocalMaterialProperties<N: RealField> {
pub id: Option<MaterialId>,
pub friction: (N, MaterialCombineMode),
pub restitution: (N, MaterialCombineMode),
pub surface_velocity: Vector<N>,
}
pub trait MaterialClone<N: RealField> {
fn clone_box(&self) -> Box<Material<N>> {
unimplemented!()
}
}
pub type MaterialId = u32;
impl<N: RealField, T: 'static + Material<N> + Clone> MaterialClone<N> for T {
fn clone_box(&self) -> Box<Material<N>> {
Box::new(self.clone())
}
}
pub trait Material<N: RealField>: Downcast + Send + Sync + MaterialClone<N> {
fn local_properties(&self, context: MaterialContext<N>) -> LocalMaterialProperties<N>;
}
impl_downcast!(Material<N> where N: RealField);
impl<N: RealField> Clone for Box<Material<N>> {
fn clone(&self) -> Box<Material<N>> {
self.clone_box()
}
}
impl<N: RealField> Material<N> {
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,
}
}
}
#[derive(Clone)]
pub struct MaterialHandle<N: RealField>(Arc<Box<Material<N>>>);
impl<N: RealField> MaterialHandle<N> {
#[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 Material<N> {
&mut **Arc::make_mut(&mut self.0)
}
}
impl<N: RealField> AsRef<Material<N>> for MaterialHandle<N> {
#[inline]
fn as_ref(&self) -> &Material<N> {
&*self.deref()
}
}
impl<N: RealField> Deref for MaterialHandle<N> {
type Target = Material<N>;
#[inline]
fn deref(&self) -> &Material<N> {
&**self.0.deref()
}
}