diff --git a/src/xr_input/debug_gizmos.rs b/src/xr_input/debug_gizmos.rs index e2e8227..84d2d8e 100644 --- a/src/xr_input/debug_gizmos.rs +++ b/src/xr_input/debug_gizmos.rs @@ -1,8 +1,8 @@ +use bevy::log::{debug, info}; use bevy::prelude::{ Color, Gizmos, GlobalTransform, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With, Without, }; -use bevy::log::info; use crate::{ input::XrInput, @@ -103,11 +103,22 @@ pub fn draw_gizmos( Err(_) => info!("too many tracking roots"), } //draw the hands - let left_transform = left_controller_query.get_single().unwrap().0; - let right_transform = right_controller_query.get_single().unwrap().0; - - draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, right_transform); - draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, left_transform); + //left + let left_transform = left_controller_query.get_single(); + match left_transform { + Ok(left_entity) => { + draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, left_entity.0); + } + Err(_) => debug!("no left controller entity for debug gizmos"), + } + //right + let right_transform = right_controller_query.get_single(); + match right_transform { + Ok(right_entity) => { + draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, right_entity.0); + } + Err(_) => debug!("no right controller entity for debug gizmos"), + } } fn draw_hand_gizmo( diff --git a/src/xr_input/hands/emulated.rs b/src/xr_input/hands/emulated.rs index 10282f4..36b8201 100644 --- a/src/xr_input/hands/emulated.rs +++ b/src/xr_input/hands/emulated.rs @@ -144,6 +144,9 @@ pub(crate) fn update_hand_skeleton_from_emulated( ), >, ) { + //get the transforms outside the loop + let left = left_controller_transform.get_single(); + let right = right_controller_transform.get_single(); let mut data: [[Transform; 26]; 2] = [[Transform::default(); 26]; 2]; for (subaction_path, hand) in [ ( @@ -189,21 +192,36 @@ pub(crate) fn update_hand_skeleton_from_emulated( .state(&session, subaction_path) .unwrap() .current_state; - data[match hand { - Hand::Left => 0, - Hand::Right => 1, - }] = update_hand_bones_emulated( - match hand { - Hand::Left => left_controller_transform.single(), - Hand::Right => right_controller_transform.single(), + match hand { + Hand::Left => match left { + Ok(hand_transform) => { + data[0] = update_hand_bones_emulated( + hand_transform, + hand, + thumb_curl, + index_curl, + middle_curl, + ring_curl, + little_curl, + ); + } + Err(_) => debug!("no left controller transform for hand bone emulation"), }, - hand, - thumb_curl, - index_curl, - middle_curl, - ring_curl, - little_curl, - ); + Hand::Right => match right { + Ok(hand_transform) => { + data[1] = update_hand_bones_emulated( + hand_transform, + hand, + thumb_curl, + index_curl, + middle_curl, + ring_curl, + little_curl, + ); + } + Err(_) => debug!("no right controller transform for hand bone emulation"), + }, + } } let trt = tracking_root_transform.single(); for (mut t, bone, hand, status, mut radius) in bones.iter_mut() { diff --git a/src/xr_input/trackers.rs b/src/xr_input/trackers.rs index bd61977..a57e8a8 100644 --- a/src/xr_input/trackers.rs +++ b/src/xr_input/trackers.rs @@ -1,4 +1,4 @@ -use bevy::log::info; +use bevy::log::{debug, info}; use bevy::prelude::{ Added, BuildChildren, Commands, Component, Entity, Query, Res, Transform, Vec3, With, Without, }; @@ -74,49 +74,67 @@ pub fn update_open_xr_controllers( let left_grip_space = controller.grip_space(Hand::Left); let left_aim_space = controller.aim_space(Hand::Left); let left_postion = left_grip_space.0.pose.position.to_vec3(); - let left_aim_pose = left_controller_query.get_single_mut().unwrap().1; + //TODO figure out how to not get the entity multiple times + let left_aim_pose = left_controller_query.get_single_mut(); + //set aim pose match left_aim_pose { - Some(mut pose) => { - *pose = AimPose(Transform { - translation: left_aim_space.0.pose.position.to_vec3(), - rotation: left_aim_space.0.pose.orientation.to_quat(), - scale: Vec3::splat(1.0), - }); - } - None => (), + Ok(left_entity) => match left_entity.1 { + Some(mut pose) => { + *pose = AimPose(Transform { + translation: left_aim_space.0.pose.position.to_vec3(), + rotation: left_aim_space.0.pose.orientation.to_quat(), + scale: Vec3::splat(1.0), + }); + } + None => (), + }, + Err(_) => debug!("no left controlelr entity found"), + } + //set translation + let left_translation = left_controller_query.get_single_mut(); + match left_translation { + Ok(mut left_entity) => left_entity.0.translation = left_postion, + Err(_) => (), + } + //set rotation + let left_rotataion = left_controller_query.get_single_mut(); + match left_rotataion { + Ok(mut left_entity) => { + left_entity.0.rotation = left_grip_space.0.pose.orientation.to_quat() + } + Err(_) => (), } - - left_controller_query - .get_single_mut() - .unwrap() - .0 - .translation = left_postion; - - left_controller_query.get_single_mut().unwrap().0.rotation = - left_grip_space.0.pose.orientation.to_quat(); //get right controller let right_grip_space = controller.grip_space(Hand::Right); let right_aim_space = controller.aim_space(Hand::Right); let right_postion = right_grip_space.0.pose.position.to_vec3(); - let right_aim_pose = right_controller_query.get_single_mut().unwrap().1; + let right_aim_pose = right_controller_query.get_single_mut(); match right_aim_pose { - Some(mut pose) => { - *pose = AimPose(Transform { - translation: right_aim_space.0.pose.position.to_vec3(), - rotation: right_aim_space.0.pose.orientation.to_quat(), - scale: Vec3::splat(1.0), - }); - } - None => (), + Ok(right_entity) => match right_entity.1 { + Some(mut pose) => { + *pose = AimPose(Transform { + translation: right_aim_space.0.pose.position.to_vec3(), + rotation: right_aim_space.0.pose.orientation.to_quat(), + scale: Vec3::splat(1.0), + }); + } + None => (), + }, + Err(_) => debug!("no right controlelr entity found"), + } + //set translation + let right_translation = right_controller_query.get_single_mut(); + match right_translation { + Ok(mut right_entity) => right_entity.0.translation = right_postion, + Err(_) => (), + } + //set rotation + let right_rotataion = right_controller_query.get_single_mut(); + match right_rotataion { + Ok(mut right_entity) => { + right_entity.0.rotation = right_grip_space.0.pose.orientation.to_quat() + } + Err(_) => (), } - - right_controller_query - .get_single_mut() - .unwrap() - .0 - .translation = right_postion; - - right_controller_query.get_single_mut().unwrap().0.rotation = - right_grip_space.0.pose.orientation.to_quat(); }