rapier3d/dynamics/joint/impulse_joint/
impulse_joint_set.rs

1use parry::utils::hashset::HashSet;
2
3use super::ImpulseJoint;
4use crate::geometry::{InteractionGraph, RigidBodyGraphIndex, TemporaryInteractionIndex};
5
6use crate::data::Coarena;
7use crate::data::arena::Arena;
8use crate::dynamics::{GenericJoint, IslandManager, RigidBodyHandle, RigidBodySet};
9
10/// The unique identifier of a joint added to the joint set.
11/// The unique identifier of a collider added to a collider set.
12#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
14#[repr(transparent)]
15pub struct ImpulseJointHandle(pub crate::data::arena::Index);
16
17impl ImpulseJointHandle {
18    /// Converts this handle into its (index, generation) components.
19    pub fn into_raw_parts(self) -> (u32, u32) {
20        self.0.into_raw_parts()
21    }
22
23    /// Reconstructs an handle from its (index, generation) components.
24    pub fn from_raw_parts(id: u32, generation: u32) -> Self {
25        Self(crate::data::arena::Index::from_raw_parts(id, generation))
26    }
27
28    /// An always-invalid joint handle.
29    pub fn invalid() -> Self {
30        Self(crate::data::arena::Index::from_raw_parts(
31            crate::INVALID_U32,
32            crate::INVALID_U32,
33        ))
34    }
35}
36
37pub(crate) type JointIndex = usize;
38pub(crate) type JointGraphEdge = crate::data::graph::Edge<ImpulseJoint>;
39
40#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
41#[derive(Clone, Default, Debug)]
42/// A set of impulse_joints that can be handled by a physics `World`.
43pub struct ImpulseJointSet {
44    rb_graph_ids: Coarena<RigidBodyGraphIndex>,
45    /// Map joint handles to edge ids on the graph.
46    joint_ids: Arena<TemporaryInteractionIndex>,
47    joint_graph: InteractionGraph<RigidBodyHandle, ImpulseJoint>,
48    /// A set of rigid-body handles to wake-up during the next timestep.
49    pub(crate) to_wake_up: HashSet<RigidBodyHandle>,
50}
51
52impl ImpulseJointSet {
53    /// Creates a new empty set of impulse_joints.
54    pub fn new() -> Self {
55        Self {
56            rb_graph_ids: Coarena::new(),
57            joint_ids: Arena::new(),
58            joint_graph: InteractionGraph::new(),
59            to_wake_up: HashSet::default(),
60        }
61    }
62
63    /// The number of impulse_joints on this set.
64    pub fn len(&self) -> usize {
65        self.joint_graph.graph.edges.len()
66    }
67
68    /// `true` if there are no impulse_joints in this set.
69    pub fn is_empty(&self) -> bool {
70        self.joint_graph.graph.edges.is_empty()
71    }
72
73    /// Retrieve the joint graph where edges are impulse_joints and nodes are rigid body handles.
74    pub fn joint_graph(&self) -> &InteractionGraph<RigidBodyHandle, ImpulseJoint> {
75        &self.joint_graph
76    }
77
78    /// Iterates through all the joints between two rigid-bodies.
79    pub fn joints_between(
80        &self,
81        body1: RigidBodyHandle,
82        body2: RigidBodyHandle,
83    ) -> impl Iterator<Item = (ImpulseJointHandle, &ImpulseJoint)> {
84        self.rb_graph_ids
85            .get(body1.0)
86            .zip(self.rb_graph_ids.get(body2.0))
87            .into_iter()
88            .flat_map(move |(id1, id2)| self.joint_graph.interaction_pair(*id1, *id2).into_iter())
89            .map(|inter| (inter.2.handle, inter.2))
90    }
91
92    /// Iterates through all the impulse joints attached to the given rigid-body.
93    pub fn attached_joints(
94        &self,
95        body: RigidBodyHandle,
96    ) -> impl Iterator<
97        Item = (
98            RigidBodyHandle,
99            RigidBodyHandle,
100            ImpulseJointHandle,
101            &ImpulseJoint,
102        ),
103    > {
104        self.rb_graph_ids
105            .get(body.0)
106            .into_iter()
107            .flat_map(move |id| self.joint_graph.interactions_with(*id))
108            .map(|inter| (inter.0, inter.1, inter.2.handle, inter.2))
109    }
110
111    /// Iterates through all the impulse joints attached to the given rigid-body.
112    pub fn map_attached_joints_mut(
113        &mut self,
114        body: RigidBodyHandle,
115        mut f: impl FnMut(RigidBodyHandle, RigidBodyHandle, ImpulseJointHandle, &mut ImpulseJoint),
116    ) {
117        self.rb_graph_ids.get(body.0).into_iter().for_each(|id| {
118            for inter in self.joint_graph.interactions_with_mut(*id) {
119                (f)(inter.0, inter.1, inter.3.handle, inter.3)
120            }
121        })
122    }
123
124    /// Iterates through all the enabled impulse joints attached to the given rigid-body.
125    pub fn attached_enabled_joints(
126        &self,
127        body: RigidBodyHandle,
128    ) -> impl Iterator<
129        Item = (
130            RigidBodyHandle,
131            RigidBodyHandle,
132            ImpulseJointHandle,
133            &ImpulseJoint,
134        ),
135    > {
136        self.attached_joints(body)
137            .filter(|inter| inter.3.data.is_enabled())
138    }
139
140    /// Is the given joint handle valid?
141    pub fn contains(&self, handle: ImpulseJointHandle) -> bool {
142        self.joint_ids.contains(handle.0)
143    }
144
145    /// Gets the joint with the given handle.
146    pub fn get(&self, handle: ImpulseJointHandle) -> Option<&ImpulseJoint> {
147        let id = self.joint_ids.get(handle.0)?;
148        self.joint_graph.graph.edge_weight(*id)
149    }
150
151    /// Gets a mutable reference to the joint with the given handle.
152    pub fn get_mut(
153        &mut self,
154        handle: ImpulseJointHandle,
155        wake_up_connected_bodies: bool,
156    ) -> Option<&mut ImpulseJoint> {
157        let id = self.joint_ids.get(handle.0)?;
158        let joint = self.joint_graph.graph.edge_weight_mut(*id);
159        if wake_up_connected_bodies {
160            if let Some(joint) = &joint {
161                self.to_wake_up.insert(joint.body1);
162                self.to_wake_up.insert(joint.body2);
163            }
164        }
165        joint
166    }
167
168    /// Gets the joint with the given handle without a known generation.
169    ///
170    /// This is useful when you know you want the joint at index `i` but
171    /// don't know what is its current generation number. Generation numbers are
172    /// used to protect from the ABA problem because the joint position `i`
173    /// are recycled between two insertion and a removal.
174    ///
175    /// Using this is discouraged in favor of `self.get(handle)` which does not
176    /// suffer form the ABA problem.
177    pub fn get_unknown_gen(&self, i: u32) -> Option<(&ImpulseJoint, ImpulseJointHandle)> {
178        let (id, handle) = self.joint_ids.get_unknown_gen(i)?;
179        Some((
180            self.joint_graph.graph.edge_weight(*id)?,
181            ImpulseJointHandle(handle),
182        ))
183    }
184
185    /// Gets a mutable reference to the joint with the given handle without a known generation.
186    ///
187    /// This is useful when you know you want the joint at position `i` but
188    /// don't know what is its current generation number. Generation numbers are
189    /// used to protect from the ABA problem because the joint position `i`
190    /// are recycled between two insertion and a removal.
191    ///
192    /// Using this is discouraged in favor of `self.get_mut(handle)` which does not
193    /// suffer form the ABA problem.
194    pub fn get_unknown_gen_mut(
195        &mut self,
196        i: u32,
197    ) -> Option<(&mut ImpulseJoint, ImpulseJointHandle)> {
198        let (id, handle) = self.joint_ids.get_unknown_gen(i)?;
199        Some((
200            self.joint_graph.graph.edge_weight_mut(*id)?,
201            ImpulseJointHandle(handle),
202        ))
203    }
204
205    /// Iterates through all the joint on this set.
206    pub fn iter(&self) -> impl Iterator<Item = (ImpulseJointHandle, &ImpulseJoint)> {
207        self.joint_graph
208            .graph
209            .edges
210            .iter()
211            .map(|e| (e.weight.handle, &e.weight))
212    }
213
214    /// Iterates mutably through all the joint on this set.
215    pub fn iter_mut(&mut self) -> impl Iterator<Item = (ImpulseJointHandle, &mut ImpulseJoint)> {
216        self.joint_graph
217            .graph
218            .edges
219            .iter_mut()
220            .map(|e| (e.weight.handle, &mut e.weight))
221    }
222
223    // /// The set of impulse_joints as an array.
224    // pub(crate) fn impulse_joints(&self) -> &[JointGraphEdge] {
225    //     // self.joint_graph
226    //     //     .graph
227    //     //     .edges
228    //     //     .iter_mut()
229    //     //     .map(|e| &mut e.weight)
230    // }
231
232    // #[cfg(not(feature = "parallel"))]
233    #[allow(dead_code)] // That will likely be useful when we re-introduce intra-island parallelism.
234    pub(crate) fn joints_mut(&mut self) -> &mut [JointGraphEdge] {
235        &mut self.joint_graph.graph.edges[..]
236    }
237
238    #[cfg(feature = "parallel")]
239    pub(crate) fn joints_vec_mut(&mut self) -> &mut Vec<JointGraphEdge> {
240        &mut self.joint_graph.graph.edges
241    }
242
243    /// Inserts a new joint into this set and retrieve its handle.
244    ///
245    /// If `wake_up` is set to `true`, then the bodies attached to this joint will be
246    /// automatically woken up during the next timestep.
247    #[profiling::function]
248    pub fn insert(
249        &mut self,
250        body1: RigidBodyHandle,
251        body2: RigidBodyHandle,
252        data: impl Into<GenericJoint>,
253        wake_up: bool,
254    ) -> ImpulseJointHandle {
255        let data = data.into();
256        let handle = self.joint_ids.insert(0.into());
257        let joint = ImpulseJoint {
258            body1,
259            body2,
260            data,
261            impulses: na::zero(),
262            handle: ImpulseJointHandle(handle),
263        };
264
265        let default_id = InteractionGraph::<(), ()>::invalid_graph_index();
266        let mut graph_index1 = *self
267            .rb_graph_ids
268            .ensure_element_exist(joint.body1.0, default_id);
269        let mut graph_index2 = *self
270            .rb_graph_ids
271            .ensure_element_exist(joint.body2.0, default_id);
272
273        // NOTE: the body won't have a graph index if it does not
274        // have any joint attached.
275        if !InteractionGraph::<RigidBodyHandle, ImpulseJoint>::is_graph_index_valid(graph_index1) {
276            graph_index1 = self.joint_graph.graph.add_node(joint.body1);
277            self.rb_graph_ids.insert(joint.body1.0, graph_index1);
278        }
279
280        if !InteractionGraph::<RigidBodyHandle, ImpulseJoint>::is_graph_index_valid(graph_index2) {
281            graph_index2 = self.joint_graph.graph.add_node(joint.body2);
282            self.rb_graph_ids.insert(joint.body2.0, graph_index2);
283        }
284
285        self.joint_ids[handle] = self.joint_graph.add_edge(graph_index1, graph_index2, joint);
286
287        if wake_up {
288            self.to_wake_up.insert(body1);
289            self.to_wake_up.insert(body2);
290        }
291
292        ImpulseJointHandle(handle)
293    }
294
295    /// Retrieve all the enabled impulse joints happening between two active bodies.
296    // NOTE: this is very similar to the code from NarrowPhase::select_active_interactions.
297    pub(crate) fn select_active_interactions(
298        &self,
299        islands: &IslandManager,
300        bodies: &RigidBodySet,
301        out: &mut [Vec<JointIndex>],
302    ) {
303        for out_island in &mut out[..islands.num_islands()] {
304            out_island.clear();
305        }
306
307        // FIXME: don't iterate through all the interactions.
308        for (i, edge) in self.joint_graph.graph.edges.iter().enumerate() {
309            let joint = &edge.weight;
310            let rb1 = &bodies[joint.body1];
311            let rb2 = &bodies[joint.body2];
312
313            if joint.data.is_enabled()
314                && (rb1.is_dynamic() || rb2.is_dynamic())
315                && (!rb1.is_dynamic() || !rb1.is_sleeping())
316                && (!rb2.is_dynamic() || !rb2.is_sleeping())
317            {
318                let island_index = if !rb1.is_dynamic() {
319                    rb2.ids.active_island_id
320                } else {
321                    rb1.ids.active_island_id
322                };
323
324                out[island_index].push(i);
325            }
326        }
327    }
328
329    /// Removes a joint from this set.
330    ///
331    /// If `wake_up` is set to `true`, then the bodies attached to this joint will be
332    /// automatically woken up.
333    #[profiling::function]
334    pub fn remove(&mut self, handle: ImpulseJointHandle, wake_up: bool) -> Option<ImpulseJoint> {
335        let id = self.joint_ids.remove(handle.0)?;
336        let endpoints = self.joint_graph.graph.edge_endpoints(id)?;
337
338        if wake_up {
339            if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.0) {
340                self.to_wake_up.insert(*rb_handle);
341            }
342            if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.1) {
343                self.to_wake_up.insert(*rb_handle);
344            }
345        }
346
347        let removed_joint = self.joint_graph.graph.remove_edge(id);
348
349        if let Some(edge) = self.joint_graph.graph.edge_weight(id) {
350            self.joint_ids[edge.handle.0] = id;
351        }
352
353        removed_joint
354    }
355
356    /// Deletes all the impulse_joints attached to the given rigid-body.
357    ///
358    /// The provided rigid-body handle is not required to identify a rigid-body that
359    /// is still contained by the `bodies` component set.
360    /// Returns the (now invalid) handles of the removed impulse_joints.
361    #[profiling::function]
362    pub fn remove_joints_attached_to_rigid_body(
363        &mut self,
364        handle: RigidBodyHandle,
365    ) -> Vec<ImpulseJointHandle> {
366        let mut deleted = vec![];
367
368        if let Some(deleted_id) = self
369            .rb_graph_ids
370            .remove(handle.0, InteractionGraph::<(), ()>::invalid_graph_index())
371        {
372            if InteractionGraph::<(), ()>::is_graph_index_valid(deleted_id) {
373                // We have to delete each joint one by one in order to:
374                // - Wake-up the attached bodies.
375                // - Update our Handle -> graph edge mapping.
376                // Delete the node.
377                let to_delete: Vec<_> = self
378                    .joint_graph
379                    .interactions_with(deleted_id)
380                    .map(|e| (e.0, e.1, e.2.handle))
381                    .collect();
382                for (h1, h2, to_delete_handle) in to_delete {
383                    deleted.push(to_delete_handle);
384                    let to_delete_edge_id = self.joint_ids.remove(to_delete_handle.0).unwrap();
385                    self.joint_graph.graph.remove_edge(to_delete_edge_id);
386
387                    // Update the id of the edge which took the place of the deleted one.
388                    if let Some(j) = self.joint_graph.graph.edge_weight_mut(to_delete_edge_id) {
389                        self.joint_ids[j.handle.0] = to_delete_edge_id;
390                    }
391
392                    // Wake up the attached bodies.
393                    self.to_wake_up.insert(h1);
394                    self.to_wake_up.insert(h2);
395                }
396
397                if let Some(other) = self.joint_graph.remove_node(deleted_id) {
398                    // One rigid-body joint graph index may have been invalidated
399                    // so we need to update it.
400                    self.rb_graph_ids.insert(other.0, deleted_id);
401                }
402            }
403        }
404
405        deleted
406    }
407}