Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to reproduce a crash with a more complex example #26

Open
wants to merge 4 commits into
base: RapierContext_Component
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ codegen-units = 1
#parry2d = { path = "../parry/crates/parry2d" }
#parry3d = { path = "../parry/crates/parry3d" }
#rapier2d = { path = "../rapier/crates/rapier2d" }
#rapier3d = { path = "../rapier/crates/rapier3d" }
rapier3d = { path = "../rapier/crates/rapier3d" }

#nalgebra = { git = "https://github.com/dimforge/nalgebra", branch = "dev" }
#parry2d = { git = "https://github.com/dimforge/parry", branch = "master" }
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/multi_world3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default()
.with_default_world(RapierContextInitialization::NoAutomaticRapierContext),
.with_custom_initialization(RapierContextInitialization::NoAutomaticRapierContext),
RapierDebugRenderPlugin::default(),
))
.add_systems(
Expand Down
99 changes: 99 additions & 0 deletions bevy_rapier3d/examples/multi_world_interpolated3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use bevy_rapier3d::prelude::*;

const N_WORLDS: usize = 2;

fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default()
.with_custom_initialization(RapierContextInitialization::NoAutomaticRapierContext),
RapierDebugRenderPlugin::default(),
))
.insert_resource(TimestepMode::Interpolated {
dt: 1.0 / 60.0,
time_scale: 1.0,
substeps: 2,
})
.add_systems(
Startup,
((create_worlds, setup_physics).chain(), setup_graphics),
)
.add_systems(
Update,
change_world, //.run_if(input_just_pressed(KeyCode::KeyC)),
)
.run();
}

fn create_worlds(mut commands: Commands) {
for i in 0..N_WORLDS {
let mut world = commands.spawn(RapierContext::default());
if i == 0 {
world.insert(DefaultRapierContext);
}
}
}

fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 3.0, -10.0)
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..Default::default()
});
}

fn change_world(
query_context: Query<Entity, With<DefaultRapierContext>>,
q_other_ctx: Query<Entity, (With<RapierContext>, Without<DefaultRapierContext>)>,
mut query_links: Query<&mut RapierContextEntityLink>,
) {
let default_context = query_context.single();
for mut link in query_links.iter_mut() {
if link.0 == default_context {
link.0 = q_other_ctx.single();
continue;
}
link.0 = default_context;
}
}

pub fn setup_physics(mut commands: Commands) {
/*
* Create the cube
*/

let color = [
Hsla::hsl(220.0, 1.0, 0.3),
Hsla::hsl(180.0, 1.0, 0.3),
Hsla::hsl(260.0, 1.0, 0.7),
][0 % 3];

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(color),
ActiveEvents::all(),
));

/*
* Ground
*/
let color = Hsla::hsl(260.0, 1.0, 0.7);
let ground_size = 5.1;
let ground_height = 0.1;
let starting_y = -0.5 - ground_height;

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
ColliderDebugColor(color),
));
}
4 changes: 3 additions & 1 deletion src/pipeline/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ impl<'a> EventQueue<'a> {
.get(handle)
.map(|co| Entity::from_bits(co.user_data as u64))
.or_else(|| self.deleted_colliders.get(&handle).copied())
.expect("Internal error: entity not found for collision event.")
.unwrap_or_else(|| {
panic!("Internal error: entity not found for collision event with collider handle: {handle:?}.")
})
}
}

Expand Down
1 change: 1 addition & 0 deletions src/plugin/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ impl RapierContext {
substep_integration_parameters.dt = dt / (substeps as Real) * time_scale;

for _ in 0..substeps {
dbg!("execute step");
self.pipeline.step(
&gravity.into(),
&substep_integration_parameters,
Expand Down
12 changes: 5 additions & 7 deletions src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ where
self
}

/// Specifies a default world initialization strategy.
///
/// The default is to initialize a [`RapierContext`] with a length unit of 1.
pub fn with_default_world(
/// Specifies an initialization strategy when the [`App`] starts.
pub fn with_custom_initialization(
mut self,
default_world_initialization: RapierContextInitialization,
) -> Self {
Expand Down Expand Up @@ -110,6 +108,7 @@ where
systems::on_add_entity_with_parent,
systems::on_change_world,
systems::sync_removals,
apply_deferred,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_scene_colliders,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
Expand Down Expand Up @@ -206,16 +205,15 @@ where
.register_type::<RapierConfiguration>()
.register_type::<SimulationToRenderTime>()
.register_type::<DefaultRapierContext>()
.register_type::<RapierContextInitialization>()
.register_type::<ColliderDebugColor>();
.register_type::<RapierContextInitialization>();

app.insert_resource(Events::<CollisionEvent>::default())
.insert_resource(Events::<ContactForceEvent>::default())
.insert_resource(Events::<MassModifiedEvent>::default());
let default_world_init = app.world().get_resource::<RapierContextInitialization>();
if let Some(world_init) = default_world_init {
warn!("RapierPhysicsPlugin added but a `RapierContextInitialization` resource was already existing.\
This might overwrite previous configuration made via `RapierPhysicsPlugin::with_default_world`\
This will overwrite previous configuration made via `RapierPhysicsPlugin::with_custom_initialization`\
or `RapierPhysicsPlugin::with_length_unit`.
The following resource will be used: {:?}", world_init);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/systems/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ pub fn init_colliders(
let Some(context_entity) = context_entity else {
continue;
};
println!("init collider {entity} for world {context_entity}");

let config = config.get(context_entity).unwrap_or_else(|_| {
panic!("Failed to retrieve `RapierConfiguration` on entity {context_entity}.")
Expand Down
9 changes: 7 additions & 2 deletions src/plugin/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use bevy::prelude::*;
/// for scene queries.
pub fn step_simulation<Hooks>(
mut context: Query<(
Entity,
&mut RapierContext,
&RapierConfiguration,
&mut SimulationToRenderTime,
Expand All @@ -44,10 +45,12 @@ pub fn step_simulation<Hooks>(
{
let hooks_adapter = BevyPhysicsHooksAdapter::new(hooks.into_inner());

for (mut context, config, mut sim_to_render_time) in context.iter_mut() {
for (entity, mut context, config, mut sim_to_render_time) in context.iter_mut() {
let context = &mut *context;

if config.physics_pipeline_active {
println!("step {entity}");
//println!("step {:?}", context.colliders);
context.step_simulation(
config.gravity,
*timestep_mode,
Expand All @@ -57,7 +60,9 @@ pub fn step_simulation<Hooks>(
&mut sim_to_render_time,
Some(&mut interpolation_query),
);
context.deleted_colliders.clear();
println!("clear");
// That's the offending line ! This should be within `step_simulation` only if we actually step.
// context.deleted_colliders.clear();
} else {
context.propagate_modified_body_positions_to_colliders();
}
Expand Down
Loading