From c56ebcc663d15ded86233d8caa11d00179e5ec16 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 17:58:37 +1100 Subject: refactor testbed to use harness --- examples2d/platform2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples2d') diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 20b2e59..3b543db 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,13 +60,13 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.add_callback(move |_, physics, _, _, time| { + testbed.add_callback(move |_, physics, _, _, run_state| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); let dt = 0.016; - next_pos.translation.vector.y += (time * 5.0).sin() * dt; - next_pos.translation.vector.x += time.sin() * 5.0 * dt; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; if next_pos.translation.vector.x >= rad * 10.0 { next_pos.translation.vector.x -= dt; -- cgit From f7820139471178fd273c7698eba17cb4ee0cf00d Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:24:29 +1100 Subject: make examples compile, code that accessed window & graphics via the callback is currently disabled, until that is added back in --- examples2d/add_remove2.rs | 10 +++++++--- examples2d/platform2.rs | 2 +- examples2d/sensor2.rs | 8 +++++--- 3 files changed, 13 insertions(+), 7 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a3d223d..7ace00f 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -19,7 +19,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, handle, &physics.bodies, &physics.colliders); + + // TODO: need a way to access graphics & window + // graphics.add(window, handle, &physics.bodies, &physics.colliders); let to_remove: Vec<_> = physics .bodies @@ -31,7 +33,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - graphics.remove_body_nodes(window, handle); + + // TODO: need a way to access graphics & window + // graphics.remove_body_nodes(window, handle); } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 3b543db..5b84a18 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,7 +60,7 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.add_callback(move |_, physics, _, _, run_state| { + testbed.harness_mut().add_callback(move |physics, _, run_state, _| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 959ecbf..c9edea0 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,7 +69,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, physics, events, graphics, _| { + testbed.harness_mut().add_callback(move |physics, events, _, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -80,10 +80,12 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - graphics.set_body_color(parent_handle1, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); } if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - graphics.set_body_color(parent_handle2, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); } } }); -- cgit From e11ace383164de39dfaadcb00f258497b132cf1d Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:31:30 +1100 Subject: cargo fmt --- examples2d/add_remove2.rs | 4 ++-- examples2d/platform2.rs | 30 ++++++++++++++++-------------- examples2d/sensor2.rs | 40 +++++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 35 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index 7ace00f..a351a8f 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -19,7 +19,7 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - + // TODO: need a way to access graphics & window // graphics.add(window, handle, &physics.bodies, &physics.colliders); @@ -33,7 +33,7 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - + // TODO: need a way to access graphics & window // graphics.remove_body_nodes(window, handle); } diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 5b84a18..769161f 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,23 +60,25 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.harness_mut().add_callback(move |physics, _, run_state, _| { - let platform = physics.bodies.get_mut(platform_handle).unwrap(); - let mut next_pos = *platform.position(); + testbed + .harness_mut() + .add_callback(move |physics, _, run_state, _| { + let platform = physics.bodies.get_mut(platform_handle).unwrap(); + let mut next_pos = *platform.position(); - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; - if next_pos.translation.vector.x >= rad * 10.0 { - next_pos.translation.vector.x -= dt; - } - if next_pos.translation.vector.x <= -rad * 10.0 { - next_pos.translation.vector.x += dt; - } + if next_pos.translation.vector.x >= rad * 10.0 { + next_pos.translation.vector.x -= dt; + } + if next_pos.translation.vector.x <= -rad * 10.0 { + next_pos.translation.vector.x += dt; + } - platform.set_next_kinematic_position(next_pos); - }); + platform.set_next_kinematic_position(next_pos); + }); /* * Run the simulation. diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index c9edea0..123f633 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,26 +69,28 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, events, _, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; - - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); + testbed + .harness_mut() + .add_callback(move |physics, events, _, _| { + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; + + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); + } } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); - } - } - }); + }); /* * Set up the testbed. -- cgit From 6f508e5d04e5652991f9feaad09231af84542ac1 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:23:25 +1100 Subject: remove redundant time :f32 from harness callbacks. it can be access via run_state.time --- examples2d/add_remove2.rs | 2 +- examples2d/platform2.rs | 2 +- examples2d/sensor2.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a351a8f..614829b 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 769161f..88f05f2 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -62,7 +62,7 @@ pub fn init_world(testbed: &mut Testbed) { */ testbed .harness_mut() - .add_callback(move |physics, _, run_state, _| { + .add_callback(move |physics, _, run_state| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 123f633..382581e 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -71,7 +71,7 @@ pub fn init_world(testbed: &mut Testbed) { // Callback that will be executed on the main loop to handle proximities. testbed .harness_mut() - .add_callback(move |physics, events, _, _| { + .add_callback(move |physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), -- cgit From 34e79e9afc21ff0202d8a0338d0e8e038402a159 Mon Sep 17 00:00:00 2001 From: rezural Date: Sat, 2 Jan 2021 16:45:55 +1100 Subject: unify callbacks with & without graphics & window --- examples2d/add_remove2.rs | 15 ++++++++++----- examples2d/platform2.rs | 30 ++++++++++++++---------------- examples2d/sensor2.rs | 40 +++++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 42 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index 614829b..cb71025 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |window, graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -20,8 +20,11 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.add(window, handle, &physics.bodies, &physics.colliders); + if graphics.is_some() { + graphics + .unwrap() + .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + } let to_remove: Vec<_> = physics .bodies @@ -34,8 +37,10 @@ pub fn init_world(testbed: &mut Testbed) { .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - // TODO: need a way to access graphics & window - // graphics.remove_body_nodes(window, handle); + // FIXME: need a way to access graphics & window in a loop + // if graphics.is_some() { + // graphics.unwrap().remove_body_nodes(window.unwrap(), handle); + // } } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 88f05f2..afd4d64 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,25 +60,23 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed - .harness_mut() - .add_callback(move |physics, _, run_state| { - let platform = physics.bodies.get_mut(platform_handle).unwrap(); - let mut next_pos = *platform.position(); + testbed.add_callback(move |_, _, physics, _, run_state| { + let platform = physics.bodies.get_mut(platform_handle).unwrap(); + let mut next_pos = *platform.position(); - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; - if next_pos.translation.vector.x >= rad * 10.0 { - next_pos.translation.vector.x -= dt; - } - if next_pos.translation.vector.x <= -rad * 10.0 { - next_pos.translation.vector.x += dt; - } + if next_pos.translation.vector.x >= rad * 10.0 { + next_pos.translation.vector.x -= dt; + } + if next_pos.translation.vector.x <= -rad * 10.0 { + next_pos.translation.vector.x += dt; + } - platform.set_next_kinematic_position(next_pos); - }); + platform.set_next_kinematic_position(next_pos); + }); /* * Run the simulation. diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 382581e..9fdbf14 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,28 +69,26 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed - .harness_mut() - .add_callback(move |physics, events, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; - - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); - } + testbed.add_callback(move |_, _, physics, events, _| { + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; + + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // FIXME: need a way to access graphics & window in a loop + // graphics.set_body_color(parent_handle1, color); } - }); + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // FIXME: need a way to access graphics & window in a loop + // graphics.set_body_color(parent_handle2, color); + } + } + }); /* * Set up the testbed. -- cgit From 5ca82eeaee5c45d31cdbb5f963d0f93b19196ea8 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 3 Jan 2021 19:54:56 +1100 Subject: enable graphics and windows related code in examples --- examples2d/add_remove2.rs | 15 ++++++--------- examples2d/sensor2.rs | 17 ++++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index cb71025..0aeffbe 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, graphics, physics, _, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -20,10 +20,8 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, handle, &mut physics.bodies); - if graphics.is_some() { - graphics - .unwrap() - .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add(*window, handle, &physics.bodies, &physics.colliders); } let to_remove: Vec<_> = physics @@ -37,10 +35,9 @@ pub fn init_world(testbed: &mut Testbed) { .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - // FIXME: need a way to access graphics & window in a loop - // if graphics.is_some() { - // graphics.unwrap().remove_body_nodes(window.unwrap(), handle); - // } + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(*window, handle); + } } }); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 9fdbf14..3d5976d 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,7 +69,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, _, physics, events, _| { + testbed.add_callback(move |_, mut graphics, physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -78,14 +78,13 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // FIXME: need a way to access graphics & window in a loop - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // FIXME: need a way to access graphics & window in a loop - // graphics.set_body_color(parent_handle2, color); + if let Some(graphics) = &mut graphics { + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + graphics.set_body_color(parent_handle2, color); + } } } }); -- cgit