ncollide3d/query/contact/
contact_shape_shape.rs

1use na::RealField;
2
3use crate::math::{Isometry, Point};
4use crate::query::{self, Contact};
5use crate::shape::{Ball, Plane, Shape};
6
7/// Computes one contact point between two shapes.
8///
9/// Returns `None` if the objects are separated by a distance greater than `prediction`.
10pub fn contact<N: RealField + Copy>(
11    m1: &Isometry<N>,
12    g1: &dyn Shape<N>,
13    m2: &Isometry<N>,
14    g2: &dyn Shape<N>,
15    prediction: N,
16) -> Option<Contact<N>> {
17    let ball1 = g1.as_shape::<Ball<N>>();
18    let ball2 = g2.as_shape::<Ball<N>>();
19
20    if let (Some(b1), Some(b2)) = (ball1, ball2) {
21        let p1 = Point::from(m1.translation.vector);
22        let p2 = Point::from(m2.translation.vector);
23
24        query::contact_ball_ball(&p1, b1, &p2, b2, prediction)
25    } else if let (Some(p1), Some(s2)) = (g1.as_shape::<Plane<N>>(), g2.as_support_map()) {
26        query::contact_plane_support_map(m1, p1, m2, s2, prediction)
27    } else if let (Some(s1), Some(p2)) = (g1.as_support_map(), g2.as_shape::<Plane<N>>()) {
28        query::contact_support_map_plane(m1, s1, m2, p2, prediction)
29    } else if let (Some(b1), (Some(_), Some(_))) =
30        (ball1, (g2.as_convex_polyhedron(), g2.as_point_query()))
31    {
32        let p1 = Point::from(m1.translation.vector);
33        query::contact_ball_convex_polyhedron(&p1, b1, m2, g2, prediction)
34    } else if let ((Some(_), Some(_)), Some(b2)) =
35        ((g1.as_convex_polyhedron(), g1.as_point_query()), ball2)
36    {
37        let p2 = Point::from(m2.translation.vector);
38        query::contact_convex_polyhedron_ball(m1, g1, &p2, b2, prediction)
39    } else if let (Some(s1), Some(s2)) = (g1.as_support_map(), g2.as_support_map()) {
40        query::contact_support_map_support_map(m1, s1, m2, s2, prediction)
41    } else if let Some(c1) = g1.as_composite_shape() {
42        query::contact_composite_shape_shape(m1, c1, m2, g2, prediction)
43    } else if let Some(c2) = g2.as_composite_shape() {
44        query::contact_shape_composite_shape(m1, g1, m2, c2, prediction)
45    } else {
46        panic!("No algorithm known to compute a contact point between the given pair of shapes.")
47    }
48}