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
use crate::geometry::contact_generator::{
    ContactGenerator, ContactPhase, HeightFieldShapeContactGeneratorWorkspace,
    PrimitiveContactGenerator, TrimeshShapeContactGeneratorWorkspace,
};
use crate::geometry::Shape;
use std::any::Any;

/// Trait implemented by structures responsible for selecting a collision-detection algorithm
/// for a given pair of shapes.
pub trait ContactDispatcher {
    /// Select the collision-detection algorithm for the given pair of primitive shapes.
    fn dispatch_primitives(
        &self,
        shape1: &Shape,
        shape2: &Shape,
    ) -> (
        PrimitiveContactGenerator,
        Option<Box<dyn Any + Send + Sync>>,
    );
    /// Select the collision-detection algorithm for the given pair of non-primitive shapes.
    fn dispatch(
        &self,
        shape1: &Shape,
        shape2: &Shape,
    ) -> (ContactPhase, Option<Box<dyn Any + Send + Sync>>);
}

/// The default contact dispatcher used by Rapier.
pub struct DefaultContactDispatcher;

impl ContactDispatcher for DefaultContactDispatcher {
    fn dispatch_primitives(
        &self,
        shape1: &Shape,
        shape2: &Shape,
    ) -> (
        PrimitiveContactGenerator,
        Option<Box<dyn Any + Send + Sync>>,
    ) {
        match (shape1, shape2) {
            (Shape::Ball(_), Shape::Ball(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_ball_ball,
                    #[cfg(feature = "simd-is-enabled")]
                    generate_contacts_simd: super::generate_contacts_ball_ball_simd,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Cuboid(_), Shape::Cuboid(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_cuboid_cuboid,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Polygon(_), Shape::Polygon(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_polygon_polygon,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Capsule(_), Shape::Capsule(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_capsule_capsule,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Cuboid(_), Shape::Ball(_))
            | (Shape::Ball(_), Shape::Cuboid(_))
            | (Shape::Triangle(_), Shape::Ball(_))
            | (Shape::Ball(_), Shape::Triangle(_))
            | (Shape::Capsule(_), Shape::Ball(_))
            | (Shape::Ball(_), Shape::Capsule(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_ball_convex,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Capsule(_), Shape::Cuboid(_)) | (Shape::Cuboid(_), Shape::Capsule(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_cuboid_capsule,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (Shape::Triangle(_), Shape::Cuboid(_)) | (Shape::Cuboid(_), Shape::Triangle(_)) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_cuboid_triangle,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            _ => (PrimitiveContactGenerator::default(), None),
        }
    }

    fn dispatch(
        &self,
        shape1: &Shape,
        shape2: &Shape,
    ) -> (ContactPhase, Option<Box<dyn Any + Send + Sync>>) {
        match (shape1, shape2) {
            (Shape::Trimesh(_), _) | (_, Shape::Trimesh(_)) => (
                ContactPhase::NearPhase(ContactGenerator {
                    generate_contacts: super::generate_contacts_trimesh_shape,
                    ..ContactGenerator::default()
                }),
                Some(Box::new(TrimeshShapeContactGeneratorWorkspace::new())),
            ),
            (Shape::HeightField(_), _) | (_, Shape::HeightField(_)) => (
                ContactPhase::NearPhase(ContactGenerator {
                    generate_contacts: super::generate_contacts_heightfield_shape,
                    ..ContactGenerator::default()
                }),
                Some(Box::new(HeightFieldShapeContactGeneratorWorkspace::new())),
            ),
            _ => {
                let (gen, workspace) = self.dispatch_primitives(shape1, shape2);
                (ContactPhase::ExactPhase(gen), workspace)
            }
        }
    }
}