From 4307d2a66eda31eecc75c401e87d30fe8600552e Mon Sep 17 00:00:00 2001 From: Schmarni Date: Wed, 8 Nov 2023 03:14:50 +0100 Subject: [PATCH] Refactord main.rs into lib.rs as Preperation for android support --- examples/demo/Cargo.toml | 2 +- examples/demo/src/lib.rs | 693 ++++++++++++++++++++++++++++++++++++++ examples/demo/src/main.rs | 692 +------------------------------------ 3 files changed, 695 insertions(+), 692 deletions(-) create mode 100644 examples/demo/src/lib.rs diff --git a/examples/demo/Cargo.toml b/examples/demo/Cargo.toml index fd158a5..7201aec 100644 --- a/examples/demo/Cargo.toml +++ b/examples/demo/Cargo.toml @@ -6,6 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = "0.11.3" +bevy = "0.12" bevy_openxr = "0.0.1" bevy_rapier3d = { git = "https://github.com/alexichepura/bevy_rapier", version = "0.22.0" } diff --git a/examples/demo/src/lib.rs b/examples/demo/src/lib.rs new file mode 100644 index 0000000..0623ee1 --- /dev/null +++ b/examples/demo/src/lib.rs @@ -0,0 +1,693 @@ +use std::{f32::consts::PI, ops::Mul, time::Duration}; + +use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + ecs::schedule::ScheduleLabel, + log::info, + prelude::{ + 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, + }, + time::{Fixed, Time, Timer}, + transform::TransformSystem, +}; +use bevy_oxr::{ + input::XrInput, + resources::{XrFrameState, XrInstance, XrSession}, + xr_input::{ + debug_gizmos::OpenXrDebugRenderer, + hand::{HandBone, HandInputDebugRenderer, HandResource, HandsResource, OpenXrHandInput}, + interactions::{ + draw_interaction_gizmos, draw_socket_gizmos, interactions, socket_interactions, + update_interactable_states, InteractionEvent, Touched, XRDirectInteractor, + XRInteractable, XRInteractableState, XRInteractorState, XRSelection, + }, + oculus_touch::OculusController, + prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}, + trackers::{OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker}, + Hand, + }, + DefaultXrPlugins, +}; + +mod setup; +use crate::setup::setup_scene; +use bevy_rapier3d::prelude::*; + +pub fn main() { + color_eyre::install().unwrap(); + + info!("Running bevy_openxr demo"); + let mut app = App::new(); + + app + //lets get the usual diagnostic stuff added + .add_plugins(LogDiagnosticsPlugin::default()) + .add_plugins(FrameTimeDiagnosticsPlugin) + //lets get the xr defaults added + .add_plugins(DefaultXrPlugins) + //lets add the debug renderer for the controllers + .add_plugins(OpenXrDebugRenderer) + //rapier goes here + .add_plugins(RapierPhysicsPlugin::::default().with_default_system_setup(false)) + // .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 + //add locomotion + .add_systems(Update, proto_locomotion) + .insert_resource(PrototypeLocomotionConfig::default()) + //lets add the interaction systems + .add_event::() + .add_systems(Update, prototype_interaction_input) + .add_systems(Update, interactions.before(update_interactable_states)) + .add_systems(Update, update_interactable_states) + .add_systems( + Update, + socket_interactions.before(update_interactable_states), + ) + //add the grabbable system + .add_systems(Update, update_grabbables.after(update_interactable_states)) + //draw the interaction gizmos + .add_systems( + Update, + draw_interaction_gizmos.after(update_interactable_states), + ) + .add_systems(Update, draw_socket_gizmos.after(update_interactable_states)) + //add our cube spawning system + .add_event::() + .insert_resource(SpawnCubeTimer(Timer::from_seconds( + 0.25, + bevy::time::TimerMode::Once, + ))) + .add_systems(Update, request_cube_spawn) + .add_systems(Update, cube_spawner.after(request_cube_spawn)) + //test capsule + .add_systems(Startup, spawn_capsule) + //physics hands + .add_plugins(OpenXrHandInput) + .add_plugins(HandInputDebugRenderer) + .add_systems(Startup, spawn_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, + ( + PhysicsSet::SyncBackend, + PhysicsSet::StepSimulation, + PhysicsSet::Writeback, + ) + .chain() + .before(TransformSystem::TransformPropagate), + ); + //add rapier systems + 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(( + OpenXRLeftController, + OpenXRController, + OpenXRTracker, + SpatialBundle::default(), + XRDirectInteractor, + XRInteractorState::default(), + XRSelection::default(), + )); + //right hand + commands.spawn(( + OpenXRRightController, + OpenXRController, + OpenXRTracker, + SpatialBundle::default(), + XRDirectInteractor, + XRInteractorState::default(), + XRSelection::default(), + )); +} + +fn spawn_capsule( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.spawn(( + PbrBundle { + mesh: meshes.add(Mesh::from(shape::Capsule { + radius: 0.033, + depth: 0.115, + ..default() + })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(0.0, 2.0, 0.0), + ..default() + }, + // Collider::capsule_y(0.0575, 0.034), + Collider::capsule( + Vec3 { + x: 0.0, + y: -0.0575, + z: 0.0, + }, + Vec3 { + x: 0.0, + y: 0.0575, + z: 0.0, + }, + 0.034, + ), + RigidBody::Dynamic, + )); +} + +#[derive(Component, PartialEq, Debug, Clone, Copy)] +pub enum PhysicsHandBone { + Palm, + Wrist, + ThumbMetacarpal, + ThumbProximal, + ThumbDistal, + ThumbTip, + IndexMetacarpal, + IndexProximal, + IndexIntermediate, + IndexDistal, + IndexTip, + MiddleMetacarpal, + MiddleProximal, + MiddleIntermediate, + MiddleDistal, + MiddleTip, + RingMetacarpal, + RingProximal, + RingIntermediate, + RingDistal, + RingTip, + LittleMetacarpal, + LittleProximal, + LittleIntermediate, + LittleDistal, + LittleTip, +} +#[derive(Component, PartialEq)] +pub enum BoneInitState { + True, + False, +} + +fn spawn_physics_hands(mut commands: Commands) { + //here we go + let hands = [Hand::Left, Hand::Right]; + let bones = [ + PhysicsHandBone::Palm, + PhysicsHandBone::Wrist, + PhysicsHandBone::ThumbMetacarpal, + PhysicsHandBone::ThumbProximal, + PhysicsHandBone::ThumbDistal, + PhysicsHandBone::ThumbTip, + PhysicsHandBone::IndexMetacarpal, + PhysicsHandBone::IndexProximal, + PhysicsHandBone::IndexIntermediate, + PhysicsHandBone::IndexDistal, + PhysicsHandBone::IndexTip, + PhysicsHandBone::MiddleMetacarpal, + PhysicsHandBone::MiddleProximal, + PhysicsHandBone::MiddleIntermediate, + PhysicsHandBone::MiddleDistal, + PhysicsHandBone::MiddleTip, + PhysicsHandBone::RingMetacarpal, + PhysicsHandBone::RingProximal, + PhysicsHandBone::RingIntermediate, + PhysicsHandBone::RingDistal, + PhysicsHandBone::RingTip, + PhysicsHandBone::LittleMetacarpal, + PhysicsHandBone::LittleProximal, + PhysicsHandBone::LittleIntermediate, + PhysicsHandBone::LittleDistal, + PhysicsHandBone::LittleTip, + ]; + 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(( + SpatialBundle::default(), + Collider::capsule( + Vec3 { + x: 0.0, + y: -0.0575, + z: 0.0, + }, + Vec3 { + x: 0.0, + y: 0.0575, + z: 0.0, + }, + radius, + ), + RigidBody::Dynamic, + Velocity::default(), + CollisionGroups::new(hand_membership, Group::from_bits(0b0001).unwrap()), + // SolverGroups::new(self_group, interaction_group), + bone.clone(), + BoneInitState::False, + hand.clone(), + )); + } + } +} + +pub enum MatchingType { + PositionMatching, + VelocityMatching, +} + +fn update_physics_hands( + hands_res: Option>, + mut bone_query: Query<( + &mut Transform, + &mut Collider, + &PhysicsHandBone, + &mut BoneInitState, + &Hand, + &mut Velocity, + )>, + hand_query: Query<(&Transform, &HandBone, &Hand, Without)>, + time: Res