Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers for ContactData and SingleContact to flip contact data #557

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/collision/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,23 @@ impl SingleContact {
pub fn global_normal2(&self, rotation: &Rotation) -> Vector {
rotation * self.normal2
}

/// Flips the contact data, swapping the points and normals.
pub fn flip(&mut self) {
std::mem::swap(&mut self.point1, &mut self.point2);
std::mem::swap(&mut self.normal1, &mut self.normal2);
}

/// Returns a flipped copy of the contact data, swapping the points and normals.
pub fn flipped(&self) -> Self {
Self {
point1: self.point2,
point2: self.point1,
normal1: self.normal2,
normal2: self.normal1,
penetration: self.penetration,
}
}
}

/// Data related to a contact between two bodies.
Expand Down Expand Up @@ -515,7 +532,7 @@ pub struct ContactData {
/// The contact feature ID on the first shape. This indicates the ID of
/// the vertex, edge, or face of the contact, if one can be determined.
pub feature_id1: PackedFeatureId,
/// The contact feature ID on the first shape. This indicates the ID of
/// The contact feature ID on the second shape. This indicates the ID of
/// the vertex, edge, or face of the contact, if one can be determined.
pub feature_id2: PackedFeatureId,
}
Expand Down Expand Up @@ -601,4 +618,30 @@ impl ContactData {
pub fn global_normal2(&self, rotation: &Rotation) -> Vector {
rotation * self.normal2
}

/// Flips the contact data, swapping the points, normals, and feature IDs,
/// and negating the impulses.
pub fn flip(&mut self) {
std::mem::swap(&mut self.point1, &mut self.point2);
std::mem::swap(&mut self.normal1, &mut self.normal2);
std::mem::swap(&mut self.feature_id1, &mut self.feature_id2);
self.normal_impulse = -self.normal_impulse;
self.tangent_impulse = -self.tangent_impulse;
}

/// Returns a flipped copy of the contact data, swapping the points, normals, and feature IDs,
/// and negating the impulses.
pub fn flipped(&self) -> Self {
Self {
point1: self.point2,
point2: self.point1,
normal1: self.normal2,
normal2: self.normal1,
penetration: self.penetration,
normal_impulse: -self.normal_impulse,
tangent_impulse: -self.tangent_impulse,
feature_id1: self.feature_id2,
feature_id2: self.feature_id1,
}
}
}