aboutsummaryrefslogtreecommitdiff
path: root/src/geometry/collider.rs
blob: aed76c8a102b9009cc92386cdd87a07403143464 (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use crate::dynamics::{MassProperties, RigidBodyHandle, RigidBodySet};
use crate::geometry::{
    Ball, Capsule, ColliderGraphIndex, Contact, Cuboid, HeightField, InteractionGraph, Polygon,
    Proximity, Triangle, Trimesh,
};
use crate::math::{AngVector, Isometry, Point, Rotation, Vector};
use na::Point3;
use ncollide::bounding_volume::{HasBoundingVolume, AABB};
use num::Zero;

#[derive(Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
/// An enum grouping all the possible shape of a collider.
pub enum Shape {
    /// A ball shape.
    Ball(Ball),
    /// A convex polygon shape.
    Polygon(Polygon),
    /// A cuboid shape.
    Cuboid(Cuboid),
    /// A capsule shape.
    Capsule(Capsule),
    /// A triangle shape.
    Triangle(Triangle),
    /// A triangle mesh shape.
    Trimesh(Trimesh),
    /// A heightfield shape.
    HeightField(HeightField),
}

impl Shape {
    /// Gets a reference to the underlying ball shape, if `self` is one.
    pub fn as_ball(&self) -> Option<&Ball> {
        match self {
            Shape::Ball(b) => Some(b),
            _ => None,
        }
    }

    /// Gets a reference to the underlying polygon shape, if `self` is one.
    pub fn as_polygon(&self) -> Option<&Polygon> {
        match self {
            Shape::Polygon(p) => Some(p),
            _ => None,
        }
    }

    /// Gets a reference to the underlying cuboid shape, if `self` is one.
    pub fn as_cuboid(&self) -> Option<&Cuboid> {
        match self {
            Shape::Cuboid(c) => Some(c),
            _ => None,
        }
    }

    /// Gets a reference to the underlying capsule shape, if `self` is one.
    pub fn as_capsule(&self) -> Option<&Capsule> {
        match self {
            Shape::Capsule(c) => Some(c),
            _ => None,
        }
    }

    /// Gets a reference to the underlying triangle mesh shape, if `self` is one.
    pub fn as_trimesh(&self) -> Option<&Trimesh> {
        match self {
            Shape::Trimesh(c) => Some(c),
            _ => None,
        }
    }

    /// Gets a reference to the underlying heightfield shape, if `self` is one.
    pub fn as_heightfield(&self) -> Option<&HeightField> {
        match self {
            Shape::HeightField(h) => Some(h),
            _ => None,
        }
    }

    /// Gets a reference to the underlying triangle shape, if `self` is one.
    pub fn as_triangle(&self) -> Option<&Triangle> {
        match self {
            Shape::Triangle(c) => Some(c),
            _ => None,
        }
    }

    /// Computes the axis-aligned bounding box of this shape.
    pub fn compute_aabb(&self, position: &Isometry<f32>) -> AABB<f32> {
        match self {
            Shape::Ball(ball) => ball.bounding_volume(position),
            Shape::Polygon(poly) => poly.aabb(position),
            Shape::Capsule(caps) => caps.aabb(position),
            Shape::Cuboid(cuboid) => cuboid.bounding_volume(position),
            Shape::Triangle(triangle) => triangle.bounding_volume(position),
            Shape::Trimesh(trimesh) => trimesh.aabb(position),
            Shape::HeightField(heightfield) => heightfield.bounding_volume(position),
        }
    }
}

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
/// A geometric entity that can be attached to a body so it can be affected by contacts and proximity queries.
///
/// To build a new collider, use the `ColliderBuilder` structure.
pub struct Collider {
    shape: Shape,
    density: f32,
    is_sensor: bool,
    pub(crate) parent: RigidBodyHandle,
    pub(crate) delta: Isometry<f32>,
    pub(crate) position: Isometry<f32>,
    pub(crate) predicted_position: Isometry<f32>,
    /// The friction coefficient of this collider.
    pub friction: f32,
    /// The restitution coefficient of this collider.
    pub restitution: f32,
    pub(crate) contact_graph_index: ColliderGraphIndex,
    pub(crate) proximity_graph_index: ColliderGraphIndex,
    pub(crate) proxy_index: usize,
}

impl Clone for Collider {
    fn clone(&self) -> Self {
        Self {
            shape: self.shape.clone(),
            parent: RigidBodySet::invalid_handle(),
            contact_graph_index: ColliderGraphIndex::new(crate::INVALID_U32),
            proximity_graph_index: ColliderGraphIndex::new(crate::INVALID_U32),
            proxy_index: crate::INVALID_USIZE,
            ..*self
        }
    }
}

impl Collider {
    /// The rigid body this collider is attached to.
    pub fn parent(&self) -> RigidBodyHandle {
        self.parent
    }

    /// Is this collider a sensor?
    pub fn is_sensor(&self) -> bool {
        self.is_sensor
    }

    #[doc(hidden)]
    pub fn set_position_debug(&mut self, position: Isometry<f32>) {
        self.position = position;
    }

    /// The position of this collider expressed in the local-space of the rigid-body it is attached to.
    #[deprecated(note = "use `.position_wrt_parent()` instead.")]
    pub fn delta(&self) -> &Isometry<f32> {
        &self.delta
    }

    /// The world-space position of this collider.
    pub fn position(&self) -> &Isometry<f32> {
        &self.position
    }

    /// The position of this collider wrt the body it is attached to.
    pub fn position_wrt_parent(&self) -> &Isometry<f32> {
        &self.delta
    }

    /// The density of this collider.
    pub fn density(&self) -> f32 {
        self.density
    }

    /// The geometric shape of this collider.
    pub fn shape(&self) -> &Shape {
        &self.shape
    }

    /// Compute the axis-aligned bounding box of this collider.
    pub fn compute_aabb(&self) -> AABB<f32> {
        self.shape.compute_aabb(&self.position)
    }

    // pub(crate) fn compute_aabb_with_prediction(&self) -> AABB<f32> {
    //     let aabb1 = self.shape.compute_aabb(&self.position);
    //     let aabb2 = self.shape.compute_aabb(&self.predicted_position);
    //     aabb1.merged(&aabb2)
    // }

    /// Compute the local-space mass properties of this collider.
    pub fn mass_properties(&self) -> MassProperties {
        match &self.shape {
            Shape::Ball(ball) => MassProperties::from_ball(self.density, ball.radius),
            #[cfg(feature = "dim2")]
            Shape::Polygon(p) => MassProperties::from_polygon(self.density, p.vertices()),
            #[cfg(feature = "dim3")]
            Shape::Polygon(_p) => unimplemented!(),
            Shape::Cuboid(c) => MassProperties::from_cuboid(self.density, c.half_extents),
            Shape::Capsule(caps) => {
                MassProperties::from_capsule(self.density, caps.a, caps.b, caps.radius)
            }
            Shape::Triangle(_) => MassProperties::zero(),
            Shape::Trimesh(_) => MassProperties::zero(),
            Shape::HeightField(_) => MassProperties::zero(),
        }
    }
}

/// A structure responsible for building a new collider.
#[derive(Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ColliderBuilder {
    /// The shape of the collider to be built.
    pub shape: Shape,
    /// The density of the collider to be built.
    pub density: f32,
    /// The friction coefficient of the collider to be built.
    pub friction: f32,
    /// The restitution coefficient of the collider to be built.
    pub restitution: f32,
    /// The position of this collider relative to the local frame of the rigid-body it is attached to.
    pub delta: Isometry<f32>,
    /// Is this collider a sensor?
    pub is_sensor: bool,
}

impl ColliderBuilder {
    /// Initialize a new collider builder with the given shape.
    pub fn new(shape: Shape) -> Self {
        Self {
            shape,
            density: 1.0,
            friction: Self::default_friction(),
            restitution: 0.0,
            delta