I’d like to apply a force when two things collide that propels them away from each other.
let collider_world = physics.collider_world_mut();
let mut body_handles: Vec<BodyHandle> = vec![];
for (collider, _, _, _) in collider_world.contact_pairs(false) {
body_handles.push(collider.body());
}
for body_handle in body_handles {
let rigid_body = physics.body_mut(body_handle).unwrap();
let vector = Vector2::new(-500.0, 0.0);
let force = Force2::new(vector, 0.0);
rigid_body.apply_force(0, &force, ForceType::Impulse, true);
}
vector
here would be determined dynamically. I could be conceptually misunderstanding things however, so I hope this question makes sense.
Hi! All the information you need are provided by the iterator given by .contact_pairs(false)
:
let mut body_handles_and_normals = vec![];
for (collider1, collider2, _, contact_manifold) in collider_world.contact_pairs(false) {
// `collider1` is the first collider involved in the contact.
// `collider2` is the second collider involved in the contact.
// All the contacts points between those two colliders are stored in the `contact_manifold`.
// If you are only interested in the most significant contact, and its normal you can do
// this:
if let Some(tracked_contact) = manifold.deepest_contact() {
let contact_normal = tracked_contact.contact.normal;
body_handles_and_normals.push((collider1.body(), collider2.body(), contact_normal));
}
}
Documentation of the contact manifold: https://www.ncollide.org/rustdoc/ncollide2d/query/struct.ContactManifold.html#method.deepest_contact
1 Like