aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2021-09-12 09:58:47 +0200
committerSébastien Crozet <sebastien@crozet.re>2021-09-12 01:49:09 -0700
commitd858ae4c4e04d37c6b5a2e37d10c55bf2f239714 (patch)
tree9d78e6917318e86bb56bf6a6b1d31ae2aa1902d9
parent5e133aac92ee5376131d0449daef2ae32e8f2848 (diff)
downloadrapier-d858ae4c4e04d37c6b5a2e37d10c55bf2f239714.tar.gz
rapier-d858ae4c4e04d37c6b5a2e37d10c55bf2f239714.tar.bz2
rapier-d858ae4c4e04d37c6b5a2e37d10c55bf2f239714.zip
Make the 2D add-remove demo more intereting.
-rw-r--r--examples2d/add_remove2.rs58
-rw-r--r--src/geometry/broad_phase_multi_sap/broad_phase.rs2
-rw-r--r--src_testbed/harness/mod.rs1
-rw-r--r--src_testbed/plugin.rs10
-rw-r--r--src_testbed/testbed.rs16
5 files changed, 47 insertions, 40 deletions
diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs
index 4c830c3..f701a90 100644
--- a/examples2d/add_remove2.rs
+++ b/examples2d/add_remove2.rs
@@ -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);
+ }
+ }
});
/*
diff --git a/src/geometry/broad_phase_multi_sap/broad_phase.rs b/src/geometry/broad_phase_multi_sap/broad_phase.rs
index 4dc3e3a..76d48fe 100644
--- a/src/geometry/broad_phase_multi_sap/broad_phase.rs
+++ b/src/geometry/broad_phase_multi_sap/broad_phase.rs
@@ -3,7 +3,7 @@ use super::{
};
use crate::geometry::broad_phase_multi_sap::SAPProxyIndex;
use crate::geometry::{
- ColliderBroadPhaseData, ColliderChanges, ColliderHandle, ColliderPosition, ColliderShape, AABB,
+ ColliderBroadPhaseData, ColliderChanges, ColliderHandle, ColliderPosition, ColliderShape,
};
use crate::math::Real;
use crate::utils::IndexMut2;
diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs
index c85fbc4..0bc08d0 100644
--- a/src_testbed/harness/mod.rs
+++ b/src_testbed/harness/mod.rs
@@ -124,6 +124,7 @@ impl Harness {
self.physics.broad_phase = BroadPhase::new();
self.physics.narrow_phase = NarrowPhase::new();
self.state.timestep_id = 0;
+ self.state.time = 0.0;
self.physics.ccd_solver = CCDSolver::new();
self.physics.query_pipeline = QueryPipeline::new();
self.physics.pipeline = PhysicsPipeline::new();
diff --git a/src_testbed/plugin.rs b/src_testbed/plugin.rs
index e7f2a84..8a9d98c 100644
--- a/src_testbed/plugin.rs
+++ b/src_testbed/plugin.rs
@@ -1,11 +1,16 @@
use crate::harness::Harness;
use crate::physics::PhysicsState;
use crate::GraphicsManager;
-use bevy::prelude::{Assets, Commands, Mesh, Query, StandardMaterial, Transform};
+use bevy::prelude::*;
+use bevy::render::pipeline::PipelineDescriptor;
use bevy_egui::EguiContext;
-use na::Point3;
pub trait TestbedPlugin {
+ fn init_plugin(
+ &mut self,
+ pipelines: &mut Assets<PipelineDescriptor>,
+ shaders: &mut Assets<Shader>,
+ );
fn init_graphics(
&mut self,
graphics: &mut GraphicsManager,
@@ -14,7 +19,6 @@ pub trait TestbedPlugin {
materials: &mut Assets<StandardMaterial>,
components: &mut Query<(&mut Transform,)>,
harness: &mut Harness,
- gen_color: &mut dyn FnMut() -> Point3<f32>,
);
fn clear_graphics(&mut self, graphics: &mut GraphicsManager, commands: &mut Commands);
fn run_callbacks(&mut self, harness: &mut Harness);
diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs
index 581d623..e26125b 100644
--- a/src_testbed/testbed.rs
+++ b/src_testbed/testbed.rs
@@ -35,6 +35,7 @@ use bevy_egui::EguiContext;
use crate::camera2d::{OrbitCamera, OrbitCameraPlugin};
#[cfg(feature = "dim3")]
use crate::camera3d::{OrbitCamera, OrbitCameraPlugin};
+use bevy::render::pipeline::PipelineDescriptor;
const RAPIER_BACKEND: usize = 0;
#[cfg(feature = "other-backends")]
@@ -136,6 +137,8 @@ pub struct TestbedGraphics<'a, 'b, 'c, 'd> {
commands: &'a mut Commands<'d>,
meshes: &'a mut Assets<Mesh>,
materials: &'a mut Assets<StandardMaterial>,
+ shaders: &'a mut Assets<Shader>,
+ pipelines: &'a mut Assets<PipelineDescriptor>,
components: &'a mut Query<'b, (&'c mut Transform,)>,
camera: &'a mut OrbitCamera,
}
@@ -620,7 +623,10 @@ impl<'a, 'b, 'c, 'd> Testbed<'a, 'b, 'c, 'd> {
self.harness.add_callback(callback);
}
- pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) {
+ pub fn add_plugin(&mut self, mut plugin: impl TestbedPlugin + 'static) {
+ if let Some(gfx) = &mut self.graphics {
+ plugin.init_plugin(gfx.pipelines, gfx.shaders);
+ }
self.plugins.0.push(Box::new(plugin));
}
@@ -855,6 +861,8 @@ fn egui_focus(ui_context: Res<EguiContext>, mut cameras: Query<&mut OrbitCamera>
fn update_testbed(
mut commands: Commands,
windows: Res<Windows>,
+ mut pipelines: ResMut<Assets<PipelineDescriptor>>,
+ mut shaders: ResMut<Assets<Shader>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
builders: NonSendMut<SceneBuilders>,
@@ -960,6 +968,8 @@ fn update_testbed(
let meshes = &mut *meshes;
let graphics_context = TestbedGraphics {
+ pipelines: &mut *pipelines,
+ shaders: &mut *shaders,
manager: &mut *manager,
commands: &mut commands,
meshes: &mut *meshes,
@@ -1047,7 +1057,6 @@ fn update_testbed(
}
for plugin in &mut plugins.0 {
- let next_color = graphics.next_color();
plugin.init_graphics(
&mut graphics,
&mut commands,
@@ -1055,7 +1064,6 @@ fn update_testbed(
materials,
&mut gfx_components,
&mut harness,
- &mut || next_color,
);
}
}
@@ -1099,6 +1107,8 @@ fn update_testbed(
let graphics = &mut graphics;
let mut testbed_graphics = TestbedGraphics {
+ pipelines: &mut *pipelines,
+ shaders: &mut *shaders,
manager: &mut *graphics,
commands: &mut commands,
meshes: &mut *meshes,