Skip to content

Commit 552cfeb

Browse files
agarret7sebcrozet
andauthored
Fix shape modification not updating graphics in testbed (#708)
* Fix shape modification not updating graphics in testbed * Add update collider to Testbed * chore: lint shape_modifications3 * chore: simplify GraphicsManager::remove_collider_nodes --------- Co-authored-by: Sébastien Crozet <sebcrozet@dimforge.com>
1 parent 2ed1934 commit 552cfeb

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

examples3d/debug_shape_modification3.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,32 @@ pub fn init_world(testbed: &mut Testbed) {
3535
let collider = ColliderBuilder::ball(ball_rad).density(100.0);
3636
let ball_coll_handle = colliders.insert_with_parent(collider, ball_handle, &mut bodies);
3737

38+
/*
39+
* Colliders without bodies
40+
*/
41+
let shape_size = 3.0;
42+
let static_collider =
43+
ColliderBuilder::ball(shape_size).translation(vector![-15.0, shape_size, 18.0]);
44+
colliders.insert(static_collider);
45+
46+
let shapes = [
47+
SharedShape::ball(shape_size),
48+
SharedShape::cuboid(shape_size, shape_size, shape_size),
49+
SharedShape::cone(shape_size, shape_size),
50+
SharedShape::cylinder(shape_size, shape_size),
51+
];
52+
let mut shape_idx = 0;
53+
let shapeshifting_collider = ColliderBuilder::new(shapes[shape_idx].clone())
54+
.translation(vector![-15.0, shape_size, 9.0]);
55+
let shapeshifting_coll_handle = colliders.insert(shapeshifting_collider);
56+
3857
let mut linvel = Vector::zeros();
3958
let mut angvel = Vector::zeros();
4059
let mut pos = Isometry::identity();
4160
let mut step = 0;
4261
let snapped_frame = 51;
4362

44-
testbed.add_callback(move |_, physics, _, _| {
63+
testbed.add_callback(move |mut gfx, physics, _, _| {
4564
step += 1;
4665

4766
// Snap the ball velocity or restore it.
@@ -53,6 +72,15 @@ pub fn init_world(testbed: &mut Testbed) {
5372
pos = *ball.position();
5473
}
5574

75+
let shapeshifting_coll = physics
76+
.colliders
77+
.get_mut(shapeshifting_coll_handle)
78+
.unwrap();
79+
if step % 50 == 0 {
80+
shape_idx = (shape_idx + 1) % 4;
81+
shapeshifting_coll.set_shape(shapes[shape_idx].clone())
82+
}
83+
5684
if step == 100 {
5785
ball.set_linvel(linvel, true);
5886
ball.set_angvel(angvel, true);
@@ -62,6 +90,11 @@ pub fn init_world(testbed: &mut Testbed) {
6290

6391
let ball_coll = physics.colliders.get_mut(ball_coll_handle).unwrap();
6492
ball_coll.set_shape(SharedShape::ball(ball_rad * step as f32 * 2.0));
93+
94+
if let Some(gfx) = &mut gfx {
95+
gfx.update_collider(ball_coll_handle, &physics.colliders);
96+
gfx.update_collider(shapeshifting_coll_handle, &physics.colliders);
97+
}
6598
});
6699

67100
/*
@@ -111,5 +144,5 @@ pub fn init_world(testbed: &mut Testbed) {
111144
* Set up the testbed.
112145
*/
113146
testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
114-
testbed.look_at(point![10.0, 10.0, 10.0], Point::origin());
147+
testbed.look_at(point![40.0, 40.0, 40.0], Point::origin());
115148
}

src_testbed/graphics.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ impl GraphicsManager {
8383
) {
8484
let body = body.unwrap_or(RigidBodyHandle::invalid());
8585
if let Some(sns) = self.b2sn.get_mut(&body) {
86-
for sn in sns.iter_mut() {
86+
sns.retain(|sn| {
8787
if let Some(sn_c) = sn.collider {
8888
if sn_c == collider {
8989
commands.entity(sn.entity).despawn();
90+
return false;
9091
}
9192
}
92-
}
93+
94+
true
95+
});
9396
}
9497
}
9598

src_testbed/testbed.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,6 @@ impl TestbedGraphics<'_, '_, '_, '_, '_, '_> {
492492
)
493493
}
494494

495-
pub fn remove_collider(&mut self, handle: ColliderHandle, colliders: &ColliderSet) {
496-
if let Some(parent_handle) = colliders.get(handle).map(|c| c.parent()) {
497-
self.graphics
498-
.remove_collider_nodes(&mut *self.commands, parent_handle, handle)
499-
}
500-
}
501-
502495
pub fn remove_body(&mut self, handle: RigidBodyHandle) {
503496
self.graphics.remove_body_nodes(&mut *self.commands, handle)
504497
}
@@ -513,6 +506,18 @@ impl TestbedGraphics<'_, '_, '_, '_, '_, '_> {
513506
)
514507
}
515508

509+
pub fn remove_collider(&mut self, handle: ColliderHandle, colliders: &ColliderSet) {
510+
if let Some(parent_handle) = colliders.get(handle).map(|c| c.parent()) {
511+
self.graphics
512+
.remove_collider_nodes(&mut *self.commands, parent_handle, handle)
513+
}
514+
}
515+
516+
pub fn update_collider(&mut self, handle: ColliderHandle, colliders: &ColliderSet) {
517+
self.remove_collider(handle, colliders);
518+
self.add_collider(handle, colliders);
519+
}
520+
516521
pub fn keys(&self) -> &ButtonInput<KeyCode> {
517522
self.keys
518523
}

0 commit comments

Comments
 (0)