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

Multiple worlds/pipelines #328

Closed
wants to merge 114 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
d671d7a
Refactored context to have multiple worlds
AnthonyTornetta Feb 10, 2023
cdfafa4
Supporting multiple worlds
AnthonyTornetta Feb 11, 2023
2010f94
Adding example + reworking to be faster. Disabled custom events for now
AnthonyTornetta Feb 13, 2023
d6fae15
Multiple worlds work together
AnthonyTornetta Feb 13, 2023
55f2745
Improved example
AnthonyTornetta Feb 13, 2023
1cb6d8c
Added docs
AnthonyTornetta Feb 13, 2023
2eed336
Clippy --fix
AnthonyTornetta Feb 13, 2023
23f8046
Added support for new changing worlds
AnthonyTornetta Feb 14, 2023
83816cd
Making imports work
AnthonyTornetta Feb 15, 2023
4b99318
Fixed imports
AnthonyTornetta Feb 15, 2023
e5af080
Removed error when removing DEFAULT_WORLD_ID
AnthonyTornetta Feb 20, 2023
a0dfceb
Fixed improper swapping of worlds
AnthonyTornetta Feb 20, 2023
d1dd55d
testing please ignore
AnthonyTornetta Feb 23, 2023
c534146
Fixed incorrect after
AnthonyTornetta Feb 23, 2023
8b32f80
Made BodyWorld changes bubble down to children.
AnthonyTornetta Mar 1, 2023
b36c5dc
Testing ReadMass changes
AnthonyTornetta Mar 6, 2023
455de13
Added debug statements to master
AnthonyTornetta Mar 6, 2023
a1a0a8f
More debug prints
AnthonyTornetta Mar 6, 2023
1461aa6
Fixing bubble up
AnthonyTornetta Mar 6, 2023
ce8c335
Commented out useless code
AnthonyTornetta Mar 6, 2023
d6d378f
Debugging
AnthonyTornetta Mar 6, 2023
e52085c
Merge remote-tracking branch 'upstream/master' into multiple-worlds
AnthonyTornetta Mar 7, 2023
ed07c1b
Merge branch 'multiple-worlds'
AnthonyTornetta Mar 7, 2023
87161ab
Got bevy events working
AnthonyTornetta Mar 9, 2023
4fb4e7b
Clippy
AnthonyTornetta Mar 9, 2023
bc49212
Merge branch 'dimforge:master' into master
AnthonyTornetta Mar 13, 2023
a700a5e
Fixed merge issue
AnthonyTornetta Mar 13, 2023
6296f3e
Fixed system ordering to support switching worlds
AnthonyTornetta Mar 15, 2023
2bbb03e
Renamed BodyWorld to PhysicsWorld
AnthonyTornetta Mar 22, 2023
1fa4c15
Debugging deleted colliders
AnthonyTornetta Mar 29, 2023
aca20f4
Merge branch 'master' of https://github.com/AnthonyTornetta/bevy_rapier
AnthonyTornetta Mar 29, 2023
8a98afd
Removed debug print
AnthonyTornetta Mar 29, 2023
97b849e
Fixed #285
AnthonyTornetta Mar 29, 2023
5222375
Updated to own version of rapier
AnthonyTornetta Mar 30, 2023
78a988a
Merge branch 'dimforge:master' into master
AnthonyTornetta Apr 13, 2023
8f03231
Merge branch 'dimforge:master' into multiple-worlds
AnthonyTornetta Apr 13, 2023
ae56257
Fixed bug where removed entities didn't have a world component.
AnthonyTornetta May 31, 2023
dbc60bd
Merge branch 'master' into multiple-worlds
AnthonyTornetta May 31, 2023
596d44b
Reverted custom rapier version change
AnthonyTornetta May 31, 2023
c85a9f1
Clippy --fix
AnthonyTornetta May 31, 2023
f310885
Cargo clippy --fix
AnthonyTornetta May 31, 2023
c8a10cd
Merge branch 'master' into multiple-worlds
AnthonyTornetta May 31, 2023
fb4f69a
Fixed #383
AnthonyTornetta Jun 14, 2023
69f3f3e
Reverted version revert
AnthonyTornetta Jun 14, 2023
9af3f45
Removed useless system
AnthonyTornetta Jun 14, 2023
1a37074
Fixing stuff
AnthonyTornetta Jun 15, 2023
824f168
Added debug prints
AnthonyTornetta Jun 15, 2023
a50235a
System reordering
AnthonyTornetta Jun 15, 2023
ee5460a
Revert "System reordering"
AnthonyTornetta Jun 15, 2023
a21969a
Revert "Fixed #383"
AnthonyTornetta Jun 15, 2023
524d64c
Swapped to my version of rapier
AnthonyTornetta Jun 15, 2023
53f9f67
Kinda got it working (ish)
AnthonyTornetta Jun 16, 2023
78fc43c
Parnet + child relationships are now not super buggy
AnthonyTornetta Jun 16, 2023
071287d
It *works*
AnthonyTornetta Jun 19, 2023
b08cad8
Removed printlns
AnthonyTornetta Jun 20, 2023
45e69d2
Warnings be gone
AnthonyTornetta Jun 20, 2023
3161c99
removed me
AnthonyTornetta Jun 20, 2023
22ce456
Redoing fix
AnthonyTornetta Jun 22, 2023
b38f5fd
it no work
AnthonyTornetta Jun 22, 2023
e69d56d
Revert "it no work"
AnthonyTornetta Jun 22, 2023
7714c24
Revert "Redoing fix"
AnthonyTornetta Jun 22, 2023
7e044fd
"Working" for static bodies as children
AnthonyTornetta Jun 23, 2023
761b506
Parent/child relationships properly work now
AnthonyTornetta Jun 23, 2023
36e79d7
Fixed accidental overwriting of world_offset
AnthonyTornetta Jun 30, 2023
9850a3f
Merge remote-tracking branch 'upstream/master' into child_parent_fix_…
AnthonyTornetta Jul 14, 2023
6bb69e0
Merge branch 'child_parent_fix_vel_edition' of https://github.com/Ant…
AnthonyTornetta Jul 14, 2023
e837557
Merge branch 'child_parent_fix_vel_edition' into update-master
AnthonyTornetta Jul 14, 2023
3b52e99
Removed dead code
AnthonyTornetta Jul 14, 2023
90eb3d8
Downgraded rapier for this version
AnthonyTornetta Jul 14, 2023
3de1455
Updated Examples
AnthonyTornetta Jul 14, 2023
18d39ca
Updated Examples
AnthonyTornetta Jul 14, 2023
09622de
Fixed warnings
AnthonyTornetta Jul 14, 2023
33ea9e4
Fixed warnings
AnthonyTornetta Jul 14, 2023
67c65bc
Merge branch 'dimforge/master'
AnthonyTornetta Sep 14, 2023
759eb7e
Fixed warnings
AnthonyTornetta Sep 14, 2023
417a44a
Fixed compiler warning
AnthonyTornetta Sep 14, 2023
fe8b5f4
cargo clippy --fix
AnthonyTornetta Sep 14, 2023
3537322
Fixed most clippy issues
AnthonyTornetta Sep 14, 2023
54633e0
Merge branch 'master' of https://github.com/AnthonyTornetta/bevy_rapier
AnthonyTornetta Sep 14, 2023
d859070
Merge branch 'master' into multiple-worlds
AnthonyTornetta Sep 15, 2023
c223be8
Removed private modifier because modifying this is used to work aroun…
AnthonyTornetta Sep 15, 2023
ab2aa0b
Merge branch 'master' into multiple-worlds
AnthonyTornetta Sep 15, 2023
72e0acb
Debugging system order
AnthonyTornetta Sep 15, 2023
932b6d5
Updating system ordering to use chain
AnthonyTornetta Sep 15, 2023
b7ee234
Testing system ordering
AnthonyTornetta Sep 15, 2023
a63c6ac
Merge branch 'testing'
AnthonyTornetta Sep 15, 2023
38565b1
Fixed system ordering
AnthonyTornetta Sep 15, 2023
7c266fe
Fixed system ordering
AnthonyTornetta Sep 15, 2023
c9ea6f9
Removed debug prints
AnthonyTornetta Sep 15, 2023
c8d4ccd
Preventing expensive unneeded change detections
AnthonyTornetta Oct 12, 2023
d8360b7
Fixed randomly occuring system ordering issue
AnthonyTornetta Oct 17, 2023
423ab38
Merge branch 'master' into multiple-worlds
AnthonyTornetta Oct 17, 2023
94ca628
Merge remote-tracking branch 'upstream/master'
AnthonyTornetta Nov 18, 2023
3bed38d
Fixed imports
AnthonyTornetta Nov 18, 2023
72d5d15
Merge branch 'master' into multiple-worlds
AnthonyTornetta Nov 18, 2023
6d72181
Merge remote-tracking branch 'upstream/master'
AnthonyTornetta Feb 24, 2024
acd8502
Swapped back to custom version of rapier
AnthonyTornetta Feb 24, 2024
cca2ace
Merge branch 'master' into multiple-worlds
AnthonyTornetta Feb 24, 2024
29a934d
Fixed wrong world-swapping logic
AnthonyTornetta Feb 25, 2024
4738916
Formatting
AnthonyTornetta May 29, 2024
e737b9e
Merge remote-tracking branch 'upstream/master' into multiple-worlds
AnthonyTornetta May 29, 2024
30b61be
Merge character controller
AnthonyTornetta May 29, 2024
939fbab
Merge joints
AnthonyTornetta May 29, 2024
0f081c0
Merge remove
AnthonyTornetta May 29, 2024
ca5d879
Merge writeback
AnthonyTornetta May 29, 2024
46d266a
Merge collider
AnthonyTornetta May 29, 2024
2b5ec29
Merge rigid_body
AnthonyTornetta May 29, 2024
70d0f8d
Merge mod
AnthonyTornetta May 29, 2024
197fd34
Merge world
AnthonyTornetta May 29, 2024
f744377
Clippy fix
AnthonyTornetta May 29, 2024
5bcb4d3
Reworked world syncing to be more reliable
AnthonyTornetta May 29, 2024
f2ddf29
Fixed ambiguous system ordering that could cause issues
AnthonyTornetta May 29, 2024
0b66c3f
Added gravity builder method to make creation of worlds easier
AnthonyTornetta May 29, 2024
643f8b3
Added missing scale multiply
AnthonyTornetta May 29, 2024
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
8 changes: 6 additions & 2 deletions bevy_rapier2d/examples/player_movement2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ fn main() {
#[derive(Component)]
pub struct Player(f32);

pub fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
pub fn spawn_player(mut commands: Commands, mut rapier_context: ResMut<RapierContext>) {
// Set gravity to 0.0 and spawn camera.
rapier_config.gravity = Vec2::ZERO;
rapier_context
.get_world_mut(DEFAULT_WORLD_ID)
.expect("Default world should exist")
.set_gravity(Vec2::ZERO);

commands.spawn(Camera2dBundle::default());

let sprite_size = 100.0;
Expand Down
13 changes: 6 additions & 7 deletions bevy_rapier2d/examples/testbed2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,12 @@ fn main() {
)
.add_systems(
OnExit(Examples::PlayerMovement2),
(
cleanup,
|mut rapier_config: ResMut<RapierConfiguration>, ctxt: Res<RapierContext>| {
rapier_config.gravity =
RapierConfiguration::new(ctxt.integration_parameters.length_unit).gravity;
},
),
(cleanup, |mut rapier_context: ResMut<RapierContext>| {
rapier_context
.get_world_mut(DEFAULT_WORLD_ID)
.expect("Default world should exist")
.set_gravity(Vect::Y * -9.81)
}),
)
//
//testbed
Expand Down
103 changes: 103 additions & 0 deletions bevy_rapier3d/examples/change_world3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

const N_WORLDS: usize = 5;
const WORLD_CHANGE_DELAY_SEC: f32 = 3.0;

#[derive(Component)]
/// Denotes which object(s) to change the world of
struct ChangeWorld;

fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(Update, change_world)
.run();
}

fn change_world(mut query: Query<&mut PhysicsWorld, With<ChangeWorld>>, time: Res<Time>) {
for mut bw in query.iter_mut() {
if time.elapsed_seconds() > (bw.world_id.0 as f32 + 1.0) * WORLD_CHANGE_DELAY_SEC {
let new_world_id = bw.world_id.0 + 1;

if new_world_id != N_WORLDS {
println!("Changing world to {new_world_id}.");
bw.world_id = WorldId::new(new_world_id);
}
}
}
}

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()
});
}

