Skip to content

Commit

Permalink
Allow using XPBD with substep timestep
Browse files Browse the repository at this point in the history
  • Loading branch information
idanarye committed Mar 27, 2024
1 parent 57cd979 commit ac1ae02
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions demos/src/app_setup_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl AppSetupConfiguration {
pub enum ScheduleToUse {
Update,
FixedUpdate,
#[cfg(feature = "xpbd")]
PhysicsSchedule,
}

impl ScheduleToUse {
Expand Down
18 changes: 17 additions & 1 deletion demos/src/bin/platformer_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy_tnua_rapier2d::*;
#[cfg(feature = "xpbd2d")]
use bevy_tnua_xpbd2d::*;
#[cfg(feature = "xpbd2d")]
use bevy_xpbd_2d::{prelude as xpbd, prelude::*};
use bevy_xpbd_2d::{prelude as xpbd, prelude::*, PhysicsSchedule};

use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_control_systems::platformer_control_systems::{
Expand Down Expand Up @@ -53,6 +53,10 @@ fn main() {
// bevy-tnua-rapier2d.
app.add_plugins(TnuaRapier2dPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
panic!("Cannot happen - XPBD and Rapier used together");
}
}
}
#[cfg(feature = "xpbd2d")]
Expand All @@ -69,6 +73,11 @@ fn main() {
app.add_plugins(PhysicsPlugins::new(FixedUpdate));
app.add_plugins(TnuaXpbd2dPlugin::new(FixedUpdate));
}
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(PhysicsPlugins::default());
app.insert_resource(Time::new_with(Physics::fixed_hz(144.0)));
app.add_plugins(TnuaXpbd2dPlugin::new(PhysicsSchedule));
}
}
}

Expand All @@ -85,6 +94,11 @@ fn main() {
app.add_plugins(TnuaControllerPlugin::new(FixedUpdate));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(TnuaControllerPlugin::new(PhysicsSchedule));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(PhysicsSchedule));
}
}

app.add_plugins(tnua_demos_crate::ui::DemoUi::<
Expand All @@ -100,6 +114,8 @@ fn main() {
match app_setup_configuration.schedule_to_use {
ScheduleToUse::Update => Update.intern(),
ScheduleToUse::FixedUpdate => FixedUpdate.intern(),
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => PhysicsSchedule.intern(),
},
apply_platformer_controls.in_set(TnuaUserControlsSystemSet),
);
Expand Down
18 changes: 17 additions & 1 deletion demos/src/bin/platformer_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy_tnua_rapier3d::*;
#[cfg(feature = "xpbd3d")]
use bevy_tnua_xpbd3d::*;
#[cfg(feature = "xpbd3d")]
use bevy_xpbd_3d::{prelude as xpbd, prelude::*};
use bevy_xpbd_3d::{prelude as xpbd, prelude::*, PhysicsSchedule};

use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_animating_systems::platformer_animating_systems::{
Expand Down Expand Up @@ -54,6 +54,10 @@ fn main() {
app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default().in_fixed_schedule());
app.add_plugins(TnuaRapier3dPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
panic!("Cannot happen - XPBD and Rapier used together");
}
}
}
#[cfg(feature = "xpbd3d")]
Expand All @@ -69,6 +73,11 @@ fn main() {
app.add_plugins(PhysicsPlugins::new(FixedUpdate));
app.add_plugins(TnuaXpbd3dPlugin::new(FixedUpdate));
}
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(PhysicsPlugins::default());
app.insert_resource(Time::new_with(Physics::fixed_hz(144.0)));
app.add_plugins(TnuaXpbd3dPlugin::new(PhysicsSchedule));
}
}
}

Expand All @@ -85,6 +94,11 @@ fn main() {
app.add_plugins(TnuaControllerPlugin::new(FixedUpdate));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(TnuaControllerPlugin::new(PhysicsSchedule));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(PhysicsSchedule));
}
}

