Skip to content

Commit

Permalink
Fixed dimforge#383
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Jun 14, 2023
1 parent c8a10cd commit fb4f69a
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 73 deletions.
6 changes: 6 additions & 0 deletions bevy_rapier2d/examples/custom_system_setup2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
schedule.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackend2,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
Expand All @@ -41,6 +42,11 @@ fn main() {
.in_base_set(PhysicsSet::SyncBackend),
);

schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend2)
.in_base_set(PhysicsSet::SyncBackend2),
);

schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackendFlush)
.in_base_set(PhysicsSet::SyncBackendFlush),
Expand Down
6 changes: 6 additions & 0 deletions bevy_rapier3d/examples/custom_system_setup3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
schedule.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackend2,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
Expand All @@ -41,6 +42,11 @@ fn main() {
.in_base_set(PhysicsSet::SyncBackend),
);

schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend2)
.in_base_set(PhysicsSet::SyncBackend2),
);

schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackendFlush)
.in_base_set(PhysicsSet::SyncBackendFlush),
Expand Down
95 changes: 95 additions & 0 deletions bevy_rapier3d/examples/super_fast3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

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)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(RapierDebugRenderPlugin::default())
.add_startup_system(setup_simulation)
// .add_system(update_cam_pos)
.run();
}

const VEL_Y: f32 = 1000.0;
const TOP_CUBE_DIFF_VEL: f32 = -1.0;

#[derive(Component)]
struct FollowMe;

fn setup_simulation(mut commands: Commands) {
// Right side - independent entities

commands
.spawn((
TransformBundle::from(Transform::from_xyz(1.0, 0.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(180.0, 1.0, 0.3)),
Velocity {
linvel: Vec3::new(0.0, VEL_Y, 0.0),
angvel: Vec3::new(0.0, 0.0, 0.0),
},
GravityScale(0.0),
Ccd::enabled(),
))
.with_children(|child| {
child.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 5.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(220.0, 1.0, 0.3)),
Ccd::enabled(),
FollowMe,
Velocity::linear(Vec3::new(0.0, TOP_CUBE_DIFF_VEL, 0.0)),
GravityScale(0.0),
));

child.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 10.0, 10.0)
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..Default::default()
});
});

// Left side - independent entities are fine

commands.spawn((
TransformBundle::from(Transform::from_xyz(-1.0, 0.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(180.0, 1.0, 0.3)),
Velocity {
linvel: Vec3::new(0.0, VEL_Y, 0.0),
angvel: Vec3::new(0.0, 0.0, 0.0),
},
GravityScale(0.0),
Ccd::enabled(),
));

commands.spawn((
TransformBundle::from(Transform::from_xyz(-1.0, 5.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(220.0, 1.0, 0.3)),
Ccd::enabled(),
Velocity {
linvel: Vec3::new(0.0, VEL_Y + TOP_CUBE_DIFF_VEL, 0.0),
angvel: Vec3::new(0.0, 0.0, 0.0),
},
GravityScale(0.0),
));
}

fn update_cam_pos(
follow_me: Query<&GlobalTransform, (Without<Camera>, With<FollowMe>)>,
mut cam_trans: Query<&mut Transform, With<Camera>>,
) {
let trans = follow_me.get_single().unwrap().translation();
cam_trans.get_single_mut().unwrap().translation = Vec3::new(0.0, trans.y + 10.0, 10.0);
}
18 changes: 16 additions & 2 deletions src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,18 @@ where
.after(systems::update_character_controls)
.in_set(PropagateTransformsSet)
.in_set(RapierTransformPropagateSet),
)
.into_configs(),
PhysicsSet::SyncBackend2 => (
systems::init_async_colliders.after(RapierTransformPropagateSet),
systems::apply_scale.after(systems::init_async_colliders),
systems::apply_collider_user_changes.after(systems::apply_scale),
systems::apply_rigid_body_user_changes.after(systems::apply_collider_user_changes),
systems::apply_joint_user_changes.after(systems::apply_rigid_body_user_changes),
systems::init_rigid_bodies.after(systems::apply_joint_user_changes),
systems::sync_velocity.after(systems::init_rigid_bodies),
systems::init_colliders
.after(systems::init_rigid_bodies)
.after(systems::sync_velocity)
.after(systems::init_async_colliders),
systems::init_joints.after(systems::init_colliders),
systems::apply_initial_rigid_body_impulses.after(systems::init_colliders),
Expand Down Expand Up @@ -145,7 +149,11 @@ pub enum PhysicsSet {
/// initializing) backend data structures with current component state.
/// These systems typically run at the after [`CoreSet::Update`].
SyncBackend,
/// The copy of [`apply_system_buffers`] that runs immediately after [`PhysicsSet::SyncBackend`].
/// This set runs the systems responsible for synchronizing (and
/// initializing) backend data structures with current component state.
/// These systems typically run immediately after [`PhysicsSet::SyncBackend`].
SyncBackend2,
/// The copy of [`apply_system_buffers`] that runs immediately after [`PhysicsSet::SyncBackend2`].
SyncBackendFlush,
/// The systems responsible for advancing the physics simulation, and
/// updating the internal state for scene queries.
Expand Down Expand Up @@ -205,6 +213,7 @@ where
app.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackend2,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
Expand All @@ -217,6 +226,11 @@ where
app.add_systems(
Self::get_systems(PhysicsSet::SyncBackend).in_base_set(PhysicsSet::SyncBackend),
);

app.add_systems(
Self::get_systems(PhysicsSet::SyncBackend2).in_base_set(PhysicsSet::SyncBackend2),
);

app.add_systems(
Self::get_systems(PhysicsSet::SyncBackendFlush)
.in_base_set(PhysicsSet::SyncBackendFlush),
Expand Down
Loading

0 comments on commit fb4f69a

Please sign in to comment.