diff --git a/Cargo.toml b/Cargo.toml index 87e7639..494038a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ default = ["openxr/mint", "linked"] linked = ["openxr/linked"] [workspace] -members = [ "examples/android" ] +members = ["examples/android", "examples/demo"] [dependencies] anyhow = "1.0.75" @@ -31,10 +31,7 @@ openxr = { version = "0.17.1", features = ["static"] } bevy = "0.12" color-eyre = "0.6.2" # bevy_rapier3d = { git = "https://github.com/Schmarni-Dev/bevy_rapier" } -bevy_rapier3d = { git = "https://github.com/alexichepura/bevy_rapier", version = "0.22.0", branch = "bevy-012"} - -[workspace] -members = ["examples/demo"] +bevy_rapier3d = { git = "https://github.com/devil-ira/bevy_rapier", version = "0.22.0", branch = "bevy-0.12" } [[example]] name = "xr" diff --git a/examples/demo/Cargo.toml b/examples/demo/Cargo.toml index 69d7776..e53a0b1 100644 --- a/examples/demo/Cargo.toml +++ b/examples/demo/Cargo.toml @@ -3,22 +3,13 @@ name = "demo" version = "0.1.0" edition = "2021" - -[lib] -crate-type = ["cdylib","lib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib", "lib"] + [dependencies] -bevy = { git = "https://github.com/bevyengine/bevy.git" } -# bevy = "0.11.3" -# default-features is false because it for some reason complains when trying to statically link openxr -bevy_openxr = { path = "../../", default-features = false} -# bevy_openxr = { git = "https://github.com/Schmarni-Dev/bevy_openxr", default-features = false, branch = "demo"} -bevy_rapier3d = { git = "https://github.com/Schmarni-Dev/bevy_rapier" } +bevy = "0.12" +bevy_oxr = { path = "../../", default-features = false } +bevy_rapier3d = { git = "https://github.com/devil-ira/bevy_rapier", branch = "bevy-0.12" } color-eyre = "0.6.2" - -[profile.release] -lto = "fat" -codegen-units = 1 -panic = "abort" - diff --git a/examples/demo/src/lib.rs b/examples/demo/src/lib.rs index 54d9766..2b9a989 100644 --- a/examples/demo/src/lib.rs +++ b/examples/demo/src/lib.rs @@ -1,22 +1,25 @@ +use std::{f32::consts::PI, ops::Mul, time::Duration}; + use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + ecs::schedule::ScheduleLabel, log::info, prelude::{ - *, - bevy_main, default, shape, App, Assets, Color, Commands, Component, Event, EventReader, - EventWriter, GlobalTransform, IntoSystemConfigs, IntoSystemSetConfigs, Mesh, PbrBundle, - PostUpdate, Query, Res, ResMut, Resource, SpatialBundle, StandardMaterial, Startup, - Transform, Update, With, Without, + default, shape, App, Assets, Color, Commands, Component, Entity, Event, EventReader, + EventWriter, FixedUpdate, Gizmos, GlobalTransform, IntoSystemConfigs, IntoSystemSetConfigs, + Mesh, PbrBundle, PostUpdate, Quat, Query, Res, ResMut, Resource, Schedule, SpatialBundle, + StandardMaterial, Startup, Transform, Update, Vec3, Vec3Swizzles, With, Without, World, bevy_main, }, - time::{Time, Timer}, + time::{Fixed, Time, Timer}, transform::TransformSystem, }; -use bevy_openxr::{ +use bevy_oxr::{ input::XrInput, resources::{XrFrameState, XrInstance, XrSession}, xr_input::{ debug_gizmos::OpenXrDebugRenderer, - hand::{HandBone, HandInputDebugRenderer, HandResource, HandsResource, OpenXrHandInput}, + hand::{ HandInputDebugRenderer, HandResource, HandsResource, OpenXrHandInput}, + hands::HandBone, interactions::{ draw_interaction_gizmos, draw_socket_gizmos, interactions, socket_interactions, update_interactable_states, InteractionEvent, Touched, XRDirectInteractor, @@ -51,7 +54,7 @@ pub fn main() { .add_plugins(OpenXrDebugRenderer) //rapier goes here .add_plugins(RapierPhysicsPlugin::::default().with_default_system_setup(false)) - .add_plugins(RapierDebugRenderPlugin::default()) + // .add_plugins(RapierDebugRenderPlugin::default()) //lets setup the starting scene .add_systems(Startup, setup_scene) .add_systems(Startup, spawn_controllers_example) //you need to spawn controllers or it crashes TODO:: Fix this @@ -89,9 +92,24 @@ pub fn main() { .add_plugins(OpenXrHandInput) .add_plugins(HandInputDebugRenderer) .add_systems(Startup, spawn_physics_hands) - .add_systems(Update, update_physics_hands); + .add_systems( + FixedUpdate, + update_physics_hands.before(PhysicsSet::SyncBackend), + ); //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( PostUpdate, ( @@ -103,24 +121,41 @@ pub fn main() { .before(TransformSystem::TransformPropagate), ); //add rapier systems - app.add_systems( - PostUpdate, - ( - RapierPhysicsPlugin::::get_systems(PhysicsSet::SyncBackend) - .in_set(PhysicsSet::SyncBackend), - ( - RapierPhysicsPlugin::::get_systems(PhysicsSet::StepSimulation), - // despawn_one_box, - ) - .in_set(PhysicsSet::StepSimulation), - RapierPhysicsPlugin::::get_systems(PhysicsSet::Writeback) - .in_set(PhysicsSet::Writeback), - ), - ); - + physics_schedule.add_systems(( + RapierPhysicsPlugin::::get_systems(PhysicsSet::SyncBackend) + .in_set(PhysicsSet::SyncBackend), + RapierPhysicsPlugin::::get_systems(PhysicsSet::StepSimulation) + .in_set(PhysicsSet::StepSimulation), + RapierPhysicsPlugin::::get_systems(PhysicsSet::Writeback) + .in_set(PhysicsSet::Writeback), + )); + app.add_schedule(physics_schedule) // configure our fixed timestep schedule to run at the rate we want + .insert_resource(Time::::from_duration(Duration::from_secs_f32( + FIXED_TIMESTEP, + ))) + .add_systems(FixedUpdate, run_physics_schedule) + .add_systems(Startup, configure_physics); 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) { + rapier_config.timestep_mode = TimestepMode::Fixed { + dt: FIXED_TIMESTEP, + substeps: 1, + } +} + fn spawn_controllers_example(mut commands: Commands) { //left hand commands.spawn(( @@ -244,13 +279,21 @@ fn spawn_physics_hands(mut commands: Commands) { PhysicsHandBone::LittleDistal, PhysicsHandBone::LittleTip, ]; - //lets just do the Right ThumbMetacarpal for now - //i dont understand the groups yet - let self_group = Group::GROUP_1; - let interaction_group = Group::ALL; let radius = 0.010; + let left_hand_membership_group = Group::GROUP_1; + let right_hand_membership_group = Group::GROUP_2; + let floor_membership = Group::GROUP_3; + for hand in hands.iter() { + let hand_membership = match hand + { + Hand::Left => left_hand_membership_group, + Hand::Right => right_hand_membership_group, + }; + let mut hand_filter: Group = Group::ALL; + hand_filter.remove(hand_membership); + hand_filter.remove(floor_membership); for bone in bones.iter() { //spawn the thing commands.spawn(( @@ -268,8 +311,9 @@ fn spawn_physics_hands(mut commands: Commands) { }, radius, ), - RigidBody::KinematicPositionBased, - // CollisionGroups::new(self_group, interaction_group), + RigidBody::Dynamic, + Velocity::default(), + CollisionGroups::new(hand_membership, Group::from_bits(0b0001).unwrap()), // SolverGroups::new(self_group, interaction_group), bone.clone(), BoneInitState::False, @@ -279,6 +323,11 @@ fn spawn_physics_hands(mut commands: Commands) { } } +pub enum MatchingType { + PositionMatching, + VelocityMatching, +} + fn update_physics_hands( hands_res: Option>, mut bone_query: Query<( @@ -287,9 +336,13 @@ fn update_physics_hands( &PhysicsHandBone, &mut BoneInitState, &Hand, + &mut Velocity, )>, hand_query: Query<(&Transform, &HandBone, &Hand, Without)>, + time: Res