diff options
| author | Crozet Sébastien <developer@crozet.re> | 2021-01-22 16:10:24 +0100 |
|---|---|---|
| committer | Crozet Sébastien <developer@crozet.re> | 2021-01-22 16:10:24 +0100 |
| commit | cf52e01308cefcce71b9914c539641cb7530b22c (patch) | |
| tree | 8e12a108d040d2b287957bec2b611c569b530bc7 | |
| parent | 800b35b103c60a3f13dffdfe1c20561074041cea (diff) | |
| parent | e6fc8f67faf3e37afe38d683cbd930d457f289be (diff) | |
| download | rapier-cf52e01308cefcce71b9914c539641cb7530b22c.tar.gz rapier-cf52e01308cefcce71b9914c539641cb7530b22c.tar.bz2 rapier-cf52e01308cefcce71b9914c539641cb7530b22c.zip | |
Merge branch 'master' into split_geom
# Conflicts:
# examples2d/sensor2.rs
# examples3d/sensor3.rs
# src/dynamics/integration_parameters.rs
# src/dynamics/solver/parallel_island_solver.rs
# src/dynamics/solver/velocity_constraint.rs
# src/dynamics/solver/velocity_ground_constraint.rs
# src_testbed/nphysics_backend.rs
# src_testbed/physx_backend.rs
# src_testbed/testbed.rs
24 files changed, 359 insertions, 356 deletions
diff --git a/.github/workflows/rapier-ci-bench.yml b/.github/workflows/rapier-ci-bench.yml index aa6e6ca..29653e8 100644 --- a/.github/workflows/rapier-ci-bench.yml +++ b/.github/workflows/rapier-ci-bench.yml @@ -3,7 +3,7 @@ name: Rapier CI bench on: push: branches: [ master ] - pull_request: + pull_request_target: branches: [ master ] workflow_dispatch: @@ -14,21 +14,23 @@ jobs: BENCHBOT_AMQP_PASS: ${{ secrets.BENCHBOT_AMQP_PASS }} BENCHBOT_AMQP_VHOST: ${{ secrets.BENCHBOT_AMQP_VHOST }} BENCHBOT_AMQP_HOST: ${{ secrets.BENCHBOT_AMQP_HOST }} - BENCHBOT_TARGET_REPO: ${{ github.repository }} + BENCHBOT_TARGET_REPO: ${{ github.event.pull_request.head.repo.clone_url }} BENCHBOT_TARGET_COMMIT: ${{ github.event.pull_request.head.sha }} BENCHBOT_SHA: ${{ github.sha }} - BENCHBOT_HEAD_REF: ${{github.head_ref}} + BENCHBOT_HEAD_REF: ${{ github.head_ref }} BENCHBOT_OTHER_BACKENDS: false runs-on: ubuntu-latest steps: - - name: Find commit SHA - if: github.ref == 'refs/heads/master' + - name: Set env on master + if: github.head_ref == '' run: | echo "BENCHBOT_TARGET_COMMIT=$BENCHBOT_SHA" >> $GITHUB_ENV + echo "BENCHBOT_TARGET_REPO=https://github.com/dimforge/rapier" >> $GITHUB_ENV + echo "BENCHBOT_HEAD_REF=master" >> $GITHUB_ENV echo "BENCHBOT_OTHER_BACKENDS=true" >> $GITHUB_ENV - name: Send 3D bench message shell: bash run: curl -u $BENCHBOT_AMQP_USER:$BENCHBOT_AMQP_PASS -i -H "content-type:application/json" -X POST https://$BENCHBOT_AMQP_HOST/api/exchanges/$BENCHBOT_AMQP_VHOST//publish - -d'{"properties":{},"routing_key":"benchmark","payload":"{ \"repository\":\"https://github.com/'$BENCHBOT_TARGET_REPO'\", \"branch\":\"'$GITHUB_REF'\", \"commit\":\"'$BENCHBOT_TARGET_COMMIT'\", \"other_backends\":'$BENCHBOT_OTHER_BACKENDS' }","payload_encoding":"string"}'
\ No newline at end of file + -d'{"properties":{},"routing_key":"benchmark","payload":"{ \"repository\":\"'$BENCHBOT_TARGET_REPO'\", \"branch\":\"'$BENCHBOT_HEAD_REF'\", \"commit\":\"'$BENCHBOT_TARGET_COMMIT'\", \"other_backends\":'$BENCHBOT_OTHER_BACKENDS' }","payload_encoding":"string"}'
\ No newline at end of file diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a3d223d..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, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -19,7 +19,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, 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 .bodies @@ -31,7 +34,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - graphics.remove_body_nodes(window, handle); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(*window, handle); + } } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 20b2e59..afd4d64 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; diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 6649a43..e53abe3 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.add_callback(move |_, mut graphics, physics, events, _| { while let Ok(prox) = events.intersection_events.try_recv() { let color = if prox.intersecting { Point3::new(1.0, 1.0, 0.0) @@ -79,12 +79,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 { - graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - 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); + } } } }); diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index a42dc97..f353162 100644 --- a/examples3d/debug_add_remove_collider3.rs +++ b/examples3d/debug_add_remove_collider3.rs @@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); colliders.insert(collider, ball_handle, &mut bodies); - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders @@ -43,7 +43,10 @@ pub fn init_world(testbed: &mut Testbed) { ground_collider_handle = physics .colliders .insert(coll, ground_handle, &mut physics.bodies); - graphics.add_collider(window, ground_collider_handle, &physics.colliders); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, ground_collider_handle, &physics.colliders); + } }); /* diff --git a/examples3d/debug_dynamic_collider_add3.rs b/examples3d/debug_dynamic_collider_add3.rs index a71e95e..bd7205e 100644 --- a/examples3d/debug_dynamic_collider_add3.rs +++ b/examples3d/debug_dynamic_collider_add3.rs @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut extra_colliders = Vec::new(); let snapped_frame = 51; - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { step += 1; // Add a bigger ball collider @@ -56,7 +56,11 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, ball_handle, &mut physics.bodies); - graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); + } + extra_colliders.push(new_ball_collider_handle); // Snap the ball velocity or restore it. @@ -95,7 +99,11 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(coll, ground_handle, &mut physics.bodies); - graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); + } + extra_colliders.push(new_ground_collider_handle); }); diff --git a/examples3d/debug_rollback3.rs b/examples3d/debug_rollback3.rs index 19f9474..27b27cf 100644 --- a/examples3d/debug_rollback3.rs +++ b/examples3d/debug_rollback3.rs @@ -44,7 +44,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut step = 0; let snapped_frame = 51; - testbed.add_callback(move |_, physics, _, _, _| { + testbed.add_callback(move |_, _, physics, _, _| { step += 1; // Snap the ball velocity or restore it. diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index fafa988..5acc2e8 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut k = 0; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) @@ -41,7 +41,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, handle, &physics.bodies, &physics.colliders); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add(window, handle, &physics.bodies, &physics.colliders); + } if physics.bodies.len() > MAX_NUMBER_OF_BODIES { let mut to_remove: Vec<_> = physics @@ -67,7 +70,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - graphics.remove_body_nodes(window, *handle); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(window, *handle); + } } } }); diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 0975f39..7b5c986 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -65,7 +65,7 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a callback to control the platform. */ let mut count = 0; - testbed.add_callback(move |_, physics, _, _, time| { + testbed.add_callback(move |_, _, physics, _, run_state| { count += 1; if count % 100 > 50 { return; @@ -75,8 +75,8 @@ pub fn init_world(testbed: &mut Testbed) { let mut next_pos = *platform.position(); let dt = 0.016; - next_pos.translation.vector.y += (time * 5.0).sin() * dt; - next_pos.translation.vector.z += time.sin() * 5.0 * dt; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; if next_pos.translation.vector.z >= rad * 10.0 { next_pos.translation.vector.z -= dt; diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index 234eaf3..641e93d 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,7 +73,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.add_callback(move |_, mut graphics, physics, events, _| { while let Ok(prox) = events.intersection_events.try_recv() { let color = if prox.intersecting { Point3::new(1.0, 1.0, 0.0) @@ -84,11 +84,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 { - graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - 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); + } } } }); diff --git a/src/dynamics/integration_parameters.rs b/src/dynamics/integration_parameters.rs index 706fb3f..0d4d3b6 100644 --- a/src/dynamics/integration_parameters.rs +++ b/src/dynamics/integration_parameters.rs @@ -5,9 +5,8 @@ use crate::math::Real; #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct IntegrationParameters { /// The timestep length (default: `1.0 / 60.0`) - dt: Real, - /// The inverse of `dt`. - inv_dt: Real, + pub dt: Real, + // /// If `true` and if rapier is compiled with the `parallel` feature, this will enable rayon-based multithreading (default: `true`). // /// // /// This parameter is ignored if rapier is not compiled with is `parallel` feature. @@ -31,7 +30,7 @@ pub struct IntegrationParameters { /// Contacts at points where the involved bodies have a relative /// velocity smaller than this threshold wont be affected by the restitution force (default: `1.0`). pub restitution_velocity_threshold: Real, - /// Amount of penetration the engine wont attempt to correct (default: `0.001m`). + /// Amount of penetration the engine wont attempt to correct (default: `0.005m`). pub allowed_linear_error: Real, /// The maximal distance separating two objects that will generate predictive contacts (default: `0.002`). pub prediction_distance: Real, @@ -89,6 +88,7 @@ pub struct IntegrationParameters { impl IntegrationParameters { /// Creates a set of integration parameters with the given values. + #[deprecated = "Use `IntegrationParameters { dt: 60.0, ..Default::default() }` instead"] pub fn new( dt: Real, // multithreading_enabled: bool, @@ -112,7 +112,6 @@ impl IntegrationParameters { ) -> Self { IntegrationParameters { dt, - inv_dt: if dt == 0.0 { 0.0 } else { 1.0 / dt }, // multithreading_enabled, erp, joint_erp, @@ -142,30 +141,29 @@ impl IntegrationParameters { /// The current time-stepping length. #[inline(always)] + #[deprecated = "You can just read the `IntegrationParams::dt` value directly"] pub fn dt(&self) -> Real { self.dt } - /// The inverse of the time-stepping length. + /// The inverse of the time-stepping length, i.e. the steps per seconds (Hz). /// /// This is zero if `self.dt` is zero. #[inline(always)] pub fn inv_dt(&self) -> Real { - self.inv_dt + if self.dt == 0.0 { + 0.0 + } else { + 1.0 / self.dt + } } /// Sets the time-stepping length. - /// - /// This automatically recompute `self.inv_dt`. #[inline] + #[deprecated = "You can just set the `IntegrationParams::dt` value directly"] pub fn set_dt(&mut self, dt: Real) { assert!(dt >= 0.0, "The time-stepping length cannot be negative."); self.dt = dt; - if dt == 0.0 { - self.inv_dt = 0.0 - } else { - self.inv_dt = 1.0 / dt - } } /// Sets the inverse time-stepping length (i.e. the frequency). @@ -173,7 +171,6 @@ impl IntegrationParameters { /// This automatically recompute `self.dt`. #[inline] pub fn set_inv_dt(&mut self, inv_dt: Real) { - self.inv_dt = inv_dt; if inv_dt == 0.0 { self.dt = 0.0 } else { @@ -184,26 +181,32 @@ impl IntegrationParameters { impl Default for IntegrationParameters { fn default() -> Self { - Self::new( - 1.0 / 60.0, - // true, - 0.2, - 0.2, - 1.0, - 1.0, - 0.005, - 0.001, - 0.2, - 0.2, - 0.002, - 0.2, - 4, - 1, - 10, - 1, - false, - false, - false, - ) + Self { + dt: 1.0 / 60.0, + // multithreading_enabled: true, + return_after_ccd_substep: false, + erp: 0.2, + joint_erp: 0.2, + warmstart_coeff: 1.0, + restitution_velocity_threshold: 1.0, + allowed_linear_error: 0.005, + prediction_distance: 0.002, + allowed_angular_error: 0.001, + max_linear_correction: 0.2, + max_angular_correction: 0.2, + max_stabilization_multiplier: 0.2, + max_velocity_iterations: 4, + max_position_iterations: 1, + // FIXME: what is the optimal value for min_island_size? + // It should not be too big so that we don't end up with + // huge islands that don't fit in cache. + // However we don't want it to be too small and end up with + // tons of islands, reducing SIMD parallelism opportunities. + min_island_size: 128, + max_ccd_position_iterations: 10, + max_ccd_substeps: 1, + multiple_ccd_substep_sensor_events_enabled: false, + ccd_on_penetration_enabled: false, + } } } diff --git a/src/dynamics/solver/island_solver.rs b/src/dynamics/solver/island_solver.rs index 46d6b75..deed8c2 100644 --- a/src/dynamics/solver/island_solver.rs +++ b/src/dynamics/solver/island_solver.rs @@ -57,8 +57,7 @@ impl IslandSolver { } counters.solver.velocity_update_time.resume(); - bodies - .foreach_active_island_body_mut_internal(island_id, |_, rb| rb.integrate(params.dt())); + bodies.foreach_active_island_body_mut_internal(island_id, |_, rb| rb.integrate(params.dt)); counters.solver.velocity_update_time.pause(); if manifold_indices.len() != 0 || joint_indices.len() != 0 { diff --git a/src/dynamics/solver/parallel_island_solver.rs b/src/dynamics/solver/parallel_island_solver.rs index 389b6e6..24435db 100644 --- a/src/dynamics/solver/parallel_island_solver.rs +++ b/src/dynamics/solver/parallel_island_solver.rs @@ -251,7 +251,7 @@ impl ParallelIslandSolver { let dvel = mj_lambdas[rb.active_set_offset]; rb.linvel += dvel.linear; rb.angvel += rb.effective_world_inv_inertia_sqrt.transform_vector(dvel.angular); - rb.integrate(params.dt()); + rb.integrate(params.dt)); positions[rb.active_set_offset] = rb.position; } } diff --git a/src/dynamics/solver/velocity_constraint.rs b/src/dynamics/solver/velocity_constraint.rs index c2aa6f5..9d75830 100644 --- a/src/dynamics/solver/velocity_constraint.rs +++ b/src/dynamics/solver/velocity_constraint.rs @@ -144,6 +144,7 @@ impl VelocityConstraint { out_constraints: &mut Vec<AnyVelocityConstraint>, push: bool, ) { + let inv_dt = params.inv_dt(); let rb1 = &bodies[manifold.data.body_pair.body1]; let rb2 = &bodies[manifold.data.body_pair.body2]; let mj_lambda1 = rb1.active_set_offset; @@ -244,7 +245,7 @@ impl VelocityConstraint { rhs += manifold_point.restitution * rhs } - rhs += manifold_point.dist.max(0.0) * params.inv_dt(); + rhs += manifold_point.dist.max(0.0) * inv_dt; let impulse = manifold_point.data.impulse * warmstart_coeff; diff --git a/src/dynamics/solver/velocity_ground_constraint.rs b/src/dynamics/solver/velocity_ground_constraint.rs index 7ea4fbb..49bb465 100644 --- a/src/dynamics/solver/velocity_ground_constraint.rs +++ b/src/dynamics/solver/velocity_ground_constraint.rs @@ -63,6 +63,7 @@ impl VelocityGroundConstraint { out_constraints: &mut Vec<AnyVelocityConstraint>, push: bool, ) { + let inv_dt = params.inv_dt(); let mut rb1 = &bodies[manifold.data.body_pair.body1]; let mut rb2 = &bodies[manifold.data.body_pair.body2]; let flipped = !rb2.is_dynamic(); @@ -159,7 +160,7 @@ impl VelocityGroundConstraint { rhs += manifold_point.restitution * rhs } - rhs += manifold_point.dist.max(0.0) * params.inv_dt(); + rhs += manifold_point.dist.max(0.0) * inv_dt; let impulse = manifold_points[k].data.impulse * warmstart_coeff; diff --git a/src/pipeline/physics_pipeline.rs b/src/pipeline/physics_pipeline.rs index 718dd10..d59a534 100644 --- a/src/pipeline/physics_pipeline.rs +++ b/src/pipeline/physics_pipeline.rs @@ -154,7 +154,7 @@ impl PhysicsPipeline { self.counters.stages.update_time.start(); bodies.foreach_active_dynamic_body_mut_internal(|_, b| { b.update_world_mass_properties(); - b.integrate_accelerations(integration_parameters.dt(), *gravity) + b.integrate_accelerations(integration_parameters.dt, *gravity) }); self.counters.stages.update_time.pause(); @@ -233,7 +233,7 @@ impl PhysicsPipeline { rb.linvel = na::zero(); rb.angvel = na::zero(); } else { - rb.update_predicted_position(integration_parameters.dt()); + rb.update_predicted_position(integration_parameters.dt); } rb.update_colliders_positions(colliders); @@ -330,6 +330,7 @@ mod test { ); } + #[cfg(feature = "serde")] #[test] fn rigid_body_removal_snapshot_handle_determinism() { let mut colliders = ColliderSet::new(); diff --git a/src_testbed/box2d_backend.rs b/src_testbed/box2d_backend.rs index 2d4ef29..29fd4fa 100644 --- a/src_testbed/box2d_backend.rs +++ b/src_testbed/box2d_backend.rs @@ -219,7 +219,7 @@ impl Box2dWorld { counters.step_started(); self.world.step( - params.dt(), + params.dt, params.max_velocity_iterations as i32, params.max_position_iterations as i32, ); diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index c95a722..413ef52 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,4 +1,8 @@ -use crate::physics::{PhysicsEvents, PhysicsState}; +use crate::{ + physics::{PhysicsEvents, PhysicsState}, + GraphicsManager, +}; +use kiss3d::window::Window; use plugin::HarnessPlugin; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; @@ -7,24 +11,58 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; pub mod plugin; -pub struct HarnessState { +pub struct RunState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, + #[cfg(feature = "parallel")] + pub num_threads: usize, pub timestep_id: usize, + pub time: f32, +} + +impl RunState { + pub fn new() -> Self { + #[cfg(feature = "parallel")] + let num_threads = num_cpus::get_physical(); + + #[cfg(feature = "parallel")] + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + + Self { + #[cfg(feature = "parallel")] + thread_pool: thread_pool, + #[cfg(feature = "parallel")] + num_threads, + timestep_id: 0, + time: 0.0, + } + } } pub struct Harness { - physics: PhysicsState, + pub physics: PhysicsState, max_steps: usize, callbacks: Callbacks, plugins: Vec<Box<dyn HarnessPlugin>>, - time: f32, events: PhysicsEvents, event_handler: ChannelEventCollector, - pub state: HarnessState, + pub state: RunState, } -type Callbacks = Vec<Box<dyn FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32)>>; +type Callbacks = Vec< + Box< + dyn FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ), + >, +>; #[allow(dead_code)] impl Harness { @@ -46,18 +84,13 @@ impl Harness { intersection_events: proximity_channel.1, }; let physics = PhysicsState::new(); - let state = HarnessState { - #[cfg(feature = "parallel")] - thread_pool, - timestep_id: 0, - }; + let state = RunState::new(); Self { physics, max_steps: 1000, callbacks: Vec::new(), plugins: Vec::new(), - time: 0.0, events, event_handler, state, @@ -101,7 +134,6 @@ impl Harness { self.physics.joints = joints; self.physics.broad_phase = BroadPhase::new(); self.physics.narrow_phase = NarrowPhase::new(); - self.time = 0.0; self.state.timestep_id = 0; self.physics.query_pipeline = QueryPipeline::new(); self.physics.pipeline = PhysicsPipeline::new(); @@ -113,7 +145,13 @@ impl Harness { } pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, >( &mut self, callback: F, @@ -122,6 +160,14 @@ impl Harness { } pub fn step(&mut self) { + self.step_with_graphics(None, None); + } + + pub fn step_with_graphics( + &mut self, + window: Option<&mut Window>, + graphics: Option<&mut GraphicsManager>, + ) { #[cfg(feature = "parallel")] { let physics = &mut self.physics; @@ -161,26 +207,44 @@ impl Harness { .update(&self.physics.bodies, &self.physics.colliders); for plugin in &mut self.plugins { - plugin.step(&mut self.physics) + plugin.step(&mut self.physics, &self.state) } - for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state, self.time) + // FIXME: This assumes either window & graphics are Some, or they are all None + // this is required as we cannot pass Option<&mut Window> & Option<&mut GraphicsManager directly in a loop + // there must be a better way of doing this? + match (window, graphics) { + (Some(window), Some(graphics)) => |
