Skip to content

Commit

Permalink
avoid using term 'world' when referring to a physics context, to avoi…
Browse files Browse the repository at this point in the history
…d confusion with bevy ecs World
  • Loading branch information
Vrixyz committed Nov 18, 2024
1 parent a8c94b4 commit e302ed9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
36 changes: 18 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,36 @@ which was its hardcoded behaviour.

### Modified

- Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details.
- `RapierContext`, `RapierConfiguration` and `SimulationToRenderTime` are no longer `Resource`s.
- They have been split in multiple `Component`s:
- `RapierContextColliders`
- `RapierContextJoints`
- `RapierContextSimulation`
- `RapierRigidBodySet`
- `SimulationToRenderTime`
- `RapierConfiguration`
- Migration guide:
- `ResMut<mut RapierContext>` -> `WriteRapierContext`
- `Res<RapierContext>` -> `ReadRapierContext`
- Access to `RapierConfiguration` and `SimulationToRenderTime` should query for it
on the responsible entity owning the `RenderContext`.
- See [`ray_casting`](bevy_rapier3d/examples/ray_casting3.rs) example for a usage example.
- Rapier now supports multiple independent physics contexts, see example `multi_contexts3` for usage details.
- Each entity managed by bevy_rapier has a `RapierContextEntityLink` pointing to the entity containing the components above.
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too,
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics contexts too,
you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545)
to find more information.
- Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details.
- Rapier now supports multiple independent physics contexts, see example `multi_contexts3` for usage details.
- Migration guide:
- `ResMut<mut RapierContext>` -> `WriteDefaultRapierContext`
- `Res<RapierContext>` -> `ReadDefaultRapierContext`
- Access to `RapierConfiguration` and `RenderToSimulationTime` should query for it
on the responsible entity owning the `RenderContext`.
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too,
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics contexts too,
you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545)
to get more context and information.
- `colliders_with_aabb_intersecting_aabb` now takes `bevy::math::bounding::Aabb3d` (or `[..]::Aabb2d` in 2D) as parameter.
- it is now accessible with `headless` feature enabled.
- `RapierContext`, `RapierConfiguration` and `SimulationToRenderTime` are no longer `Resource`s.
- They have been split in multiple `Component`s:
- `RapierContextColliders`
- `RapierContextJoints`
- `RapierContextSimulation`
- `RapierRigidBodySet`
- `SimulationToRenderTime`
- `RapierConfiguration`
- Migration guide:
- `ResMut<mut RapierContext>` -> `WriteRapierContext`
- `Res<RapierContext>` -> `ReadRapierContext`
- Access to `RapierConfiguration` and `SimulationToRenderTime` should query for it
on the responsible entity owning the `RenderContext`.
- See [`ray_casting`](bevy_rapier3d/examples/ray_casting3.rs) example for a usage example.

## v0.27.0 (07 July 2024)

Expand Down
24 changes: 12 additions & 12 deletions bevy_rapier3d/examples/multi_contexts3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use bevy_rapier3d::prelude::*;

const N_WORLDS: usize = 2;
const N_CONTEXTS: usize = 2;

