Fix mass property update when adding a collider.

This commit is contained in:
Sébastien Crozet
2020-09-01 17:05:24 +02:00
parent 9622827dc6
commit 2f2a073ce4
7 changed files with 127 additions and 64 deletions

View File

@@ -165,7 +165,7 @@ impl NPhysicsWorld {
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(pos * collider.delta());
collider.set_position_debug(pos * collider.position_wrt_parent());
}
}
}

View File

@@ -225,6 +225,28 @@ impl PhysxWorld {
}
}
// Update mass properties.
for (rapier_handle, physx_handle) in rapier2physx.iter() {
let rb = &bodies[*rapier_handle];
if let Some(mut ra) = scene.get_dynamic_mut(*physx_handle) {
let densities: Vec<_> = rb
.colliders()
.iter()
.map(|h| colliders[*h].density())
.collect();
unsafe {
physx_sys::PxRigidBodyExt_updateMassAndInertia_mut(
ra.as_ptr_mut().ptr as *mut physx_sys::PxRigidBody,
densities.as_ptr(),
densities.len() as u32,
std::ptr::null(),
false,
);
}
}
}
let mut res = Self {
physics,
cooking,
@@ -390,7 +412,7 @@ impl PhysxWorld {
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(iso * collider.delta());
collider.set_position_debug(iso * collider.position_wrt_parent());
}
}
}

View File

@@ -791,6 +791,29 @@ impl Testbed {
.state
.action_flags
.set(TestbedActionFlags::EXAMPLE_CHANGED, true),
WindowEvent::Key(Key::C, Action::Release, _) => {
// Delete 1 collider of 10% of the remaining dynamic bodies.
let mut colliders: Vec<_> = self
.physics
.bodies
.iter()
.filter(|e| e.1.is_dynamic())
.filter(|e| !e.1.colliders().is_empty())
.map(|e| e.1.colliders().to_vec())
.collect();
colliders.sort_by_key(|co| -(co.len() as isize));
let num_to_delete = (colliders.len() / 10).max(1);
for to_delete in &colliders[..num_to_delete] {
self.physics.pipeline.remove_collider(
to_delete[0],
&mut self.physics.broad_phase,
&mut self.physics.narrow_phase,
&mut self.physics.bodies,
&mut self.physics.colliders,
);
}
}
WindowEvent::Key(Key::D, Action::Release, _) => {
// Delete 10% of the remaining dynamic bodies.
let dynamic_bodies: Vec<_> = self
@@ -1539,7 +1562,7 @@ impl State for Testbed {
}
if self.state.flags.contains(TestbedStateFlags::CONTACT_POINTS) {
draw_contacts(window, &self.physics.narrow_phase, &self.physics.bodies);
draw_contacts(window, &self.physics.narrow_phase, &self.physics.colliders);
}
if self.state.running == RunMode::Step {
@@ -1634,7 +1657,7 @@ Hashes at frame: {}
}
}
fn draw_contacts(window: &mut Window, nf: &NarrowPhase, bodies: &RigidBodySet) {
fn draw_contacts(window: &mut Window, nf: &NarrowPhase, colliders: &ColliderSet) {
for (_, _, pair) in nf.contact_graph().interaction_pairs() {
for manifold in &pair.manifolds {
for pt in manifold.all_contacts() {
@@ -1643,8 +1666,8 @@ fn draw_contacts(window: &mut Window, nf: &NarrowPhase, bodies: &RigidBodySet) {
} else {
Point3::new(1.0, 0.0, 0.0)
};
let pos1 = bodies[manifold.body_pair.body1].position;
let pos2 = bodies[manifold.body_pair.body2].position;
let pos1 = colliders[manifold.pair.collider1].position();
let pos2 = colliders[manifold.pair.collider2].position();
let start = pos1 * pt.local_p1;
let end = pos2 * pt.local_p2;
let n = pos1 * manifold.local_n1;