app.add_plugins(tnua_demos_crate::ui::DemoUi::<
Expand All @@ -100,6 +114,8 @@ fn main() {
match app_setup_configuration.schedule_to_use {
ScheduleToUse::Update => Update.intern(),
ScheduleToUse::FixedUpdate => FixedUpdate.intern(),
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => PhysicsSchedule.intern(),
},
apply_platformer_controls.in_set(TnuaUserControlsSystemSet),
);
Expand Down
18 changes: 17 additions & 1 deletion demos/src/bin/shooter_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_tnua_rapier3d::*;
#[cfg(feature = "xpbd3d")]
use bevy_tnua_xpbd3d::*;
#[cfg(feature = "xpbd3d")]
use bevy_xpbd_3d::{prelude as xpbd, prelude::*};
use bevy_xpbd_3d::{prelude as xpbd, prelude::*, PhysicsSchedule};

use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_animating_systems::platformer_animating_systems::{
Expand Down Expand Up @@ -56,6 +56,10 @@ fn main() {
app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default().in_fixed_schedule());
app.add_plugins(TnuaRapier3dPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
panic!("Cannot happen - XPBD and Rapier used together");
}
}
}
#[cfg(feature = "xpbd3d")]
Expand All @@ -71,6 +75,11 @@ fn main() {
app.add_plugins(PhysicsPlugins::new(FixedUpdate));
app.add_plugins(TnuaXpbd3dPlugin::new(FixedUpdate));
}
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(PhysicsPlugins::default());
app.insert_resource(Time::new_with(Physics::fixed_hz(144.0)));
app.add_plugins(TnuaXpbd3dPlugin::new(PhysicsSchedule));
}
}
}

Expand All @@ -87,6 +96,11 @@ fn main() {
app.add_plugins(TnuaControllerPlugin::new(FixedUpdate));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(FixedUpdate));
}
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => {
app.add_plugins(TnuaControllerPlugin::new(PhysicsSchedule));
app.add_plugins(TnuaCrouchEnforcerPlugin::new(PhysicsSchedule));
}
}

app.add_plugins(tnua_demos_crate::ui::DemoUi::<
Expand All @@ -103,6 +117,8 @@ fn main() {
match app_setup_configuration.schedule_to_use {
ScheduleToUse::Update => Update.intern(),
ScheduleToUse::FixedUpdate => FixedUpdate.intern(),
#[cfg(feature = "xpbd")]
ScheduleToUse::PhysicsSchedule => PhysicsSchedule.intern(),
},
apply_platformer_controls.in_set(TnuaUserControlsSystemSet),
);
Expand Down
3 changes: 2 additions & 1 deletion xpbd2d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bevy_tnua_physics_integration_layer::data_for_backends::{
use bevy_tnua_physics_integration_layer::math::*;
use bevy_tnua_physics_integration_layer::subservient_sensors::TnuaSubservientSensor;
use bevy_xpbd_2d::math::{AdjustPrecision, AsF32};
use bevy_xpbd_2d::prelude::*;
use bevy_xpbd_2d::{prelude::*, PhysicsStepSet};

use bevy_tnua_physics_integration_layer::*;

Expand Down Expand Up @@ -46,6 +46,7 @@ impl Plugin for TnuaXpbd2dPlugin {
self.schedule,
TnuaSystemSet
.before(PhysicsSet::Prepare)
.before(PhysicsStepSet::BroadPhase)
.run_if(|physics_time: Res<Time<Physics>>| !physics_time.is_paused()),
);
app.add_systems(
Expand Down
3 changes: 2 additions & 1 deletion xpbd3d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy_tnua_physics_integration_layer::math::AdjustPrecision;
use bevy_tnua_physics_integration_layer::math::AsF32;
use bevy_tnua_physics_integration_layer::math::Float;
use bevy_tnua_physics_integration_layer::math::Vector3;
use bevy_xpbd_3d::prelude::*;
use bevy_xpbd_3d::{prelude::*, PhysicsStepSet};

use bevy_tnua_physics_integration_layer::data_for_backends::TnuaGhostPlatform;
use bevy_tnua_physics_integration_layer::data_for_backends::TnuaGhostSensor;
Expand Down Expand Up @@ -51,6 +51,7 @@ impl Plugin for TnuaXpbd3dPlugin {
self.schedule,
TnuaSystemSet
.before(PhysicsSet::Prepare)
.before(PhysicsStepSet::BroadPhase)
.run_if(|physics_time: Res<Time<Physics>>| !physics_time.is_paused()),
);
app.add_systems(
Expand Down

0 comments on commit ac1ae02

Please sign in to comment.