parry3d/query/contact/
contact.rs

1use crate::math::{Isometry, Point, Real, Vector};
2use core::mem;
3use na::{self, Unit};
4
5#[cfg(feature = "rkyv")]
6use rkyv::{bytecheck, CheckBytes};
7
8/// Geometric description of a contact.
9#[derive(Debug, PartialEq, Copy, Clone)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[cfg_attr(
12    feature = "rkyv",
13    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, CheckBytes),
14    archive(as = "Self")
15)]
16pub struct Contact {
17    /// Position of the contact on the first object.
18    pub point1: Point<Real>,
19
20    /// Position of the contact on the second object.
21    pub point2: Point<Real>,
22
23    /// Contact normal, pointing towards the exterior of the first shape.
24    pub normal1: Unit<Vector<Real>>,
25
26    /// Contact normal, pointing towards the exterior of the second shape.
27    ///
28    /// If these contact data are expressed in world-space, this normal is equal to `-normal1`.
29    pub normal2: Unit<Vector<Real>>,
30
31    /// Distance between the two contact points.
32    ///
33    /// If this is negative, this contact represents a penetration.
34    pub dist: Real,
35}
36
37impl Contact {
38    /// Creates a new contact.
39    #[inline]
40    pub fn new(
41        point1: Point<Real>,
42        point2: Point<Real>,
43        normal1: Unit<Vector<Real>>,
44        normal2: Unit<Vector<Real>>,
45        dist: Real,
46    ) -> Self {
47        Contact {
48            point1,
49            point2,
50            normal1,
51            normal2,
52            dist,
53        }
54    }
55}
56
57impl Contact {
58    /// Swaps the points and normals of this contact.
59    #[inline]
60    pub fn flip(&mut self) {
61        mem::swap(&mut self.point1, &mut self.point2);
62        mem::swap(&mut self.normal1, &mut self.normal2);
63    }
64
65    /// Returns a new contact containing the swapped points and normals of `self`.
66    #[inline]
67    pub fn flipped(mut self) -> Self {
68        self.flip();
69        self
70    }
71
72    /// Transform the points and normals from this contact by
73    /// the given transformations.
74    #[inline]
75    pub fn transform_by_mut(&mut self, pos1: &Isometry<Real>, pos2: &Isometry<Real>) {
76        self.point1 = pos1 * self.point1;
77        self.point2 = pos2 * self.point2;
78        self.normal1 = pos1 * self.normal1;
79        self.normal2 = pos2 * self.normal2;
80    }
81
82    /// Transform `self.point1` and `self.normal1` by the `pos`.
83    pub fn transform1_by_mut(&mut self, pos: &Isometry<Real>) {
84        self.point1 = pos * self.point1;
85        self.normal1 = pos * self.normal1;
86    }
87}