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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::bounding_volume::{self, BoundingVolume, AABB};
use crate::math::Isometry;
use crate::pipeline::broad_phase::BroadPhaseProxyHandle;
use crate::pipeline::narrow_phase::CollisionObjectGraphIndex;
use crate::pipeline::object::CollisionGroups;
use crate::pipeline::object::GeometricQueryType;
use crate::shape::{Shape, ShapeHandle};
use alga::general::RealField;
bitflags! {
#[derive(Default)]
pub struct CollisionObjectUpdateFlags: u8 {
const POSITION_CHANGED = 0b00000001;
const PREDICTED_POSITION_CHANGED = 0b00000010;
const SHAPE_CHANGED = 0b000100;
const COLLISION_GROUPS_CHANGED = 0b001000;
const QUERY_TYPE_CHANGED = 0b0010000;
}
}
impl CollisionObjectUpdateFlags {
pub fn needs_broad_phase_update(&self) -> bool {
!self.is_empty()
}
pub fn needs_narrow_phase_update(&self) -> bool {
self.intersects(
Self::POSITION_CHANGED
| Self::SHAPE_CHANGED
| Self::COLLISION_GROUPS_CHANGED
| Self::QUERY_TYPE_CHANGED,
)
}
pub fn needs_bounding_volume_update(&self) -> bool {
self.intersects(Self::POSITION_CHANGED | Self::SHAPE_CHANGED | Self::QUERY_TYPE_CHANGED)
}
pub fn needs_broad_phase_redispatch(&self) -> bool {
self.intersects(
Self::SHAPE_CHANGED | Self::COLLISION_GROUPS_CHANGED | Self::QUERY_TYPE_CHANGED,
)
}
}
pub trait CollisionObjectRef<N: RealField> {
fn graph_index(&self) -> Option<CollisionObjectGraphIndex>;
fn proxy_handle(&self) -> Option<BroadPhaseProxyHandle>;
fn position(&self) -> &Isometry<N>;
fn predicted_position(&self) -> Option<&Isometry<N>>;
fn shape(&self) -> &dyn Shape<N>;
fn collision_groups(&self) -> &CollisionGroups;
fn query_type(&self) -> GeometricQueryType<N>;
fn update_flags(&self) -> CollisionObjectUpdateFlags;
fn compute_aabb(&self) -> AABB<N> {
let mut aabb = bounding_volume::aabb(self.shape(), self.position());
aabb.loosen(self.query_type().query_limit());
aabb
}
fn compute_swept_aabb(&self) -> AABB<N> {
if let Some(predicted_pos) = self.predicted_position() {
let shape = self.shape();
let mut aabb1 = bounding_volume::aabb(shape, self.position());
let mut aabb2 = bounding_volume::aabb(shape, predicted_pos);
let margin = self.query_type().query_limit();
aabb1.loosen(margin);
aabb2.loosen(margin);
aabb1.merge(&aabb2);
aabb1
} else {
self.compute_aabb()
}
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CollisionObjectSlabHandle(pub usize);
impl CollisionObjectSlabHandle {
#[inline]
pub fn uid(&self) -> usize {
self.0
}
}
pub struct CollisionObject<N: RealField, T> {
proxy_handle: Option<BroadPhaseProxyHandle>,
graph_index: Option<CollisionObjectGraphIndex>,
position: Isometry<N>,
predicted_position: Option<Isometry<N>>,
shape: ShapeHandle<N>,
collision_groups: CollisionGroups,
query_type: GeometricQueryType<N>,
update_flags: CollisionObjectUpdateFlags,
data: T,
}
impl<N: RealField, T> CollisionObject<N, T> {
pub fn new(
proxy_handle: Option<BroadPhaseProxyHandle>,
graph_index: Option<CollisionObjectGraphIndex>,
position: Isometry<N>,
shape: ShapeHandle<N>,
groups: CollisionGroups,
query_type: GeometricQueryType<N>,
data: T,
) -> CollisionObject<N, T> {
CollisionObject {
proxy_handle,
graph_index,
position,
predicted_position: None,
shape,
collision_groups: groups,
data,
query_type,
update_flags: CollisionObjectUpdateFlags::all(),
}
}
#[inline]
pub fn graph_index(&self) -> Option<CollisionObjectGraphIndex> {
self.graph_index
}
#[inline]
pub fn set_graph_index(&mut self, index: Option<CollisionObjectGraphIndex>) {
self.graph_index = index
}
pub fn update_flags_mut(&mut self) -> &mut CollisionObjectUpdateFlags {
&mut self.update_flags
}
pub fn clear_update_flags(&mut self) {
self.update_flags = CollisionObjectUpdateFlags::empty()
}
#[inline]
pub fn proxy_handle(&self) -> Option<BroadPhaseProxyHandle> {
self.proxy_handle
}
#[inline]
pub fn set_proxy_handle(&mut self, handle: Option<BroadPhaseProxyHandle>) {
self.proxy_handle = handle
}
#[inline]
pub fn position(&self) -> &Isometry<N> {
&self.position
}
#[inline]
pub fn predicted_position(&self) -> Option<&Isometry<N>> {
self.predicted_position.as_ref()
}
#[inline]
pub fn set_position(&mut self, pos: Isometry<N>) {
self.update_flags |= CollisionObjectUpdateFlags::POSITION_CHANGED;
self.update_flags |= CollisionObjectUpdateFlags::PREDICTED_POSITION_CHANGED;
self.position = pos;
self.predicted_position = None;
}
#[inline]
pub fn set_position_with_prediction(&mut self, pos: Isometry<N>, prediction: Isometry<N>) {
self.update_flags |= CollisionObjectUpdateFlags::POSITION_CHANGED;
self.update_flags |= CollisionObjectUpdateFlags::PREDICTED_POSITION_CHANGED;
self.position = pos;
self.predicted_position = Some(prediction);
}
#[inline]
pub fn set_predicted_position(&mut self, pos: Option<Isometry<N>>) {
self.update_flags |= CollisionObjectUpdateFlags::PREDICTED_POSITION_CHANGED;
self.predicted_position = pos;
}
#[inline]
pub fn set_deformations(&mut self, coords: &[N]) {
self.update_flags |= CollisionObjectUpdateFlags::POSITION_CHANGED;
self.shape
.make_mut()
.as_deformable_shape_mut()
.expect("Attempting to deform a non-deformable shape.")
.set_deformations(coords)
}
#[inline]
pub fn shape(&self) -> &ShapeHandle<N> {
&self.shape
}
#[inline]
pub fn set_shape(&mut self, shape: ShapeHandle<N>) {
self.update_flags |= CollisionObjectUpdateFlags::SHAPE_CHANGED;
self.shape = shape
}
#[inline]
pub fn collision_groups(&self) -> &CollisionGroups {
&self.collision_groups
}
#[inline]
pub fn set_collision_groups(&mut self, groups: CollisionGroups) {
self.update_flags |= CollisionObjectUpdateFlags::COLLISION_GROUPS_CHANGED;
self.collision_groups = groups
}
#[inline]
pub fn query_type(&self) -> GeometricQueryType<N> {
self.query_type
}
#[inline]
pub fn set_query_type(&mut self, query_type: GeometricQueryType<N>) {
self.update_flags |= CollisionObjectUpdateFlags::QUERY_TYPE_CHANGED;
self.query_type = query_type;
}
#[inline]
pub fn data(&self) -> &T {
&self.data
}
#[inline]
pub fn data_mut(&mut self) -> &mut T {
&mut self.data
}
}
impl<N: RealField, T> CollisionObjectRef<N> for CollisionObject<N, T> {
fn graph_index(&self) -> Option<CollisionObjectGraphIndex> {
self.graph_index()
}
fn proxy_handle(&self) -> Option<BroadPhaseProxyHandle> {
self.proxy_handle()
}
fn position(&self) -> &Isometry<N> {
self.position()
}
fn predicted_position(&self) -> Option<&Isometry<N>> {
self.predicted_position()
}
fn shape(&self) -> &dyn Shape<N> {
self.shape().as_ref()
}
fn collision_groups(&self) -> &CollisionGroups {
self.collision_groups()
}
fn query_type(&self) -> GeometricQueryType<N> {
self.query_type()
}
fn update_flags(&self) -> CollisionObjectUpdateFlags {
self.update_flags
}
}