Skip to content

Commit 2ab588d

Browse files
authored
Rename Contacts to ContactPair (#685)
# Objective Especially after #683, we use the term "contact pair" a lot. However, the contact pair type is called `Contacts`. This name is very ambiguous: does it represent all contacts in the world, all contacts between two entities, contacts belonging to a specific contact surface, or something else? ## Solution Rename `Contacts` to `ContactPair`. It much more accurately describes what the type represents: contact data for a pair of entities that may be in contact. This is also the name used by Rapier. --- ## Migration Guide `Contacts` has been renamed to `ContactPair`.
1 parent e7d1a27 commit 2ab588d

File tree

13 files changed

+51
-48
lines changed

13 files changed

+51
-48
lines changed

crates/avian2d/examples/one_way_platform_2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl CollisionHooks for PlatformerCollisionHooks<'_, '_> {
244244
/// Even if an entity is changed to [`PassThroughOneWayPlatform::Never`], it will be allowed to pass
245245
/// through a [`OneWayPlatform`] if it is already penetrating the platform. Once it exits the platform,
246246
/// it will no longer be allowed to pass through.
247-
fn modify_contacts(&self, contacts: &mut Contacts, commands: &mut Commands) -> bool {
247+
fn modify_contacts(&self, contacts: &mut ContactPair, commands: &mut Commands) -> bool {
248248
// This is the contact modification hook, called after collision detection,
249249
// but before constraints are created for the solver. Mutable access to the ECS
250250
// is not allowed, but we can queue commands to perform deferred changes.

crates/avian3d/examples/conveyor_belt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ struct ConveyorHooks<'w, 's> {
3838

3939
// Implement the `CollisionHooks` trait for our custom system parameter.
4040
impl CollisionHooks for ConveyorHooks<'_, '_> {
41-
fn modify_contacts(&self, contact_pair: &mut Contacts, _commands: &mut Commands) -> bool {
41+
fn modify_contacts(&self, contacts: &mut ContactPair, _commands: &mut Commands) -> bool {
4242
// Get the conveyor belt and its global transform.
4343
// We don't know which entity is the conveyor belt, if any, so we need to check both.
4444
// This also affects the sign used for the conveyor belt's speed to apply it in the correct direction.
4545
let (Ok((conveyor_belt, global_transform)), sign) = self
4646
.conveyor_query
47-
.get(contact_pair.entity1)
48-
.map_or((self.conveyor_query.get(contact_pair.entity2), 1.0), |q| {
47+
.get(contacts.entity1)
48+
.map_or((self.conveyor_query.get(contacts.entity2), 1.0), |q| {
4949
(Ok(q), -1.0)
5050
})
5151
else {
@@ -59,7 +59,7 @@ impl CollisionHooks for ConveyorHooks<'_, '_> {
5959

6060
// Iterate over all contact surfaces between the conveyor belt and the other collider,
6161
// and apply a relative velocity to simulate the movement of the conveyor belt's surface.
62-
for manifold in contact_pair.manifolds.iter_mut() {
62+
for manifold in contacts.manifolds.iter_mut() {
6363
let tangent_velocity = sign * conveyor_belt.speed * direction;
6464
manifold.tangent_velocity = tangent_velocity.adjust_precision();
6565
}

crates/avian3d/examples/custom_broad_phase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn collect_collision_pairs(
9292
// The narrow phase will determine if the entities are touching and compute contact data.
9393
// NOTE: To handle sensors, collision hooks, and child colliders, you may need to configure
9494
// `flags` and other properties of the contact pair. This is not done here for simplicity.
95-
collisions.add_pair_with_key(Contacts::new(entity1, entity2), key);
95+
collisions.add_pair_with_key(ContactPair::new(entity1, entity2), key);
9696
}
9797
}
9898
}

src/collision/broad_phase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn sweep_and_prune<H: CollisionHooks>(
327327

328328
// Create a new contact pair as non-touching.
329329
// The narrow phase will determine if the entities are touching and compute contact data.
330-
let mut contacts = Contacts::new(*entity1, *entity2);
330+
let mut contacts = ContactPair::new(*entity1, *entity2);
331331

332332
// Initialize flags and other data for the contact pair.
333333
contacts.body_entity1 = Some(collider_of1.rigid_body);

src/collision/contact_types/contact_graph.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::data_structures::{
77
use crate::prelude::*;
88
use bevy::{platform_support::collections::HashSet, prelude::*};
99

10-
use super::{ContactPairFlags, Contacts};
10+
use super::{ContactPair, ContactPairFlags};
1111

1212
/// A resource that stores all contact pairs in the physics world
1313
/// in an [undirected graph](UnGraph).
@@ -70,7 +70,7 @@ use super::{ContactPairFlags, Contacts};
7070
pub struct ContactGraph {
7171
// TODO: We could have a separate intersection graph for sensors.
7272
/// The internal undirected graph where nodes are entities and edges are contact pairs.
73-
pub internal: UnGraph<Entity, Contacts>,
73+
pub internal: UnGraph<Entity, ContactPair>,
7474

7575
/// A set of all contact pairs for fast lookup.
7676
///
@@ -89,9 +89,9 @@ impl ContactGraph {
8989
/// If the pair does not exist, `None` is returned.
9090
///
9191
/// A contact pair exists between two entities if their [`ColliderAabb`]s intersect.
92-
/// Use [`Contacts::is_touching`] to determine if the actual collider shapes are touching.
92+
/// Use [`ContactPair::is_touching`] to determine if the actual collider shapes are touching.
9393
#[inline]
94-
pub fn get(&self, entity1: Entity, entity2: Entity) -> Option<&Contacts> {
94+
pub fn get(&self, entity1: Entity, entity2: Entity) -> Option<&ContactPair> {
9595
let (Some(&index1), Some(&index2)) = (
9696
self.entity_graph_index.get(entity1),
9797
self.entity_graph_index.get(entity2),
@@ -108,9 +108,9 @@ impl ContactGraph {
108108
/// If the pair does not exist, `None` is returned.
109109
///
110110
/// A contact pair exists between two entities if their [`ColliderAabb`]s intersect.
111-
/// Use [`Contacts::is_touching`] to determine if the actual collider shapes are touching.
111+
/// Use [`ContactPair::is_touching`] to determine if the actual collider shapes are touching.
112112
#[inline]
113-
pub fn get_mut(&mut self, entity1: Entity, entity2: Entity) -> Option<&mut Contacts> {
113+
pub fn get_mut(&mut self, entity1: Entity, entity2: Entity) -> Option<&mut ContactPair> {
114114
let (Some(&index1), Some(&index2)) = (
115115
self.entity_graph_index.get(entity1),
116116
self.entity_graph_index.get(entity2),
@@ -150,15 +150,15 @@ impl ContactGraph {
150150
///
151151
/// If you only want touching contacts, use [`iter_touching`](Self::iter_touching) instead.
152152
#[inline]
153-
pub fn iter(&self) -> impl Iterator<Item = &Contacts> {
153+
pub fn iter(&self) -> impl Iterator<Item = &ContactPair> {
154154
self.internal.all_edge_weights()
155155
}
156156

157157
/// Returns an iterator yielding immutable access to all contact pairs that are currently touching.
158158
///
159159
/// This is a subset of [`iter`](Self::iter) that only includes pairs where the colliders are touching.
160160
#[inline]
161-
pub fn iter_touching(&self) -> impl Iterator<Item = &Contacts> {
161+
pub fn iter_touching(&self) -> impl Iterator<Item = &ContactPair> {
162162
self.internal
163163
.all_edge_weights()
164164
.filter(|contacts| contacts.flags.contains(ContactPairFlags::TOUCHING))
@@ -171,15 +171,15 @@ impl ContactGraph {
171171
///
172172
/// If you only want touching contacts, use [`iter_touching_mut`](Self::iter_touching_mut) instead.
173173
#[inline]
174-
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Contacts> {
174+
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ContactPair> {
175175
self.internal.all_edge_weights_mut()
176176
}
177177

178178
/// Returns a iterator yielding mutable access to all contact pairs that are currently touching.
179179
///
180180
/// This is a subset of [`iter_mut`](Self::iter_mut) that only includes pairs where the colliders are touching.
181181
#[inline]
182-
pub fn iter_touching_mut(&mut self) -> impl Iterator<Item = &mut Contacts> {
182+
pub fn iter_touching_mut(&mut self) -> impl Iterator<Item = &mut ContactPair> {
183183
self.internal
184184
.all_edge_weights_mut()
185185
.filter(|contacts| contacts.flags.contains(ContactPairFlags::TOUCHING))
@@ -190,9 +190,9 @@ impl ContactGraph {
190190
/// A contact pair exists between two entities if their [`ColliderAabb`]s intersect,
191191
/// even if the shapes themselves are not yet touching.
192192
///
193-
/// Use [`Contacts::is_touching`](Contacts::is_touching) to determine if the actual collider shapes are touching.
193+
/// Use [`ContactPair::is_touching`](ContactPair::is_touching) to determine if the actual collider shapes are touching.
194194
#[inline]
195-
pub fn collisions_with(&self, entity: Entity) -> impl Iterator<Item = &Contacts> {
195+
pub fn collisions_with(&self, entity: Entity) -> impl Iterator<Item = &ContactPair> {
196196
self.entity_graph_index
197197
.get(entity)
198198
.into_iter()
@@ -204,9 +204,12 @@ impl ContactGraph {
204204
/// A contact pair exists between two entities if their [`ColliderAabb`]s intersect,
205205
/// even if the shapes themselves are not yet touching.
206206
///
207-
/// Use [`Contacts::is_touching`](Contacts::is_touching) to determine if the actual collider shapes are touching.
207+
/// Use [`ContactPair::is_touching`](ContactPair::is_touching) to determine if the actual collider shapes are touching.
208208
#[inline]
209-
pub fn collisions_with_mut(&mut self, entity: Entity) -> impl Iterator<Item = &mut Contacts> {
209+
pub fn collisions_with_mut(
210+
&mut self,
211+
entity: Entity,
212+
) -> impl Iterator<Item = &mut ContactPair> {
210213
if let Some(&index) = self.entity_graph_index.get(entity) {
211214
self.internal.edge_weights_mut(index)
212215
} else {
@@ -243,7 +246,7 @@ impl ContactGraph {
243246
/// Creating a collision pair with this method will *not* trigger any collision events
244247
/// or wake up the entities involved. Only use this method if you know what you are doing.
245248
#[inline]
246-
pub fn add_pair(&mut self, contacts: Contacts) {
249+
pub fn add_pair(&mut self, contacts: ContactPair) {
247250
let pair_key = PairKey::new(contacts.entity1.index(), contacts.entity2.index());
248251
self.add_pair_with_key(contacts, pair_key);
249252
}
@@ -262,7 +265,7 @@ impl ContactGraph {
262265
/// Creating a collision pair with this method will *not* trigger any collision events
263266
/// or wake up the entities involved. Only use this method if you know what you are doing.
264267
#[inline]
265-
pub fn add_pair_with_key(&mut self, contacts: Contacts, pair_key: PairKey) {
268+
pub fn add_pair_with_key(&mut self, contacts: ContactPair, pair_key: PairKey) {
266269
// Add the pair to the pair set for fast lookup.
267270
if !self.pair_set.insert(pair_key) {
268271
// The pair already exists.
@@ -295,7 +298,7 @@ impl ContactGraph {
295298
/// Inserting a collision pair with this method will *not* trigger any collision events
296299
/// or wake up the entities involved. Only use this method if you know what you are doing.
297300
#[inline]
298-
pub fn insert_pair(&mut self, contacts: Contacts) {
301+
pub fn insert_pair(&mut self, contacts: ContactPair) {
299302
let pair_key = PairKey::new(contacts.entity1.index(), contacts.entity2.index());
300303
self.insert_pair_with_key(contacts, pair_key);
301304
}
@@ -314,7 +317,7 @@ impl ContactGraph {
314317
/// Inserting a collision pair with this method will *not* trigger any collision events
315318
/// or wake up the entities involved. Only use this method if you know what you are doing.
316319
#[inline]
317-
pub fn insert_pair_with_key(&mut self, contacts: Contacts, pair_key: PairKey) {
320+
pub fn insert_pair_with_key(&mut self, contacts: ContactPair, pair_key: PairKey) {
318321
// Add the pair to the pair set for fast lookup.
319322
self.pair_set.insert(pair_key);
320323

@@ -344,7 +347,7 @@ impl ContactGraph {
344347
///
345348
/// For filtering and modifying collisions, consider using [`CollisionHooks`] instead.
346349
#[inline]
347-
pub fn remove_pair(&mut self, entity1: Entity, entity2: Entity) -> Option<Contacts> {
350+
pub fn remove_pair(&mut self, entity1: Entity, entity2: Entity) -> Option<ContactPair> {
348351
let (Some(&index1), Some(&index2)) = (
349352
self.entity_graph_index.get(entity1),
350353
self.entity_graph_index.get(entity2),
@@ -372,7 +375,7 @@ impl ContactGraph {
372375
#[inline]
373376
pub fn remove_collider_with<F>(&mut self, entity: Entity, mut pair_callback: F)
374377
where
375-
F: FnMut(Contacts),
378+
F: FnMut(ContactPair),
376379
{
377380
let Some(&index) = self.entity_graph_index.get(entity) else {
378381
return;

src/collision/contact_types/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bevy::prelude::*;
1919
/// are overlapping, even if the colliders themselves are not touching.
2020
#[derive(Clone, Debug, PartialEq)]
2121
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
22-
pub struct Contacts {
22+
pub struct ContactPair {
2323
/// The first collider entity in the contact.
2424
pub entity1: Entity,
2525
/// The second collider entity in the contact.
@@ -36,7 +36,7 @@ pub struct Contacts {
3636
pub flags: ContactPairFlags,
3737
}
3838

39-
/// Flags indicating the status and type of a [contact pair](Contacts).
39+
/// Flags indicating the status and type of a [contact pair](ContactPair).
4040
#[repr(transparent)]
4141
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
4242
#[derive(Hash, Clone, Copy, PartialEq, Eq, Debug, Reflect)]
@@ -62,8 +62,8 @@ bitflags::bitflags! {
6262
}
6363
}
6464

65-
impl Contacts {
66-
/// Creates a new [`Contacts`] with the given entities.
65+
impl ContactPair {
66+
/// Creates a new [`ContactPair`] with the given entities.
6767
#[inline]
6868
pub fn new(entity1: Entity, entity2: Entity) -> Self {
6969
Self {

src/collision/contact_types/system_param.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::data_structures::pair_key::PairKey;
22
use bevy::{ecs::system::SystemParam, prelude::*};
33

4-
use super::{ContactGraph, ContactPairFlags, Contacts};
4+
use super::{ContactGraph, ContactPair, ContactPairFlags};
55

66
/// A [`SystemParam`] for accessing and querying collision data.
77
///
@@ -68,7 +68,7 @@ impl Collisions<'_> {
6868
/// Returns a touching contact pair between two entities.
6969
/// If the pair does not exist, `None` is returned.
7070
#[inline]
71-
pub fn get(&self, entity1: Entity, entity2: Entity) -> Option<&Contacts> {
71+
pub fn get(&self, entity1: Entity, entity2: Entity) -> Option<&ContactPair> {
7272
self.graph.get(entity1, entity2)
7373
}
7474

@@ -91,7 +91,7 @@ impl Collisions<'_> {
9191

9292
/// Returns an iterator yielding immutable access to all touching contact pairs.
9393
#[inline]
94-
pub fn iter(&self) -> impl Iterator<Item = &Contacts> {
94+
pub fn iter(&self) -> impl Iterator<Item = &ContactPair> {
9595
self.graph
9696
.iter()
9797
.filter(|contacts| contacts.flags.contains(ContactPairFlags::TOUCHING))
@@ -100,7 +100,7 @@ impl Collisions<'_> {
100100
/// Returns an iterator yielding immutable access to all touching contact pairs
101101
/// involving the given entity.
102102
#[inline]
103-
pub fn collisions_with(&self, entity: Entity) -> impl Iterator<Item = &Contacts> {
103+
pub fn collisions_with(&self, entity: Entity) -> impl Iterator<Item = &ContactPair> {
104104
self.graph
105105
.collisions_with(entity)
106106
.filter(|contacts| contacts.is_touching())

src/collision/hooks.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use bevy::{ecs::system::ReadOnlySystemParam, prelude::*};
6161
/// group1.0 == group2.0
6262
/// }
6363
///
64-
/// fn modify_contacts(&self, contacts: &mut Contacts, _commands: &mut Commands) -> bool {
64+
/// fn modify_contacts(&self, contacts: &mut ContactPair, _commands: &mut Commands) -> bool {
6565
/// // Allow entities to pass through the bottom and sides of one-way platforms.
6666
/// // See the `one_way_platform_2d` example for a full implementation.
6767
/// let (entity1, entity2) = (contacts.entity1, contacts.entity2);
@@ -73,7 +73,7 @@ use bevy::{ecs::system::ReadOnlySystemParam, prelude::*};
7373
/// # entity1: Entity,
7474
/// # entity2: Entity,
7575
/// # platform_query: &Query<&Transform, With<OneWayPlatform>>,
76-
/// # contacts: &Contacts,
76+
/// # contacts: &ContactPair,
7777
/// # ) -> bool {
7878
/// # todo!()
7979
/// # }
@@ -148,7 +148,7 @@ pub trait CollisionHooks: ReadOnlySystemParam + Send + Sync {
148148
/// A contact pair filtering hook that determines whether contacts should be computed
149149
/// between `entity1` and `entity2`. If `false` is returned, contacts will not be computed.
150150
///
151-
/// This is called in the broad phase, before [`Contacts`] have been computed for the pair.
151+
/// This is called in the broad phase, before the [`ContactPair`] has been computed.
152152
///
153153
/// The provided [`Commands`] can be used for deferred ECS operations that run after
154154
/// broad phase pairs have been found.
@@ -166,11 +166,11 @@ pub trait CollisionHooks: ReadOnlySystemParam + Send + Sync {
166166
/// A contact modification hook that allows modifying the contacts for a given contact pair.
167167
/// If `false` is returned, the contact pair will be removed.
168168
///
169-
/// This is called in the narrow phase, after [`Contacts`] have been computed for the pair,
169+
/// This is called in the narrow phase, after the [`ContactPair`] has been computed for the pair,
170170
/// but before constraints have been generated for the contact solver.
171171
///
172172
/// The provided [`Commands`] can be used for deferred ECS operations that run after
173-
/// narrow phase [`Contacts`] have been computed and constraints have been generated.
173+
/// the narrow phase has computed contact pairs and generated constraints.
174174
///
175175
/// # Notes
176176
///
@@ -180,7 +180,7 @@ pub trait CollisionHooks: ReadOnlySystemParam + Send + Sync {
180180
/// - Command execution order is unspecified if the `parallel` feature is enabled.
181181
/// - Access to the [`ContactGraph`] resource is not allowed in this method.
182182
/// Trying to access it will result in a panic.
183-
fn modify_contacts(&self, contacts: &mut Contacts, commands: &mut Commands) -> bool {
183+
fn modify_contacts(&self, contacts: &mut ContactPair, commands: &mut Commands) -> bool {
184184
true
185185
}
186186
}

src/collision/narrow_phase/system_param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl<C: AnyCollider> NarrowPhase<'_, '_, C> {
555555
pub fn generate_constraints(
556556
&self,
557557
contact_pair_index: usize,
558-
contacts: &Contacts,
558+
contacts: &ContactPair,
559559
constraints: &mut Vec<ContactConstraint>,
560560
body1: &RigidBodyQueryReadOnlyItem,
561561
body2: &RigidBodyQueryReadOnlyItem,

src/debug_render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use bevy::{
2525
/// - [AABBs](ColliderAabb)
2626
/// - [Collider] wireframes
2727
/// - Using different colors for [sleeping](Sleeping) bodies
28-
/// - [Contacts]
28+
/// - [Contacts](ContactPair)
2929
/// - [Joints](dynamics::solver::joints)
3030
/// - [`RayCaster`]
3131
/// - [`ShapeCaster`]
3232
/// - Changing the visibility of entities to only show debug rendering
3333
///
34-
/// By default, [AABBs](ColliderAabb) and [contacts](Contacts) are not debug rendered.
34+
/// By default, [AABBs](ColliderAabb) and [contacts](ContactPair) are not debug rendered.
3535
/// You can configure the [`PhysicsGizmos`] retrieved from `GizmoConfigStore` for the global configuration
3636
/// and the [`DebugRender`] component for entity-level configuration.
3737
///

0 commit comments

Comments
 (0)