|
| 1 | +use rapier_testbed3d::Testbed; |
| 2 | +use rapier3d::prelude::*; |
| 3 | + |
| 4 | +pub fn init_world(testbed: &mut Testbed) { |
| 5 | + let mut bodies = RigidBodySet::new(); |
| 6 | + let mut colliders = ColliderSet::new(); |
| 7 | + let impulse_joints = ImpulseJointSet::new(); |
| 8 | + let mut multibody_joints = MultibodyJointSet::new(); |
| 9 | + |
| 10 | + let root = bodies.insert(RigidBodyBuilder::fixed()); |
| 11 | + colliders.insert_with_parent(ColliderBuilder::cuboid(0.5, 0.5, 0.5), root, &mut bodies); |
| 12 | + |
| 13 | + let mut attached_bodies = vec![root]; |
| 14 | + for i in 1..4 { |
| 15 | + let body = bodies.insert(RigidBodyBuilder::dynamic().translation(Vector::new( |
| 16 | + 0.0, |
| 17 | + -2.0 * i as f32, |
| 18 | + 0.0, |
| 19 | + ))); |
| 20 | + colliders.insert_with_parent(ColliderBuilder::cuboid(0.5, 0.5, 0.5), body, &mut bodies); |
| 21 | + |
| 22 | + multibody_joints.insert( |
| 23 | + *attached_bodies.last().unwrap(), |
| 24 | + body, |
| 25 | + SphericalJointBuilder::new() |
| 26 | + .local_anchor1(Point::new(0.0, -2.0, 0.0)) |
| 27 | + .build() |
| 28 | + .data, |
| 29 | + true, |
| 30 | + ); |
| 31 | + attached_bodies.push(body); |
| 32 | + } |
| 33 | + |
| 34 | + // Remove this and the example won't crash. |
| 35 | + { |
| 36 | + let body = |
| 37 | + bodies.insert(RigidBodyBuilder::dynamic().translation(Vector::new(0.0, 2.0, 0.0))); |
| 38 | + colliders.insert_with_parent(ColliderBuilder::cuboid(0.5, 0.5, 0.5), body, &mut bodies); |
| 39 | + } |
| 40 | + |
| 41 | + testbed.add_callback(move |graphics, physics, _, run_state| { |
| 42 | + if run_state.timestep_id % 33 == 31 && attached_bodies.len() > 1 { |
| 43 | + let body = attached_bodies.pop().unwrap(); |
| 44 | + physics.bodies.remove( |
| 45 | + body, |
| 46 | + &mut physics.islands, |
| 47 | + &mut physics.colliders, |
| 48 | + &mut physics.impulse_joints, |
| 49 | + &mut physics.multibody_joints, |
| 50 | + true, |
| 51 | + ); |
| 52 | + if let Some(graphics) = graphics { |
| 53 | + graphics.remove_body(body); |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + testbed.set_world(bodies, colliders, impulse_joints, multibody_joints); |
| 59 | + testbed.look_at(point![20.0, 0.0, 0.0], point![0.0, 0.0, 0.0]); |
| 60 | +} |
0 commit comments