fixed timestep physics?

This commit is contained in:
Jay Christy
2023-10-27 15:56:22 -04:00
parent ec9b3ac014
commit 3c7a65c58f

View File

@@ -1,11 +1,12 @@
use bevy::{ use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::schedule::ScheduleLabel,
log::info, log::info,
prelude::{ prelude::{
default, shape, App, Assets, Color, Commands, Component, Entity, Event, EventReader, default, shape, App, Assets, Color, Commands, Component, Entity, Event, EventReader,
EventWriter, GlobalTransform, IntoSystemConfigs, IntoSystemSetConfigs, Mesh, PbrBundle, EventWriter, FixedTime, FixedUpdate, GlobalTransform, IntoSystemConfigs,
PostUpdate, Query, Res, ResMut, Resource, SpatialBundle, StandardMaterial, Startup, IntoSystemSetConfigs, Mesh, PbrBundle, PostUpdate, Query, Res, ResMut, Resource, Schedule,
Transform, Update, Vec3, With, Without, SpatialBundle, StandardMaterial, Startup, Transform, Update, Vec3, With, Without, World,
}, },
time::{Time, Timer}, time::{Time, Timer},
transform::TransformSystem, transform::TransformSystem,
@@ -90,6 +91,18 @@ fn main() {
.add_systems(Update, update_physics_hands); .add_systems(Update, update_physics_hands);
//configure rapier sets //configure rapier sets
let mut physics_schedule = Schedule::new(PhysicsSchedule);
physics_schedule.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
.chain()
.before(TransformSystem::TransformPropagate),
);
app.configure_sets( app.configure_sets(
PostUpdate, PostUpdate,
( (
@@ -101,24 +114,39 @@ fn main() {
.before(TransformSystem::TransformPropagate), .before(TransformSystem::TransformPropagate),
); );
//add rapier systems //add rapier systems
app.add_systems( physics_schedule.add_systems((
PostUpdate, RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend)
( .in_set(PhysicsSet::SyncBackend),
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend) RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::StepSimulation)
.in_set(PhysicsSet::SyncBackend), .in_set(PhysicsSet::StepSimulation),
( RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::Writeback)
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::StepSimulation), .in_set(PhysicsSet::Writeback),
// despawn_one_box, ));
) app.add_schedule(physics_schedule) // configure our fixed timestep schedule to run at the rate we want
.in_set(PhysicsSet::StepSimulation), .insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::Writeback) .add_systems(FixedUpdate, run_physics_schedule)
.in_set(PhysicsSet::Writeback), .add_systems(Startup, configure_physics);
),
);
app.run(); app.run();
} }
//fixed timesteps?
const FIXED_TIMESTEP: f32 = 1. / 90.;
// A label for our new Schedule!
#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
struct PhysicsSchedule;
fn run_physics_schedule(world: &mut World) {
world.run_schedule(PhysicsSchedule);
}
fn configure_physics(mut rapier_config: ResMut<RapierConfiguration>) {
rapier_config.timestep_mode = TimestepMode::Fixed {
dt: FIXED_TIMESTEP,
substeps: 1,
}
}
fn spawn_controllers_example(mut commands: Commands) { fn spawn_controllers_example(mut commands: Commands) {
//left hand //left hand
commands.spawn(( commands.spawn((