1use na::RealField;
23use crate::bounding_volume::{BoundingVolume, AABB};
4use crate::math::Isometry;
5use crate::pipeline::broad_phase::{BroadPhase, BroadPhaseProxyHandle, DBVTBroadPhase};
6use crate::pipeline::narrow_phase::{
7 CollisionObjectGraphIndex, DefaultContactDispatcher, DefaultProximityDispatcher,
8 InteractionGraph, NarrowPhase,
9};
10use crate::pipeline::object::{CollisionObjectHandle, GeometricQueryType};
11use crate::shape::Shape;
1213/// Registers a collision object handle so it can be taken into acconut by the broad-phase and the narrow-phase.
14///
15/// This tells the broad-phase and the interaction graph exists and allow them to perform some setup to accomodate
16/// for a new collision object with the given `handle`, and known to currently have the given `position`, `shape` and `query_type`.
17/// The result of this registration is a pair of handles that must be stored by the user as the will be needed for various
18/// queries (on the broad-phase and interaction graph), as well as for freeing (using `remove_proxies`) the resources allocated here.
19pub fn create_proxies<'a, N: RealField + Copy, Handle: CollisionObjectHandle>(
20 handle: Handle,
21 broad_phase: &mut (impl BroadPhase<N, AABB<N>, Handle> + ?Sized),
22 interactions: &mut InteractionGraph<N, Handle>,
23 position: &Isometry<N>,
24 shape: &(impl Shape<N> + ?Sized),
25 query_type: GeometricQueryType<N>,
26) -> (BroadPhaseProxyHandle, CollisionObjectGraphIndex) {
27let mut aabb = shape.aabb(position);
28 aabb.loosen(query_type.query_limit());
2930let proxy_handle = broad_phase.create_proxy(aabb, handle);
31let graph_index = interactions.add_node(handle);
3233 (proxy_handle, graph_index)
34}
3536/// Free all the resources allocated by the broad-phase and the interaction graph for the given proxy handles.
37///
38/// This will free the resoruces allocated by the `create_proxies` function. A special care should be
39/// taken with the return value of this function.
40///
41/// # Return (important, please read)
42///
43/// Thus function either returns `None` or a pair composed of a collision object handle and a collision object graph index.
44/// If `None` is returned, no extra work is required from the caller. If `Some` is returned, the it is necessary that
45/// the collision object graph index of the collider identified by the returned handle is replaced by the returned
46/// collision object graph index. This is caused by the fact that the indices used by the interaction graph to identify
47/// collision objects are not stable wrt. the deletion of collision objects. Therefore, removing one collision object can
48/// result in the collision object graph index of another collision object to be changed.
49#[must_use = "The graph index of the collision object returned by this method has been changed to the returned graph index."]
50pub fn remove_proxies<'a, N: RealField + Copy, Handle: CollisionObjectHandle>(
51 broad_phase: &mut (impl BroadPhase<N, AABB<N>, Handle> + ?Sized),
52 interactions: &mut InteractionGraph<N, Handle>,
53 proxy_handle: BroadPhaseProxyHandle,
54 graph_index: CollisionObjectGraphIndex,
55) -> Option<(Handle, CollisionObjectGraphIndex)> {
56// NOTE: no need to notify handle the removed pairs because the node
57 // will be removed from the interaction graph anyway.
58broad_phase.remove(&[proxy_handle], &mut |_, _| {});
59 interactions
60 .remove_node(graph_index)
61 .map(|h| (h, graph_index))
62}
6364/// Allocate a default narrow-phase, configured with the default contact and proximity dispatchers.
65pub fn default_narrow_phase<N: RealField + Copy, Handle: CollisionObjectHandle>(
66) -> NarrowPhase<N, Handle> {
67let coll_dispatcher = Box::new(DefaultContactDispatcher::new());
68let prox_dispatcher = Box::new(DefaultProximityDispatcher::new());
69 NarrowPhase::new(coll_dispatcher, prox_dispatcher)
70}
7172/// Allocate a default broad-phase, configured with a default coherence margin (set to 0.01).
73pub fn default_broad_phase<N: RealField + Copy, Handle: CollisionObjectHandle>(
74) -> DBVTBroadPhase<N, AABB<N>, Handle> {
75let default_margin = 0.01f64;
76 DBVTBroadPhase::new(na::convert(default_margin))
77}
7879/// Allocate a default interaction graph.
80pub fn default_interaction_graph<N: RealField + Copy, Handle: CollisionObjectHandle>(
81) -> InteractionGraph<N, Handle> {
82 InteractionGraph::new()
83}