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
pub use collision::{CollisionStrategy, Contact};
pub use collision::prelude::Primitive;
pub mod narrow;
pub mod ecs;
pub mod broad;
pub mod util;
use std::fmt::Debug;
use cgmath::prelude::*;
use collision::prelude::*;
use Real;
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum CollisionMode {
Discrete,
Continuous,
}
#[derive(Debug, Clone)]
pub struct ContactEvent<ID, P>
where
P: EuclideanSpace,
P::Diff: Debug,
{
pub bodies: (ID, ID),
pub contact: Contact<P>,
}
impl<ID, P> ContactEvent<ID, P>
where
ID: Clone + Debug,
P: EuclideanSpace,
P::Diff: VectorSpace + Zero + Debug,
{
pub fn new(bodies: (ID, ID), contact: Contact<P>) -> Self {
Self { bodies, contact }
}
pub fn new_single(strategy: CollisionStrategy, bodies: (ID, ID)) -> Self {
Self::new(bodies, Contact::new(strategy))
}
}
#[derive(Debug, Clone)]
pub struct CollisionShape<P, T>
where
P: Primitive,
{
pub enabled: bool,
base_bound: P::Aabb,
transformed_bound: P::Aabb,
primitives: Vec<(P, T)>,
strategy: CollisionStrategy,
mode: CollisionMode,
}
impl<P, T> CollisionShape<P, T>
where
P: Primitive,
P::Aabb: Aabb<Scalar = Real>,
T: Transform<P::Point>,
{
pub fn new_complex(
strategy: CollisionStrategy,
mode: CollisionMode,
primitives: Vec<(P, T)>,
) -> Self {
let bound = get_bound(&primitives);
Self {
base_bound: bound.clone(),
primitives,
enabled: true,
transformed_bound: bound,
strategy,
mode,
}
}
pub fn new_simple(strategy: CollisionStrategy, mode: CollisionMode, primitive: P) -> Self {
Self::new_complex(strategy, mode, vec![(primitive, T::one())])
}
pub fn new_simple_offset(
strategy: CollisionStrategy,
mode: CollisionMode,
primitive: P,
transform: T,
) -> Self {
Self::new_complex(strategy, mode, vec![(primitive, transform)])
}
pub fn update(&mut self, start: &T, end: Option<&T>) {
self.transformed_bound = match end {
None => self.base_bound.transform(start),
Some(end_t) => {
let base = self.base_bound.transform(end_t);
if self.mode == CollisionMode::Continuous {
base.union(&self.base_bound.transform(start))
} else {
base
}
}
};
}
pub fn bound(&self) -> &P::Aabb {
&self.transformed_bound
}
}
fn get_bound<P, T>(primitives: &Vec<(P, T)>) -> P::Aabb
where
P: Primitive,
P::Aabb: Aabb<Scalar = Real>,
T: Transform<P::Point>,
{
primitives
.iter()
.map(|&(ref p, ref t)| p.get_bound().transform(t))
.fold(P::Aabb::zero(), |bound, b| bound.union(&b))
}