rapier3d/pipeline/
physics_hooks.rs

1use crate::dynamics::{RigidBodyHandle, RigidBodySet};
2use crate::geometry::{ColliderHandle, ColliderSet, ContactManifold, SolverContact, SolverFlags};
3use crate::math::{Real, Vector};
4use na::ComplexField;
5
6/// Context given to custom collision filters to filter-out collisions.
7pub struct PairFilterContext<'a> {
8    /// The set of rigid-bodies.
9    pub bodies: &'a RigidBodySet,
10    /// The set of colliders.
11    pub colliders: &'a ColliderSet,
12    /// The handle of the first collider involved in the potential collision.
13    pub collider1: ColliderHandle,
14    /// The handle of the first collider involved in the potential collision.
15    pub collider2: ColliderHandle,
16    /// The handle of the first body involved in the potential collision.
17    pub rigid_body1: Option<RigidBodyHandle>,
18    /// The handle of the first body involved in the potential collision.
19    pub rigid_body2: Option<RigidBodyHandle>,
20}
21
22/// Context given to custom contact modifiers to modify the contacts seen by the constraints solver.
23pub struct ContactModificationContext<'a> {
24    /// The set of rigid-bodies.
25    pub bodies: &'a RigidBodySet,
26    /// The set of colliders.
27    pub colliders: &'a ColliderSet,
28    /// The handle of the first collider involved in the potential collision.
29    pub collider1: ColliderHandle,
30    /// The handle of the first collider involved in the potential collision.
31    pub collider2: ColliderHandle,
32    /// The handle of the first body involved in the potential collision.
33    pub rigid_body1: Option<RigidBodyHandle>,
34    /// The handle of the first body involved in the potential collision.
35    pub rigid_body2: Option<RigidBodyHandle>,
36    /// The contact manifold.
37    pub manifold: &'a ContactManifold,
38    /// The solver contacts that can be modified.
39    pub solver_contacts: &'a mut Vec<SolverContact>,
40    /// The contact normal that can be modified.
41    pub normal: &'a mut Vector<Real>,
42    /// User-defined data attached to the manifold.
43    // NOTE: we keep this a &'a mut u32 to emphasize the
44    // fact that this can be modified.
45    pub user_data: &'a mut u32,
46}
47
48impl ContactModificationContext<'_> {
49    /// Helper function to update `self` to emulate a oneway-platform.
50    ///
51    /// The "oneway" behavior will only allow contacts between two colliders
52    /// if the local contact normal of the first collider involved in the contact
53    /// is almost aligned with the provided `allowed_local_n1` direction.
54    ///
55    /// To make this method work properly it must be called as part of the
56    /// `PhysicsHooks::modify_solver_contacts` method at each timestep, for each
57    /// contact manifold involving a one-way platform. The `self.user_data` field
58    /// must not be modified from the outside of this method.
59    pub fn update_as_oneway_platform(
60        &mut self,
61        allowed_local_n1: &Vector<Real>,
62        allowed_angle: Real,
63    ) {
64        const CONTACT_CONFIGURATION_UNKNOWN: u32 = 0;
65        const CONTACT_CURRENTLY_ALLOWED: u32 = 1;
66        const CONTACT_CURRENTLY_FORBIDDEN: u32 = 2;
67
68        let cang = ComplexField::cos(allowed_angle);
69
70        // Test the allowed normal with the local-space contact normal that
71        // points towards the exterior of context.collider1.
72        let contact_is_ok = self.manifold.local_n1.dot(allowed_local_n1) >= cang;
73
74        match *self.user_data {
75            CONTACT_CONFIGURATION_UNKNOWN => {
76                if contact_is_ok {
77                    // The contact is close enough to the allowed normal.
78                    *self.user_data = CONTACT_CURRENTLY_ALLOWED;
79                } else {
80                    // The contact normal isn't close enough to the allowed
81                    // normal, so remove all the contacts and mark further contacts
82                    // as forbidden.
83                    self.solver_contacts.clear();
84
85                    // NOTE: in some very rare cases `local_n1` will be
86                    // zero if the objects are exactly touching at one point.
87                    // So in this case we can't really conclude.
88                    // If the norm is non-zero, then we can tell we need to forbid
89                    // further contacts. Otherwise we have to wait for the next frame.
90                    if self.manifold.local_n1.norm_squared() > 0.1 {
91                        *self.user_data = CONTACT_CURRENTLY_FORBIDDEN;
92                    }
93                }
94            }
95            CONTACT_CURRENTLY_FORBIDDEN => {
96                // Contacts are forbidden so we need to continue forbidding contacts
97                // until all the contacts are non-penetrating again. In that case, if
98                // the contacts are OK with respect to the contact normal, then we can
99                // mark them as allowed.
100                if contact_is_ok && self.solver_contacts.iter().all(|c| c.dist > 0.0) {
101                    *self.user_data = CONTACT_CURRENTLY_ALLOWED;
102                } else {
103                    // Discard all the contacts.
104                    self.solver_contacts.clear();
105                }
106            }
107            CONTACT_CURRENTLY_ALLOWED => {
108                // We allow all the contacts right now. The configuration becomes
109                // uncertain again when the contact manifold no longer contains any contact.
110                if self.solver_contacts.is_empty() {
111                    *self.user_data = CONTACT_CONFIGURATION_UNKNOWN;
112                }
113            }
114            _ => unreachable!(),
115        }
116    }
117}
118
119bitflags::bitflags! {
120    #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
121    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
122    /// Flags affecting the behavior of the constraints solver for a given contact manifold.
123    pub struct ActiveHooks: u32 {
124        /// If set, Rapier will call `PhysicsHooks::filter_contact_pair` whenever relevant.
125        const FILTER_CONTACT_PAIRS = 0b0001;
126        /// If set, Rapier will call `PhysicsHooks::filter_intersection_pair` whenever relevant.
127        const FILTER_INTERSECTION_PAIR = 0b0010;
128        /// If set, Rapier will call `PhysicsHooks::modify_solver_contact` whenever relevant.
129        const MODIFY_SOLVER_CONTACTS = 0b0100;
130    }
131}
132impl Default for ActiveHooks {
133    fn default() -> Self {
134        ActiveHooks::empty()
135    }
136}
137
138// TODO: right now, the wasm version don't have the Send+Sync bounds.
139//       This is because these bounds are very difficult to fulfill if we want to
140//       call JS closures. Also, parallelism cannot be enabled for wasm targets, so
141//       not having Send+Sync isn't a problem.
142/// User-defined functions called by the physics engines during one timestep in order to customize its behavior.
143#[cfg(target_arch = "wasm32")]
144pub trait PhysicsHooks {
145    /// Applies the contact pair filter.
146    fn filter_contact_pair(&self, _context: &PairFilterContext) -> Option<SolverFlags> {
147        Some(SolverFlags::COMPUTE_IMPULSES)
148    }
149
150    /// Applies the intersection pair filter.
151    fn filter_intersection_pair(&self, _context: &PairFilterContext) -> bool {
152        true
153    }
154
155    /// Modifies the set of contacts seen by the constraints solver.
156    fn modify_solver_contacts(&self, _context: &mut ContactModificationContext) {}
157}
158
159/// User-defined functions called by the physics engines during one timestep in order to customize its behavior.
160#[cfg(not(target_arch = "wasm32"))]
161pub trait PhysicsHooks: Send + Sync {
162    /// Applies the contact pair filter.
163    ///
164    /// Note that this method will only be called if at least one of the colliders
165    /// involved in the contact contains the `ActiveHooks::FILTER_CONTACT_PAIRS` flags
166    /// in its physics hooks flags.
167    ///
168    /// User-defined filter for potential contact pairs detected by the broad-phase.
169    /// This can be used to apply custom logic in order to decide whether two colliders
170    /// should have their contact computed by the narrow-phase, and if these contact
171    /// should be solved by the constraints solver
172    ///
173    /// Note that using a contact pair filter will replace the default contact filtering
174    /// which consists of preventing contact computation between two non-dynamic bodies.
175    ///
176    /// This filtering method is called after taking into account the colliders collision groups.
177    ///
178    /// If this returns `None`, then the narrow-phase will ignore this contact pair and
179    /// not compute any contact manifolds for it.
180    /// If this returns `Some`, then the narrow-phase will compute contact manifolds for
181    /// this pair of colliders, and configure them with the returned solver flags. For
182    /// example, if this returns `Some(SolverFlags::COMPUTE_IMPULSES)` then the contacts
183    /// will be taken into account by the constraints solver. If this returns
184    /// `Some(SolverFlags::empty())` then the constraints solver will ignore these
185    /// contacts.
186    fn filter_contact_pair(&self, _context: &PairFilterContext) -> Option<SolverFlags> {
187        Some(SolverFlags::COMPUTE_IMPULSES)
188    }
189
190    /// Applies the intersection pair filter.
191    ///
192    /// Note that this method will only be called if at least one of the colliders
193    /// involved in the contact contains the `ActiveHooks::FILTER_INTERSECTION_PAIR` flags
194    /// in its physics hooks flags.
195    ///
196    /// User-defined filter for potential intersection pairs detected by the broad-phase.
197    ///
198    /// This can be used to apply custom logic in order to decide whether two colliders
199    /// should have their intersection computed by the narrow-phase.
200    ///
201    /// Note that using an intersection pair filter will replace the default intersection filtering
202    /// which consists of preventing intersection computation between two non-dynamic bodies.
203    ///
204    /// This filtering method is called after taking into account the colliders collision groups.
205    ///
206    /// If this returns `false`, then the narrow-phase will ignore this pair and
207    /// not compute any intersection information for it.
208    /// If this return `true` then the narrow-phase will compute intersection
209    /// information for this pair.
210    fn filter_intersection_pair(&self, _context: &PairFilterContext) -> bool {
211        true
212    }
213
214    /// Modifies the set of contacts seen by the constraints solver.
215    ///
216    /// Note that this method will only be called if at least one of the colliders
217    /// involved in the contact contains the `ActiveHooks::MODIFY_SOLVER_CONTACTS` flags
218    /// in its physics hooks flags.
219    ///
220    /// By default, the content of `solver_contacts` is computed from `manifold.points`.
221    /// This method will be called on each contact manifold which have the flag `SolverFlags::modify_solver_contacts` set.
222    /// This method can be used to modify the set of solver contacts seen by the constraints solver: contacts
223    /// can be removed and modified.
224    ///
225    /// Note that if all the contacts have to be ignored by the constraint solver, you may simply
226    /// do `context.solver_contacts.clear()`.
227    ///
228    /// Modifying the solver contacts allow you to achieve various effects, including:
229    /// - Simulating conveyor belts by setting the `surface_velocity` of a solver contact.
230    /// - Simulating shapes with multiply materials by modifying the friction and restitution
231    ///   coefficient depending of the features in contacts.
232    /// - Simulating one-way platforms depending on the contact normal.
233    ///
234    /// Each contact manifold is given a `u32` user-defined data that is persistent between
235    /// timesteps (as long as the contact manifold exists). This user-defined data is initialized
236    /// as 0 and can be modified in `context.user_data`.
237    ///
238    /// The world-space contact normal can be modified in `context.normal`.
239    fn modify_solver_contacts(&self, _context: &mut ContactModificationContext) {}
240}
241
242impl PhysicsHooks for () {
243    fn filter_contact_pair(&self, _context: &PairFilterContext) -> Option<SolverFlags> {
244        Some(SolverFlags::default())
245    }
246
247    fn filter_intersection_pair(&self, _: &PairFilterContext) -> bool {
248        true
249    }
250
251    fn modify_solver_contacts(&self, _: &mut ContactModificationContext) {}
252}