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
use std::f32;
use std::marker::PhantomData;
use cgmath::prelude::*;
use cgmath::{Vector3, Point3};
use smallvec::SmallVec;
use collision::*;
use geom::*;
pub trait PruningParams {
const PERSISTENT_THRESHOLD_SQ: f32;
}
pub struct DefaultPruningParams {}
impl PruningParams for DefaultPruningParams {
const PERSISTENT_THRESHOLD_SQ: f32 = 0.5;
}
pub struct ContactPruner<Params = DefaultPruningParams>
where
Params: PruningParams
{
min_col_time: f32,
contacts: SmallVec<[LocalContact; 4]>,
params: PhantomData<Params>,
}
impl<Params: PruningParams> ContactPruner<Params> {
pub fn new() -> Self {
ContactPruner {
min_col_time: f32::INFINITY,
contacts: SmallVec::new(),
params: PhantomData,
}
}
pub fn with_capacity(cap: usize) -> Self {
ContactPruner {
min_col_time: f32::INFINITY,
contacts: SmallVec::with_capacity(cap),
params: PhantomData,
}
}
pub fn push(&mut self, new_contact: LocalContact) {
if new_contact.global.t < self.min_col_time - COLLISION_EPSILON {
self.contacts.clear();
self.contacts.push(new_contact);
self.min_col_time = new_contact.global.t;
return;
} else if new_contact.global.t > self.min_col_time + COLLISION_EPSILON {
return;
}
for old_contact in self.contacts.iter_mut() {
let ra = new_contact.global.a - old_contact.global.a;
let rb = new_contact.global.b - old_contact.global.b;
if ra.magnitude2() <= Params::PERSISTENT_THRESHOLD_SQ ||
rb.magnitude2() <= Params::PERSISTENT_THRESHOLD_SQ
{
let prev_dist = old_contact.local_a.to_vec().magnitude2() +
old_contact.local_b.to_vec().magnitude2();
let new_dist = new_contact.local_a.to_vec().magnitude2() +
new_contact.local_b.to_vec().magnitude2();
if prev_dist < new_dist {
*old_contact = new_contact;
}
return;
}
}
self.contacts.push(new_contact);
}
pub fn clear(&mut self) {
self.min_col_time = f32::INFINITY;
self.contacts.clear();
}
}
#[derive(Clone, Debug)]
pub struct Manifold {
pub time: f32,
pub normal: Vector3<f32>,
pub tangent_vector: [Vector3<f32>; 2],
pub contacts: SmallVec<[(Point3<f32>, Point3<f32>); 4]>,
}
impl From<LocalContact> for Manifold {
fn from(lc: LocalContact) -> Self {
Manifold {
time: lc.global.t,
normal: lc.global.n,
tangent_vector: compute_basis(&lc.global.n),
contacts: SmallVec::from_vec(vec![(lc.local_a, lc.local_b)]),
}
}
}
impl<P: PruningParams> From<ContactPruner<P>> for Manifold {
fn from(pruner: ContactPruner<P>) -> Self {
let mut contacts: SmallVec<[(Point3<f32>, Point3<f32>); 4]>
= SmallVec::with_capacity(pruner.contacts.len());
let avg_normal = pruner.contacts.iter()
.fold(Vector3::zero(),
|sum, lc| {
contacts.push((lc.local_a, lc.local_b));
sum + lc.global.n
}) / (pruner.contacts.len() as f32);
Manifold {
time: pruner.min_col_time,
normal: avg_normal,
tangent_vector: compute_basis(&avg_normal),
contacts
}
}
}
impl Manifold {
pub fn new() -> Self {
Manifold {
time: 0.0,
normal: Vector3::zero(),
tangent_vector: [Vector3::zero(), Vector3::zero()],
contacts: SmallVec::new(),
}
}
pub fn len(&self) -> usize {
self.contacts.len()
}
}