fn main() {
App::new()
Expand All @@ -18,21 +18,21 @@ fn main() {
))
.add_systems(
Startup,
((create_worlds, setup_physics).chain(), setup_graphics),
((create_contexts, setup_physics).chain(), setup_graphics),
)
.add_systems(Update, move_platforms)
.add_systems(
Update,
change_world.run_if(input_just_pressed(KeyCode::KeyC)),
change_context.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((RapierContextBundle::default(), WorldId(i)));
fn create_contexts(mut commands: Commands) {
for i in 0..N_CONTEXTS {
let mut context = commands.spawn((RapierContextBundle::default(), ContextId(i)));
if i == 0 {
world.insert((DefaultRapierContext, RapierContextBundle::default()));
context.insert((DefaultRapierContext, RapierContextBundle::default()));
}
}
}
Expand All @@ -46,7 +46,7 @@ fn setup_graphics(mut commands: Commands) {
}

#[derive(Component)]
pub struct WorldId(pub usize);
pub struct ContextId(pub usize);

#[derive(Component)]
struct Platform {
Expand All @@ -59,8 +59,8 @@ fn move_platforms(time: Res<Time>, mut query: Query<(&mut Transform, &Platform)>
}
}

/// Demonstrates how easy it is to move one entity to another world.
fn change_world(
/// Demonstrates how easy it is to move one entity to another context.
fn change_context(
query_context: Query<Entity, With<DefaultRapierContext>>,
mut query_links: Query<(Entity, &mut RapierContextEntityLink)>,
) {
Expand All @@ -70,12 +70,12 @@ fn change_world(
continue;
}
link.0 = default_context;
println!("changing world of {} for world {}", e, link.0);
println!("changing context of {} for context {}", e, link.0);
}
}

pub fn setup_physics(
context: Query<(Entity, &WorldId), With<RapierContextSimulation>>,
context: Query<(Entity, &ContextId), With<RapierContextSimulation>>,
mut commands: Commands,
) {
for (context_entity, id) in context.iter() {
Expand Down
10 changes: 5 additions & 5 deletions src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
(
(
systems::on_add_entity_with_parent,
systems::on_change_world,
systems::on_change_context,
systems::sync_removals,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_scene_colliders,
Expand Down Expand Up @@ -252,7 +252,7 @@ where
)
.before(PhysicsSet::SyncBackend),
);
app.add_systems(PreStartup, insert_default_world);
app.add_systems(PreStartup, insert_default_context);

// Add each set as necessary
if self.default_system_setup {
Expand Down Expand Up @@ -309,7 +309,7 @@ pub enum RapierContextInitialization {
/// You are responsible for creating a [`RapierContextBundle`],
/// before spawning any rapier entities (rigidbodies, colliders, joints).
///
/// You might be interested in adding [`DefaultRapierContext`] to the created world.
/// You might be interested in adding [`DefaultRapierContext`] to the created physics context.
NoAutomaticRapierContext,
/// [`RapierPhysicsPlugin`] will spawn an entity containing a [`RapierContextBundle`]
/// automatically during [`PreStartup`], with the [`DefaultRapierContext`] marker component.
Expand All @@ -325,7 +325,7 @@ impl Default for RapierContextInitialization {
}
}

pub fn insert_default_world(
pub fn insert_default_context(
mut commands: Commands,
initialization_data: Res<RapierContextInitialization>,
) {
Expand Down Expand Up @@ -427,7 +427,7 @@ mod test {
.enable()
.set_breakpoint(PostUpdate, systems::on_add_entity_with_parent)
.set_breakpoint(PostUpdate, systems::init_rigid_bodies)
.set_breakpoint(PostUpdate, systems::on_change_world)
.set_breakpoint(PostUpdate, systems::on_change_context)
.set_breakpoint(PostUpdate, systems::sync_removals)
.set_breakpoint(Update, setup_physics);

Expand Down
48 changes: 24 additions & 24 deletions src/plugin/systems/multiple_rapier_contexts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! systems to support multiple worlds, and changes between them.
//! systems to support multiple physics contexts, and changes between them.
use crate::dynamics::{
RapierImpulseJointHandle, RapierMultibodyJointHandle, RapierRigidBodyHandle,
Expand Down Expand Up @@ -52,29 +52,29 @@ fn remove_old_physics(entity: Entity, commands: &mut Commands) {
.remove::<RapierImpulseJointHandle>();
}

/// Flags the entity to have its physics updated to reflect new world
/// Flags the entity to have its physics updated to reflect new context
///
/// Also recursively bubbles down world changes to children & flags them to apply any needed physics changes
pub fn on_change_world(
q_changed_worlds: Query<
/// Also recursively bubbles down context changes to children & flags them to apply any needed physics changes
pub fn on_change_context(
q_changed_contexts: Query<
(Entity, Ref<RapierContextEntityLink>),
Changed<RapierContextEntityLink>,
>,
q_children: Query<&Children>,
q_physics_world: Query<&RapierContextEntityLink>,
q_physics_context: Query<&RapierContextEntityLink>,
q_context: Query<(
&RapierContextColliders,
&RapierContextJoints,
&RapierRigidBodySet,
)>,
mut commands: Commands,
) {
for (entity, new_physics_world) in &q_changed_worlds {
let context = q_context.get(new_physics_world.0);
// Ensure the world actually changed before removing them from the world
for (entity, new_physics_context) in &q_changed_contexts {
let context = q_context.get(new_physics_context.0);
// Ensure the context actually changed before removing them from the context
if !context
.map(|(colliders, joints, rigidbody_set)| {
// They are already apart of this world if any of these are true
// They are already apart of this context if any of these are true
colliders.entity2collider.contains_key(&entity)
|| rigidbody_set.entity2body.contains_key(&entity)
|| joints.entity2impulse_joint.contains_key(&entity)
Expand All @@ -83,46 +83,46 @@ pub fn on_change_world(
.unwrap_or(false)
{
remove_old_physics(entity, &mut commands);
bubble_down_world_change(
bubble_down_context_change(
&mut commands,
entity,
&q_children,
*new_physics_world,
&q_physics_world,
*new_physics_context,
&q_physics_context,
);
}
}
}

fn bubble_down_world_change(
fn bubble_down_context_change(
commands: &mut Commands,
entity: Entity,
q_children: &Query<&Children>,
new_physics_world: RapierContextEntityLink,
q_physics_world: &Query<&RapierContextEntityLink>,
new_physics_context: RapierContextEntityLink,
q_physics_context: &Query<&RapierContextEntityLink>,
) {
let Ok(children) = q_children.get(entity) else {
return;
};

children.iter().for_each(|&child| {
if q_physics_world
if q_physics_context
.get(child)
.map(|x| *x == new_physics_world)
.map(|x| *x == new_physics_context)
.unwrap_or(false)
{
return;
}

remove_old_physics(child, commands);
commands.entity(child).insert(new_physics_world);
commands.entity(child).insert(new_physics_context);

bubble_down_world_change(
bubble_down_context_change(
commands,
child,
q_children,
new_physics_world,
q_physics_world,
new_physics_context,
q_physics_context,
);
});
}
Expand All @@ -144,7 +144,7 @@ mod test {
use rapier::math::Real;

#[test]
pub fn multi_world_hierarchy_update() {
pub fn multi_context_hierarchy_update() {
let mut app = App::new();
app.add_plugins((
HeadlessRenderPlugin,
Expand Down Expand Up @@ -181,7 +181,7 @@ mod test {
RapierRigidBodySet::default(),
))
.id();
// FIXME: We need to wait 1 frame when creating a world.
// FIXME: We need to wait 1 frame when creating a context.
// Ideally we should be able to order the systems so that we don't have to wait.
app.update();
let mut world = app.world_mut();
Expand Down

0 comments on commit e302ed9

Please sign in to comment.