Make the 2D add-remove demo more intereting.

This commit is contained in:
Sébastien Crozet
2021-09-12 09:58:47 +02:00
committed by Sébastien Crozet
parent 5e133aac92
commit d858ae4c4e
5 changed files with 47 additions and 40 deletions

View File

@@ -13,27 +13,20 @@ pub fn init_world(testbed: &mut Testbed) {
.map(|pos| {
let rigid_body = RigidBodyBuilder::new_kinematic_position_based()
.translation(pos)
// .ccd_enabled(true)
.build();
let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad * 10.0, rad)
.active_events(ActiveEvents::CONTACT_EVENTS)
.build();
let coll_handle = colliders.insert_with_parent(collider, handle, &mut bodies);
(pos, handle, coll_handle)
let collider = ColliderBuilder::cuboid(rad * 10.0, rad).build();
colliders.insert_with_parent(collider, handle, &mut bodies);
handle
})
.collect::<Vec<_>>();
let mut rotation = 0.0;
// Callback that will be executed on the main loop to handle proximities.
testbed.add_callback(move |mut graphics, physics, events, state| {
testbed.add_callback(move |mut graphics, physics, _, state| {
let rot = state.time * -1.0;
let pos = state.time.sin() * 5.0;
for (p, i, _) in platform_handles.iter() {
let mut c = physics.bodies.get_mut(*i).unwrap();
c.set_next_kinematic_rotation(rot);
//c.set_next_kinematic_translation(vector![pos, 0.0]);
for rb_handle in &platform_handles {
let rb = physics.bodies.get_mut(*rb_handle).unwrap();
rb.set_next_kinematic_rotation(rot);
}
if state.timestep_id % 10 == 0 {
@@ -41,7 +34,6 @@ pub fn init_world(testbed: &mut Testbed) {
let y = rand::random::<f32>() * 10.0 + 10.0;
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(vector![x, y])
// .ccd_enabled(true)
.build();
let handle = physics.bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad).build();
@@ -54,24 +46,24 @@ pub fn init_world(testbed: &mut Testbed) {
}
}
// let to_remove: Vec<_> = physics
// .bodies
// .iter()
// .filter(|(_, b)| b.position().translation.vector.y < -10.0)
// .map(|e| e.0)
// .collect();
// for handle in to_remove {
// physics.bodies.remove(
// handle,
// &mut physics.islands,
// &mut physics.colliders,
// &mut physics.joints,
// );
//
// if let Some(graphics) = &mut graphics {
// graphics.remove_body(handle);
// }
// }
let to_remove: Vec<_> = physics
.bodies
.iter()
.filter(|(_, b)| b.position().translation.vector.y < -10.0)
.map(|e| e.0)
.collect();
for handle in to_remove {
physics.bodies.remove(
handle,
&mut physics.islands,
&mut physics.colliders,
&mut physics.joints,
);
if let Some(graphics) = &mut graphics {
graphics.remove_body(handle);
}
}
});
/*