pub fn setup_physics(mut context: ResMut<RapierContext>, mut commands: Commands) {
for _ in 1..N_WORLDS {
context.add_world(RapierWorld::default());
}

for world_id in 0..N_WORLDS {
let color = [
Color::hsl(220.0, 1.0, 0.3),
Color::hsl(180.0, 1.0, 0.3),
Color::hsl(260.0, 1.0, 0.7),
][world_id % 3];

/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;

commands.spawn((
TransformBundle::from(Transform::from_xyz(
0.0,
(world_id as f32) * -0.5 - ground_height,
0.0,
)),
Collider::cuboid(ground_size, ground_height, ground_size),
ColliderDebugColor(color),
RigidBody::Fixed,
PhysicsWorld {
world_id: WorldId::new(world_id),
},
));
}

/*
* Create the cube
*
* The child is just there to show that physics world changes will also change the children.
*/
commands
.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 3.0, 0.0)),
RigidBody::Dynamic,
PhysicsWorld {
world_id: DEFAULT_WORLD_ID,
},
ChangeWorld,
))
.with_children(|p| {
p.spawn((
TransformBundle::from_transform(Transform::from_xyz(0.0, 0.0, 0.0)),
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(260.0, 1.0, 0.7)),
));
});
}
109 changes: 109 additions & 0 deletions bevy_rapier3d/examples/multi_world3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

