rapier3d/geometry/
mod.rs

1//! Structures related to geometry: colliders, shapes, etc.
2
3pub use self::broad_phase::BroadPhase;
4pub use self::broad_phase_bvh::{BroadPhaseBvh, BvhOptimizationStrategy};
5pub use self::broad_phase_pair_event::{BroadPhasePairEvent, ColliderPair};
6pub use self::collider::{Collider, ColliderBuilder};
7pub use self::collider_components::*;
8pub use self::collider_set::ColliderSet;
9pub use self::contact_pair::{
10    ContactData, ContactManifoldData, ContactPair, IntersectionPair, SolverContact, SolverFlags,
11};
12pub use self::interaction_graph::{
13    ColliderGraphIndex, InteractionGraph, RigidBodyGraphIndex, TemporaryInteractionIndex,
14};
15pub use self::interaction_groups::{Group, InteractionGroups};
16pub use self::mesh_converter::{MeshConverter, MeshConverterError};
17pub use self::narrow_phase::NarrowPhase;
18
19pub use parry::bounding_volume::BoundingVolume;
20pub use parry::partitioning::{Bvh, BvhBuildStrategy};
21pub use parry::query::{PointQuery, PointQueryWithLocation, RayCast, TrackedContact};
22pub use parry::shape::{SharedShape, VoxelState, VoxelType, Voxels};
23
24use crate::math::{Real, Vector};
25
26/// A contact between two colliders.
27pub type Contact = parry::query::TrackedContact<ContactData>;
28/// A contact manifold between two colliders.
29pub type ContactManifold = parry::query::ContactManifold<ContactManifoldData, ContactData>;
30/// A segment shape.
31pub type Segment = parry::shape::Segment;
32/// A cuboid shape.
33pub type Cuboid = parry::shape::Cuboid;
34/// A triangle shape.
35pub type Triangle = parry::shape::Triangle;
36/// A ball shape.
37pub type Ball = parry::shape::Ball;
38/// A capsule shape.
39pub type Capsule = parry::shape::Capsule;
40/// A heightfield shape.
41pub type HeightField = parry::shape::HeightField;
42/// A cylindrical shape.
43#[cfg(feature = "dim3")]
44pub type Cylinder = parry::shape::Cylinder;
45/// A cone shape.
46#[cfg(feature = "dim3")]
47pub type Cone = parry::shape::Cone;
48/// An axis-aligned bounding box.
49pub type Aabb = parry::bounding_volume::Aabb;
50/// A ray that can be cast against colliders.
51pub type Ray = parry::query::Ray;
52/// The intersection between a ray and a  collider.
53pub type RayIntersection = parry::query::RayIntersection;
54/// The projection of a point on a collider.
55pub type PointProjection = parry::query::PointProjection;
56/// The result of a shape-cast between two shapes.
57pub type ShapeCastHit = parry::query::ShapeCastHit;
58/// The default broad-phase implementation recommended for general-purpose usage.
59pub type DefaultBroadPhase = BroadPhaseBvh;
60
61bitflags::bitflags! {
62    /// Flags providing more information regarding a collision event.
63    #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
64    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
65    pub struct CollisionEventFlags: u32 {
66        /// Flag set if at least one of the colliders involved in the
67        /// collision was a sensor when the event was fired.
68        const SENSOR = 0b0001;
69        /// Flag set if a `CollisionEvent::Stopped` was fired because
70        /// at least one of the colliders was removed.
71        const REMOVED = 0b0010;
72    }
73}
74
75#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
76#[derive(Copy, Clone, Hash, Debug)]
77/// Events occurring when two colliders start or stop colliding
78pub enum CollisionEvent {
79    /// Event occurring when two colliders start colliding
80    Started(ColliderHandle, ColliderHandle, CollisionEventFlags),
81    /// Event occurring when two colliders stop colliding.
82    Stopped(ColliderHandle, ColliderHandle, CollisionEventFlags),
83}
84
85impl CollisionEvent {
86    /// Is this a `Started` collision event?
87    pub fn started(self) -> bool {
88        matches!(self, CollisionEvent::Started(..))
89    }
90
91    /// Is this a `Stopped` collision event?
92    pub fn stopped(self) -> bool {
93        matches!(self, CollisionEvent::Stopped(..))
94    }
95
96    /// The handle of the first collider involved in this collision event.
97    pub fn collider1(self) -> ColliderHandle {
98        match self {
99            Self::Started(h, _, _) | Self::Stopped(h, _, _) => h,
100        }
101    }
102
103    /// The handle of the second collider involved in this collision event.
104    pub fn collider2(self) -> ColliderHandle {
105        match self {
106            Self::Started(_, h, _) | Self::Stopped(_, h, _) => h,
107        }
108    }
109
110    /// Was at least one of the colliders involved in the collision a sensor?
111    pub fn sensor(self) -> bool {
112        match self {
113            Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
114                f.contains(CollisionEventFlags::SENSOR)
115            }
116        }
117    }
118
119    /// Was at least one of the colliders involved in the collision removed?
120    pub fn removed(self) -> bool {
121        match self {
122            Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
123                f.contains(CollisionEventFlags::REMOVED)
124            }
125        }
126    }
127}
128
129#[derive(Copy, Clone, PartialEq, Debug, Default)]
130/// Event occurring when the sum of the magnitudes of the contact forces
131/// between two colliders exceed a threshold.
132pub struct ContactForceEvent {
133    /// The first collider involved in the contact.
134    pub collider1: ColliderHandle,
135    /// The second collider involved in the contact.
136    pub collider2: ColliderHandle,
137    /// The sum of all the forces between the two colliders.
138    pub total_force: Vector<Real>,
139    /// The sum of the magnitudes of each force between the two colliders.
140    ///
141    /// Note that this is **not** the same as the magnitude of `self.total_force`.
142    /// Here we are summing the magnitude of all the forces, instead of taking
143    /// the magnitude of their sum.
144    pub total_force_magnitude: Real,
145    /// The world-space (unit) direction of the force with strongest magnitude.
146    pub max_force_direction: Vector<Real>,
147    /// The magnitude of the largest force at a contact point of this contact pair.
148    pub max_force_magnitude: Real,
149}
150
151impl ContactForceEvent {
152    /// Init a contact force event from a contact pair.
153    pub fn from_contact_pair(dt: Real, pair: &ContactPair, total_force_magnitude: Real) -> Self {
154        let mut result = ContactForceEvent {
155            collider1: pair.collider1,
156            collider2: pair.collider2,
157            total_force_magnitude,
158            ..ContactForceEvent::default()
159        };
160
161        for m in &pair.manifolds {
162            let mut total_manifold_impulse = 0.0;
163            for pt in m.contacts() {
164                total_manifold_impulse += pt.data.impulse;
165
166                if pt.data.impulse > result.max_force_magnitude {
167                    result.max_force_magnitude = pt.data.impulse;
168                    result.max_force_direction = m.data.normal;
169                }
170            }
171
172            result.total_force += m.data.normal * total_manifold_impulse;
173        }
174
175        let inv_dt = crate::utils::inv(dt);
176        // NOTE: convert impulses to forces. Note that we
177        //       don’t need to convert the `total_force_magnitude`
178        //       because it’s an input of this function already
179        //       assumed to be a force instead of an impulse.
180        result.total_force *= inv_dt;
181        result.max_force_magnitude *= inv_dt;
182        result
183    }
184}
185
186pub(crate) use self::collider_set::ModifiedColliders;
187pub(crate) use self::narrow_phase::ContactManifoldIndex;
188pub use parry::shape::*;
189
190#[cfg(feature = "serde-serialize")]
191pub(crate) fn default_persistent_query_dispatcher()
192-> std::sync::Arc<dyn parry::query::PersistentQueryDispatcher<ContactManifoldData, ContactData>> {
193    std::sync::Arc::new(parry::query::DefaultQueryDispatcher)
194}
195
196mod collider_components;
197mod contact_pair;
198mod interaction_graph;
199mod interaction_groups;
200mod narrow_phase;
201
202mod broad_phase;
203mod broad_phase_bvh;
204mod broad_phase_pair_event;
205mod collider;
206mod collider_set;
207mod mesh_converter;