Merge pull request #50 from dimforge/joint_removal

Implement joint removal.
This commit is contained in:
Sébastien Crozet
2020-11-02 19:00:12 +01:00
committed by GitHub
2 changed files with 42 additions and 0 deletions

View File

@@ -181,6 +181,38 @@ impl JointSet {
}
}
/// Removes a joint from this set.
///
/// If `wake_up` is set to `true`, then the bodies attached to this joint will be
/// automatically woken up.
pub fn remove(
&mut self,
handle: JointHandle,
bodies: &mut RigidBodySet,
wake_up: bool,
) -> Option<Joint> {
let id = self.joint_ids.remove(handle)?;
let endpoints = self.joint_graph.graph.edge_endpoints(id)?;
if wake_up {
// Wake-up the bodies attached to this joint.
if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.0) {
bodies.wake_up(*rb_handle, true);
}
if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.1) {
bodies.wake_up(*rb_handle, true);
}
}
let removed_joint = self.joint_graph.graph.remove_edge(id);
if let Some(edge) = self.joint_graph.graph.edge_weight(id) {
self.joint_ids[edge.handle] = id;
}
removed_joint
}
pub(crate) fn remove_rigid_body(
&mut self,
deleted_id: RigidBodyGraphIndex,

View File

@@ -847,6 +847,16 @@ impl Testbed {
);
}
}
WindowEvent::Key(Key::J, Action::Release, _) => {
// Delete 10% of the remaining joints.
let joints: Vec<_> = self.physics.joints.iter().map(|e| e.0).collect();
let num_to_delete = (joints.len() / 10).max(1);
for to_delete in &joints[..num_to_delete] {
self.physics
.joints
.remove(*to_delete, &mut self.physics.bodies, true);
}
}
WindowEvent::CursorPos(x, y, _) => {
self.cursor_pos.x = x as f32;
self.cursor_pos.y = y as f32;