const N_WORLDS: usize = 5;

fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_physics, setup_graphics))
.add_systems(Update, move_middle_world)
// .add_systems(Update, change_world)
// .add_systems(Update, despawn_last)
.run();
}

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()
});
}

#[derive(Component)]
struct Platform {
starting_y: f32,
}

fn move_middle_world(
time: Res<Time>,
mut query: Query<(&mut Transform, &PhysicsWorld, &Platform)>,
) {
for (mut transform, world, platform) in query.iter_mut() {
if world.world_id.0 == N_WORLDS / 2 {
transform.translation.y = platform.starting_y + -time.elapsed_seconds().sin();
}
}
}

/// Demonstrates despawning an entity removing it from its world
// fn despawn_last(query: Query<(&PhysicsWorld, Entity)>, mut commands: Commands) {
// for (bw, entity) in query.iter() {
// if bw.world_id == N_WORLDS - 1 {
// commands.entity(entity).despawn_recursive();
// }
// }
// }

/// Demonstrates how easy it is to move one entity to another world.
// fn change_world(mut query: Query<&mut PhysicsWorld>) {
// for mut bw in query.iter_mut() {
// if bw.world_id == 1 {
// bw.world_id = 0;
// }
// }
// }

pub fn setup_physics(mut context: ResMut<RapierContext>, mut commands: Commands) {
for _ in 1..N_WORLDS {
context.add_world(RapierWorld::default());
}

for world_id in 0..N_WORLDS {
let world_id = WorldId::new(world_id);

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

/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;

let starting_y = (world_id.0 as f32) * -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),
Platform { starting_y },
PhysicsWorld { world_id },
));

/*
* Create the cube
*/

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 1.0 + world_id.0 as f32 * 5.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(color),
PhysicsWorld { world_id },
));
}
}
17 changes: 10 additions & 7 deletions bevy_rapier3d/examples/ray_casting3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ pub fn cast_ray(
};

// Then cast the ray.
let hit = rapier_context.cast_ray(
ray.origin,
ray.direction.into(),
f32::MAX,
true,
QueryFilter::only_dynamic(),
);
let hit = rapier_context
.cast_ray(
DEFAULT_WORLD_ID,
ray.origin,
*ray.direction,
f32::MAX,
true,
QueryFilter::only_dynamic(),
)
.expect("Default world should exist.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This user facing API change is unfortunate, I'd like to discuss if alternatives could be achievable, more details in main PR comment feed.


if let Some((entity, _toi)) = hit {
// Color in blue the entity we just hit.
Expand Down
8 changes: 4 additions & 4 deletions src/control/character_controller.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::geometry::{Collider, CollisionGroups, ShapeCastHit};
use crate::math::{Real, Rot, Vect};
use crate::plugin::context::RapierWorld;
use bevy::prelude::*;

use crate::plugin::RapierContext;
pub use rapier::control::CharacterAutostep;
pub use rapier::control::CharacterLength;
use rapier::prelude::{ColliderSet, QueryFilterFlags};
Expand All @@ -26,18 +26,18 @@ pub struct CharacterCollision {

impl CharacterCollision {
pub(crate) fn from_raw(
ctxt: &RapierContext,
world: &RapierWorld,
c: &rapier::control::CharacterCollision,
) -> Option<Self> {
Self::from_raw_with_set(&ctxt.colliders, c, true)
Self::from_raw_with_set(&world.colliders, c, true)
}

pub(crate) fn from_raw_with_set(
colliders: &ColliderSet,
c: &rapier::control::CharacterCollision,
details_always_computed: bool,
) -> Option<Self> {
RapierContext::collider_entity_with_set(colliders, c.handle).map(|entity| {
RapierWorld::collider_entity_with_set(colliders, c.handle).map(|entity| {
CharacterCollision {
entity,
character_translation: c.character_pos.translation.vector.into(),
Expand Down
11 changes: 10 additions & 1 deletion src/dynamics/rigid_body.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::math::Vect;
use crate::plugin::context::WorldId;
use bevy::prelude::*;
use rapier::prelude::{
Isometry, LockedAxes as RapierLockedAxes, RigidBodyActivation, RigidBodyHandle, RigidBodyType,
Expand Down Expand Up @@ -178,7 +179,7 @@ impl Default for AdditionalMassProperties {
/// This only reads the mass from entities with a [`RigidBody`] component.
#[derive(Copy, Clone, Debug, Default, PartialEq, Component, Reflect)]
#[reflect(Component, PartialEq)]
pub struct ReadMassProperties(MassProperties);
pub struct ReadMassProperties(pub MassProperties);

impl ReadMassProperties {
/// Get the [`MassProperties`] of this [`RigidBody`].
Expand Down Expand Up @@ -458,6 +459,14 @@ impl Default for GravityScale {
}
}

/// Denotes which world this body is a part of. If omitted, the default world is assumed.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Component, Reflect)]
#[reflect(Component, PartialEq)]
pub struct PhysicsWorld {
/// The world which this body is in. Use DEFAULT_WORLD_ID for the default world.
pub world_id: WorldId,
}

/// Information used for Continuous-Collision-Detection.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Component, Reflect)]
#[reflect(Component, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions src/geometry/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ bitflags::bitflags! {

/// All of the groups.
const ALL = u32::MAX;

/// None of the groups.
const NONE = 0;
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#[cfg(feature = "serde-serialize")]
extern crate serde;

/// nalgebra used by bevy_rapier
pub extern crate nalgebra as na;
#[cfg(feature = "dim2")]
/// rapier2d used by bevy_rapier
pub extern crate rapier2d as rapier;
#[cfg(feature = "dim3")]
/// rapier3d used by bevy_rapier
pub extern crate rapier3d as rapier;
pub use rapier::parry;

Expand Down Expand Up @@ -66,6 +69,8 @@ pub mod render;
/// Miscellaneous helper functions.
pub mod utils;

// pub use crate::plugin::context::{RapierWorld, WorldId, DEFAULT_WORLD_ID};

/// Groups the most often used types.
pub mod prelude {
pub use crate::control::*;
Expand Down
Loading