From 4b1536cb0b29ae6a176acdaf2c5a3141f285d1a3 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Tue, 26 Sep 2023 12:54:52 -0400 Subject: [PATCH 01/14] got a hand array --- examples/xr.rs | 105 ++++++++++++++++++++++++++++++++++++++++++- src/xr_input/hand.rs | 3 ++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/xr_input/hand.rs diff --git a/examples/xr.rs b/examples/xr.rs index 548fe15..80eb3b7 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,12 +1,14 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; +use bevy_openxr::xr_input::{Vec3Conv, QuatConv}; use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, }; use bevy_openxr::DefaultXrPlugins; +use openxr::{Posef, Quaternionf, Vector3f, HandJoint}; fn main() { color_eyre::install().unwrap(); @@ -20,6 +22,7 @@ fn main() { .add_systems(Startup, setup) .add_systems(Update, proto_locomotion) .add_systems(Startup, spawn_controllers_example) + .add_systems(Update, spawn_skeleton) .insert_resource(PrototypeLocomotionConfig::default()) .run(); } @@ -62,11 +65,111 @@ fn setup( }); // camera commands.spawn((Camera3dBundle { - transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + transform: Transform::from_xyz(0.25, 1.25, 0.0).looking_at(Vec3{x: -0.548, y: -0.161, z: -0.137}, Vec3::Y), ..default() },)); } +fn spawn_skeleton(mut commands: Commands, mut gizmos: Gizmos) { + let hand_pose: [Posef; 26] = [ + Posef { position: Vector3f {x: -0.548, y: -0.161, z: -0.137}, orientation: Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}}, //palm + Posef { position: Vector3f {x: -0.548, y: 0.161, z: -0.137}, orientation: Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}}, + Posef { position: Vector3f {x: -0.529, y: -0.198, z: -0.126}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156,w: -0.376}}, + Posef { position: Vector3f {x: -0.533, y: -0.175, z: -0.090}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126,w: -0.254}}, + Posef { position: Vector3f {x: -0.544, y: -0.158, z: -0.069}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027,w: -0.387}}, + Posef { position: Vector3f {x: -0.557, y: -0.150, z: -0.065}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, + Posef { position: Vector3f {x: -0.521, y: -0.182, z: -0.136}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317,w: -0.376}}, + Posef { position: Vector3f {x: -0.550, y: -0.135, z: -0.102}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317,w: -0.376}}, + Posef { position: Vector3f {x: -0.571, y: -0.112, z: -0.082}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256,w: -0.404}}, + Posef { position: Vector3f {x: -0.585, y: -0.102, z: -0.070}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165,w: -0.428}}, + Posef { position: Vector3f {x: -0.593, y: -0.098, z: -0.064}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110,w: -0.440}}, + Posef { position: Vector3f {x: -0.527, y: -0.178, z: -0.144}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370,w: -0.401}}, + Posef { position: Vector3f {x: -0.559, y: -0.132, z: -0.119}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370,w: -0.401}}, + Posef { position: Vector3f {x: -0.582, y: -0.101, z: -0.104}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371,w: -0.420}}, + Posef { position: Vector3f {x: -0.599, y: -0.089, z: -0.092}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245,w: -0.443}}, + Posef { position: Vector3f {x: -0.608, y: -0.084, z: -0.086}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180,w: -0.450}}, + Posef { position: Vector3f {x: -0.535, y: -0.178, z: -0.152}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408,w: -0.445}}, + Posef { position: Vector3f {x: -0.568, y: -0.136, z: -0.137}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408,w: -0.445}}, + Posef { position: Vector3f {x: -0.590, y: -0.106, z: -0.130}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432,w: -0.464}}, + Posef { position: Vector3f {x: -0.607, y: -0.092, z: -0.122}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332,w: -0.477}}, + Posef { position: Vector3f {x: -0.617, y: -0.086, z: -0.117}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260,w: -0.482}}, + Posef { position: Vector3f {x: -0.544, y: -0.183, z: -0.159}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481,w: -0.452}}, + Posef { position: Vector3f {x: -0.576, y: -0.143, z: -0.152}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481,w: -0.452}}, + Posef { position: Vector3f {x: -0.594, y: -0.119, z: -0.154}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534,w: -0.493}}, + Posef { position: Vector3f {x: -0.607, y: -0.108, z: -0.152}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444,w: -0.498}}, + Posef { position: Vector3f {x: -0.616, y: -0.102, z: -0.150}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378,w: -0.496}}, + ]; + //cursed wrist math + let wrist_dist = Vec3{ x: 0.01, y: -0.05, z: 0.0 }; + let huh = Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}.to_quat(); + let why: Vec3 = huh.mul_vec3(wrist_dist); + + let offset = Vec3 { x: 0.548, y: 1.0, z: 0.137 }; + let palm = hand_pose[HandJoint::PALM]; + gizmos.sphere(palm.position.to_vec3() + offset, palm.orientation.to_quat(), 0.01, Color::WHITE); + let wrist = hand_pose[HandJoint::WRIST]; + gizmos.sphere(palm.position.to_vec3() + offset + why, palm.orientation.to_quat(), 0.01, Color::GRAY); + let thumb_meta = hand_pose[HandJoint::THUMB_METACARPAL]; + gizmos.sphere(thumb_meta.position.to_vec3() + offset, thumb_meta.orientation.to_quat(), 0.01, Color::RED); + let thumb_prox = hand_pose[HandJoint::THUMB_PROXIMAL]; + gizmos.sphere(thumb_prox.position.to_vec3() + offset, thumb_prox.orientation.to_quat(), 0.008, Color::RED); + let thumb_dist = hand_pose[HandJoint::THUMB_DISTAL]; + gizmos.sphere(thumb_dist.position.to_vec3() + offset, thumb_dist.orientation.to_quat(), 0.006, Color::RED); + let thumb_tip = hand_pose[HandJoint::THUMB_TIP]; + gizmos.sphere(thumb_tip.position.to_vec3() + offset, thumb_tip.orientation.to_quat(), 0.004, Color::RED); + + let index_meta = hand_pose[HandJoint::INDEX_METACARPAL]; + gizmos.sphere(index_meta.position.to_vec3() + offset, index_meta.orientation.to_quat(), 0.01, Color::ORANGE); + let index_prox = hand_pose[HandJoint::INDEX_PROXIMAL]; + gizmos.sphere(index_prox.position.to_vec3() + offset, index_prox.orientation.to_quat(), 0.008, Color::ORANGE); + let index_inter = hand_pose[HandJoint::INDEX_INTERMEDIATE]; + gizmos.sphere(index_inter.position.to_vec3() + offset, index_inter.orientation.to_quat(), 0.006, Color::ORANGE); + let index_dist = hand_pose[HandJoint::INDEX_DISTAL]; + gizmos.sphere(index_dist.position.to_vec3() + offset, index_dist.orientation.to_quat(), 0.004, Color::ORANGE); + let index_tip = hand_pose[HandJoint::INDEX_TIP]; + gizmos.sphere(index_tip.position.to_vec3() + offset, index_tip.orientation.to_quat(), 0.002, Color::ORANGE); + + let middle_meta = hand_pose[HandJoint::MIDDLE_METACARPAL]; + gizmos.sphere(middle_meta.position.to_vec3() + offset, middle_meta.orientation.to_quat(), 0.01, Color::YELLOW); + let middle_prox = hand_pose[HandJoint::MIDDLE_PROXIMAL]; + gizmos.sphere(middle_prox.position.to_vec3() + offset, middle_prox.orientation.to_quat(), 0.008, Color::YELLOW); + let middle_inter = hand_pose[HandJoint::MIDDLE_INTERMEDIATE]; + gizmos.sphere(middle_inter.position.to_vec3() + offset, middle_inter.orientation.to_quat(), 0.006, Color::YELLOW); + let middle_dist = hand_pose[HandJoint::MIDDLE_DISTAL]; + gizmos.sphere(middle_dist.position.to_vec3() + offset, middle_dist.orientation.to_quat(), 0.004, Color::YELLOW); + let middle_tip = hand_pose[HandJoint::MIDDLE_TIP]; + gizmos.sphere(middle_tip.position.to_vec3() + offset, middle_tip.orientation.to_quat(), 0.002, Color::YELLOW); + + let ring_meta = hand_pose[HandJoint::RING_METACARPAL]; + gizmos.sphere(ring_meta.position.to_vec3() + offset, ring_meta.orientation.to_quat(), 0.01, Color::GREEN); + let ring_prox = hand_pose[HandJoint::RING_PROXIMAL]; + gizmos.sphere(ring_prox.position.to_vec3() + offset, ring_prox.orientation.to_quat(), 0.008, Color::GREEN); + let ring_inter = hand_pose[HandJoint::RING_INTERMEDIATE]; + gizmos.sphere(ring_inter.position.to_vec3() + offset, ring_inter.orientation.to_quat(), 0.006, Color::GREEN); + let ring_dist = hand_pose[HandJoint::RING_DISTAL]; + gizmos.sphere(ring_dist.position.to_vec3() + offset, ring_dist.orientation.to_quat(), 0.004, Color::GREEN); + let ring_tip = hand_pose[HandJoint::RING_TIP]; + gizmos.sphere(ring_tip.position.to_vec3() + offset, ring_tip.orientation.to_quat(), 0.002, Color::GREEN); + + let little_meta = hand_pose[HandJoint::LITTLE_METACARPAL]; + gizmos.sphere(little_meta.position.to_vec3() + offset, little_meta.orientation.to_quat(), 0.01, Color::BLUE); + let little_prox = hand_pose[HandJoint::LITTLE_PROXIMAL]; + gizmos.sphere(little_prox.position.to_vec3() + offset, little_prox.orientation.to_quat(), 0.008, Color::BLUE); + let little_inter = hand_pose[HandJoint::LITTLE_INTERMEDIATE]; + gizmos.sphere(little_inter.position.to_vec3() + offset, little_inter.orientation.to_quat(), 0.006, Color::BLUE); + let little_dist = hand_pose[HandJoint::LITTLE_DISTAL]; + gizmos.sphere(little_dist.position.to_vec3() + offset, little_dist.orientation.to_quat(), 0.004, Color::BLUE); + let little_tip = hand_pose[HandJoint::LITTLE_TIP]; + gizmos.sphere(little_tip.position.to_vec3() + offset, little_tip.orientation.to_quat(), 0.002, Color::BLUE); + + + + + + +} + + fn spawn_controllers_example(mut commands: Commands) { //left hand commands.spawn(( diff --git a/src/xr_input/hand.rs b/src/xr_input/hand.rs new file mode 100644 index 0000000..0fcd267 --- /dev/null +++ b/src/xr_input/hand.rs @@ -0,0 +1,3 @@ +pub struct Hand { + pub HandJointPoses: [Posef; 26] +} \ No newline at end of file From ed9508756078c7cffd40a428c358652c281d7815 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Tue, 3 Oct 2023 14:28:33 -0400 Subject: [PATCH 02/14] well this is insane but it tracks the hand now --- examples/xr.rs | 158 ++++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 66 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 80eb3b7..1f3df09 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,3 +1,5 @@ +use std::f32::consts::PI; + use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; @@ -16,13 +18,13 @@ fn main() { info!("Running `openxr-6dof` skill"); App::new() .add_plugins(DefaultXrPlugins) - .add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to + //.add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to .add_plugins(LogDiagnosticsPlugin::default()) .add_plugins(FrameTimeDiagnosticsPlugin) .add_systems(Startup, setup) .add_systems(Update, proto_locomotion) .add_systems(Startup, spawn_controllers_example) - .add_systems(Update, spawn_skeleton) + .add_systems(Update, draw_skeleton_hand) .insert_resource(PrototypeLocomotionConfig::default()) .run(); } @@ -70,103 +72,127 @@ fn setup( },)); } -fn spawn_skeleton(mut commands: Commands, mut gizmos: Gizmos) { - let hand_pose: [Posef; 26] = [ - Posef { position: Vector3f {x: -0.548, y: -0.161, z: -0.137}, orientation: Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}}, //palm - Posef { position: Vector3f {x: -0.548, y: 0.161, z: -0.137}, orientation: Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}}, - Posef { position: Vector3f {x: -0.529, y: -0.198, z: -0.126}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156,w: -0.376}}, - Posef { position: Vector3f {x: -0.533, y: -0.175, z: -0.090}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126,w: -0.254}}, - Posef { position: Vector3f {x: -0.544, y: -0.158, z: -0.069}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027,w: -0.387}}, - Posef { position: Vector3f {x: -0.557, y: -0.150, z: -0.065}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, - Posef { position: Vector3f {x: -0.521, y: -0.182, z: -0.136}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317,w: -0.376}}, - Posef { position: Vector3f {x: -0.550, y: -0.135, z: -0.102}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317,w: -0.376}}, - Posef { position: Vector3f {x: -0.571, y: -0.112, z: -0.082}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256,w: -0.404}}, - Posef { position: Vector3f {x: -0.585, y: -0.102, z: -0.070}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165,w: -0.428}}, - Posef { position: Vector3f {x: -0.593, y: -0.098, z: -0.064}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110,w: -0.440}}, - Posef { position: Vector3f {x: -0.527, y: -0.178, z: -0.144}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370,w: -0.401}}, - Posef { position: Vector3f {x: -0.559, y: -0.132, z: -0.119}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370,w: -0.401}}, - Posef { position: Vector3f {x: -0.582, y: -0.101, z: -0.104}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371,w: -0.420}}, - Posef { position: Vector3f {x: -0.599, y: -0.089, z: -0.092}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245,w: -0.443}}, - Posef { position: Vector3f {x: -0.608, y: -0.084, z: -0.086}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180,w: -0.450}}, - Posef { position: Vector3f {x: -0.535, y: -0.178, z: -0.152}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408,w: -0.445}}, - Posef { position: Vector3f {x: -0.568, y: -0.136, z: -0.137}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408,w: -0.445}}, - Posef { position: Vector3f {x: -0.590, y: -0.106, z: -0.130}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432,w: -0.464}}, - Posef { position: Vector3f {x: -0.607, y: -0.092, z: -0.122}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332,w: -0.477}}, - Posef { position: Vector3f {x: -0.617, y: -0.086, z: -0.117}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260,w: -0.482}}, - Posef { position: Vector3f {x: -0.544, y: -0.183, z: -0.159}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481,w: -0.452}}, - Posef { position: Vector3f {x: -0.576, y: -0.143, z: -0.152}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481,w: -0.452}}, - Posef { position: Vector3f {x: -0.594, y: -0.119, z: -0.154}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534,w: -0.493}}, - Posef { position: Vector3f {x: -0.607, y: -0.108, z: -0.152}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444,w: -0.498}}, - Posef { position: Vector3f {x: -0.616, y: -0.102, z: -0.150}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378,w: -0.496}}, - ]; - //cursed wrist math - let wrist_dist = Vec3{ x: 0.01, y: -0.05, z: 0.0 }; - let huh = Quaternionf {x: -0.267,y: 0.849, z: 0.204,w: 0.407}.to_quat(); - let why: Vec3 = huh.mul_vec3(wrist_dist); +fn draw_skeleton_hand(mut commands: Commands, + mut gizmos: Gizmos, + right_controller_query: Query<( + &GlobalTransform, + With, +)>, ) { + //draw debug for controller grip center to match palm to + let right_transform = right_controller_query.get_single().unwrap().0; + let right_translation = right_transform.compute_transform().translation; + let right_quat = right_transform.compute_transform().rotation; + gizmos.sphere(right_translation, Quat::IDENTITY, 0.01, Color::PINK); + //we need to flip this i dont know why + let flip = Quat::from_rotation_x(PI); + let flap = Quat::from_rotation_z(-45.0*(PI/180.0)); + let controller_forward = right_quat.mul_quat(flip).mul_vec3(Vec3::Y); + let controller_backward = right_quat.mul_quat(flip).mul_quat(flap); + gizmos.ray(right_translation, controller_forward, Color::PINK); - let offset = Vec3 { x: 0.548, y: 1.0, z: 0.137 }; + let hand_pose: [Posef; 26] = [ + Posef { position: Vector3f {x: -0.548, y: -0.161, z: -0.137}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, //palm + Posef { position: Vector3f {x: -0.548, y: 0.161, z: -0.137}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, + Posef { position: Vector3f {x: -0.529, y: -0.198, z: -0.126}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156, w: -0.376}}, + Posef { position: Vector3f {x: -0.533, y: -0.175, z: -0.090}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126, w: -0.254}}, + Posef { position: Vector3f {x: -0.544, y: -0.158, z: -0.069}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027, w: -0.387}}, + Posef { position: Vector3f {x: -0.557, y: -0.150, z: -0.065}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, + Posef { position: Vector3f {x: -0.521, y: -0.182, z: -0.136}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, + Posef { position: Vector3f {x: -0.550, y: -0.135, z: -0.102}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, + Posef { position: Vector3f {x: -0.571, y: -0.112, z: -0.082}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256, w: -0.404}}, + Posef { position: Vector3f {x: -0.585, y: -0.102, z: -0.070}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165, w: -0.428}}, + Posef { position: Vector3f {x: -0.593, y: -0.098, z: -0.064}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110, w: -0.440}}, + Posef { position: Vector3f {x: -0.527, y: -0.178, z: -0.144}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, + Posef { position: Vector3f {x: -0.559, y: -0.132, z: -0.119}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, + Posef { position: Vector3f {x: -0.582, y: -0.101, z: -0.104}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371, w: -0.420}}, + Posef { position: Vector3f {x: -0.599, y: -0.089, z: -0.092}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245, w: -0.443}}, + Posef { position: Vector3f {x: -0.608, y: -0.084, z: -0.086}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180, w: -0.450}}, + Posef { position: Vector3f {x: -0.535, y: -0.178, z: -0.152}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, + Posef { position: Vector3f {x: -0.568, y: -0.136, z: -0.137}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, + Posef { position: Vector3f {x: -0.590, y: -0.106, z: -0.130}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432, w: -0.464}}, + Posef { position: Vector3f {x: -0.607, y: -0.092, z: -0.122}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332, w: -0.477}}, + Posef { position: Vector3f {x: -0.617, y: -0.086, z: -0.117}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260, w: -0.482}}, + Posef { position: Vector3f {x: -0.544, y: -0.183, z: -0.159}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, + Posef { position: Vector3f {x: -0.576, y: -0.143, z: -0.152}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, + Posef { position: Vector3f {x: -0.594, y: -0.119, z: -0.154}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534, w: -0.493}}, + Posef { position: Vector3f {x: -0.607, y: -0.108, z: -0.152}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444, w: -0.498}}, + Posef { position: Vector3f {x: -0.616, y: -0.102, z: -0.150}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378, w: -0.496}}, + ]; + + //cursed wrist math + let wrist_dist = Vec3{ x: -0.01, y: -0.040, z: -0.015}; + + //cursed offset + let palm_negation = Vec3 { x: 0.548, y: 0.161, z: 0.137 }; + let offset = right_translation; + + //old stuff dont touch for now let palm = hand_pose[HandJoint::PALM]; - gizmos.sphere(palm.position.to_vec3() + offset, palm.orientation.to_quat(), 0.01, Color::WHITE); - let wrist = hand_pose[HandJoint::WRIST]; - gizmos.sphere(palm.position.to_vec3() + offset + why, palm.orientation.to_quat(), 0.01, Color::GRAY); + gizmos.sphere(palm.position.to_vec3() + offset + palm_negation, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); + + let rotated_wfp = palm.position.to_vec3() + right_quat.mul_quat(flip).mul_vec3(wrist_dist); + gizmos.sphere(offset + palm_negation + rotated_wfp, palm.orientation.to_quat(), 0.01, Color::GRAY); + + let thumb_meta = hand_pose[HandJoint::THUMB_METACARPAL]; - gizmos.sphere(thumb_meta.position.to_vec3() + offset, thumb_meta.orientation.to_quat(), 0.01, Color::RED); + draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, palm_negation, offset); + let thumb_prox = hand_pose[HandJoint::THUMB_PROXIMAL]; - gizmos.sphere(thumb_prox.position.to_vec3() + offset, thumb_prox.orientation.to_quat(), 0.008, Color::RED); + draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, palm_negation, offset); let thumb_dist = hand_pose[HandJoint::THUMB_DISTAL]; - gizmos.sphere(thumb_dist.position.to_vec3() + offset, thumb_dist.orientation.to_quat(), 0.006, Color::RED); + draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, palm_negation, offset); let thumb_tip = hand_pose[HandJoint::THUMB_TIP]; - gizmos.sphere(thumb_tip.position.to_vec3() + offset, thumb_tip.orientation.to_quat(), 0.004, Color::RED); + draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, palm_negation, offset); let index_meta = hand_pose[HandJoint::INDEX_METACARPAL]; - gizmos.sphere(index_meta.position.to_vec3() + offset, index_meta.orientation.to_quat(), 0.01, Color::ORANGE); + draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, palm_negation, offset); let index_prox = hand_pose[HandJoint::INDEX_PROXIMAL]; - gizmos.sphere(index_prox.position.to_vec3() + offset, index_prox.orientation.to_quat(), 0.008, Color::ORANGE); + draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, palm_negation, offset); let index_inter = hand_pose[HandJoint::INDEX_INTERMEDIATE]; - gizmos.sphere(index_inter.position.to_vec3() + offset, index_inter.orientation.to_quat(), 0.006, Color::ORANGE); + draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, palm_negation, offset); let index_dist = hand_pose[HandJoint::INDEX_DISTAL]; - gizmos.sphere(index_dist.position.to_vec3() + offset, index_dist.orientation.to_quat(), 0.004, Color::ORANGE); + draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, palm_negation, offset); let index_tip = hand_pose[HandJoint::INDEX_TIP]; - gizmos.sphere(index_tip.position.to_vec3() + offset, index_tip.orientation.to_quat(), 0.002, Color::ORANGE); + draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, palm_negation, offset); let middle_meta = hand_pose[HandJoint::MIDDLE_METACARPAL]; - gizmos.sphere(middle_meta.position.to_vec3() + offset, middle_meta.orientation.to_quat(), 0.01, Color::YELLOW); + draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, palm_negation, offset); let middle_prox = hand_pose[HandJoint::MIDDLE_PROXIMAL]; - gizmos.sphere(middle_prox.position.to_vec3() + offset, middle_prox.orientation.to_quat(), 0.008, Color::YELLOW); + draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, palm_negation, offset); let middle_inter = hand_pose[HandJoint::MIDDLE_INTERMEDIATE]; - gizmos.sphere(middle_inter.position.to_vec3() + offset, middle_inter.orientation.to_quat(), 0.006, Color::YELLOW); + draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, palm_negation, offset); let middle_dist = hand_pose[HandJoint::MIDDLE_DISTAL]; - gizmos.sphere(middle_dist.position.to_vec3() + offset, middle_dist.orientation.to_quat(), 0.004, Color::YELLOW); + draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, palm_negation, offset); let middle_tip = hand_pose[HandJoint::MIDDLE_TIP]; - gizmos.sphere(middle_tip.position.to_vec3() + offset, middle_tip.orientation.to_quat(), 0.002, Color::YELLOW); + draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, palm_negation, offset); let ring_meta = hand_pose[HandJoint::RING_METACARPAL]; - gizmos.sphere(ring_meta.position.to_vec3() + offset, ring_meta.orientation.to_quat(), 0.01, Color::GREEN); + draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, palm_negation, offset); let ring_prox = hand_pose[HandJoint::RING_PROXIMAL]; - gizmos.sphere(ring_prox.position.to_vec3() + offset, ring_prox.orientation.to_quat(), 0.008, Color::GREEN); + draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, palm_negation, offset); let ring_inter = hand_pose[HandJoint::RING_INTERMEDIATE]; - gizmos.sphere(ring_inter.position.to_vec3() + offset, ring_inter.orientation.to_quat(), 0.006, Color::GREEN); + draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, palm_negation, offset); let ring_dist = hand_pose[HandJoint::RING_DISTAL]; - gizmos.sphere(ring_dist.position.to_vec3() + offset, ring_dist.orientation.to_quat(), 0.004, Color::GREEN); + draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, palm_negation, offset); let ring_tip = hand_pose[HandJoint::RING_TIP]; - gizmos.sphere(ring_tip.position.to_vec3() + offset, ring_tip.orientation.to_quat(), 0.002, Color::GREEN); + draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, palm_negation, offset); let little_meta = hand_pose[HandJoint::LITTLE_METACARPAL]; - gizmos.sphere(little_meta.position.to_vec3() + offset, little_meta.orientation.to_quat(), 0.01, Color::BLUE); + draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, palm_negation, offset); let little_prox = hand_pose[HandJoint::LITTLE_PROXIMAL]; - gizmos.sphere(little_prox.position.to_vec3() + offset, little_prox.orientation.to_quat(), 0.008, Color::BLUE); + draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, palm_negation, offset); let little_inter = hand_pose[HandJoint::LITTLE_INTERMEDIATE]; - gizmos.sphere(little_inter.position.to_vec3() + offset, little_inter.orientation.to_quat(), 0.006, Color::BLUE); + draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, palm_negation, offset); let little_dist = hand_pose[HandJoint::LITTLE_DISTAL]; - gizmos.sphere(little_dist.position.to_vec3() + offset, little_dist.orientation.to_quat(), 0.004, Color::BLUE); + draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, palm_negation, offset); let little_tip = hand_pose[HandJoint::LITTLE_TIP]; - gizmos.sphere(little_tip.position.to_vec3() + offset, little_tip.orientation.to_quat(), 0.002, Color::BLUE); - - - + draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, palm_negation, offset); +} +fn draw_joint(gizmos: &mut Gizmos, joint_pos: Vec3, joint_rot: Quat, radius: f32, color: Color, controller_backwards: Quat, palm_negation: Vec3, offset: Vec3) { + gizmos.sphere(controller_backwards.mul_vec3(joint_pos + palm_negation) + offset, joint_rot, radius, color); } From c49f716a02c59508263bd3d43e5ebf021bf81b34 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Tue, 3 Oct 2023 15:03:34 -0400 Subject: [PATCH 03/14] normalized hand poses to palm --- examples/xr.rs | 157 +++++++++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 58 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 1f3df09..b1305da 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -82,117 +82,158 @@ fn draw_skeleton_hand(mut commands: Commands, let right_transform = right_controller_query.get_single().unwrap().0; let right_translation = right_transform.compute_transform().translation; let right_quat = right_transform.compute_transform().rotation; - gizmos.sphere(right_translation, Quat::IDENTITY, 0.01, Color::PINK); //we need to flip this i dont know why let flip = Quat::from_rotation_x(PI); let flap = Quat::from_rotation_z(-45.0*(PI/180.0)); - let controller_forward = right_quat.mul_quat(flip).mul_vec3(Vec3::Y); let controller_backward = right_quat.mul_quat(flip).mul_quat(flap); - gizmos.ray(right_translation, controller_forward, Color::PINK); let hand_pose: [Posef; 26] = [ - Posef { position: Vector3f {x: -0.548, y: -0.161, z: -0.137}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, //palm - Posef { position: Vector3f {x: -0.548, y: 0.161, z: -0.137}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, - Posef { position: Vector3f {x: -0.529, y: -0.198, z: -0.126}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156, w: -0.376}}, - Posef { position: Vector3f {x: -0.533, y: -0.175, z: -0.090}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126, w: -0.254}}, - Posef { position: Vector3f {x: -0.544, y: -0.158, z: -0.069}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027, w: -0.387}}, - Posef { position: Vector3f {x: -0.557, y: -0.150, z: -0.065}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, - Posef { position: Vector3f {x: -0.521, y: -0.182, z: -0.136}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, - Posef { position: Vector3f {x: -0.550, y: -0.135, z: -0.102}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, - Posef { position: Vector3f {x: -0.571, y: -0.112, z: -0.082}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256, w: -0.404}}, - Posef { position: Vector3f {x: -0.585, y: -0.102, z: -0.070}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165, w: -0.428}}, - Posef { position: Vector3f {x: -0.593, y: -0.098, z: -0.064}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110, w: -0.440}}, - Posef { position: Vector3f {x: -0.527, y: -0.178, z: -0.144}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, - Posef { position: Vector3f {x: -0.559, y: -0.132, z: -0.119}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, - Posef { position: Vector3f {x: -0.582, y: -0.101, z: -0.104}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371, w: -0.420}}, - Posef { position: Vector3f {x: -0.599, y: -0.089, z: -0.092}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245, w: -0.443}}, - Posef { position: Vector3f {x: -0.608, y: -0.084, z: -0.086}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180, w: -0.450}}, - Posef { position: Vector3f {x: -0.535, y: -0.178, z: -0.152}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, - Posef { position: Vector3f {x: -0.568, y: -0.136, z: -0.137}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, - Posef { position: Vector3f {x: -0.590, y: -0.106, z: -0.130}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432, w: -0.464}}, - Posef { position: Vector3f {x: -0.607, y: -0.092, z: -0.122}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332, w: -0.477}}, - Posef { position: Vector3f {x: -0.617, y: -0.086, z: -0.117}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260, w: -0.482}}, - Posef { position: Vector3f {x: -0.544, y: -0.183, z: -0.159}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, - Posef { position: Vector3f {x: -0.576, y: -0.143, z: -0.152}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, - Posef { position: Vector3f {x: -0.594, y: -0.119, z: -0.154}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534, w: -0.493}}, - Posef { position: Vector3f {x: -0.607, y: -0.108, z: -0.152}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444, w: -0.498}}, - Posef { position: Vector3f {x: -0.616, y: -0.102, z: -0.150}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378, w: -0.496}}, + Posef { position: Vector3f {x: 0.0, y: 0.0, z: 0.0}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, //palm + Posef { position: Vector3f {x: 0.0, y: 0.32, z: 0.0}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, + + Posef { position: Vector3f {x: 0.019, y: -0.037, z: 0.011}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156, w: -0.376}}, + Posef { position: Vector3f {x: 0.015, y: -0.014, z: 0.047}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126, w: -0.254}}, + Posef { position: Vector3f {x: 0.004, y: 0.003, z: 0.068}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027, w: -0.387}}, + Posef { position: Vector3f {x: -0.009, y: 0.011, z: 0.072}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, + + Posef { position: Vector3f {x: 0.027, y: -0.021, z: 0.001}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, + Posef { position: Vector3f {x: -0.002, y:0.026, z:0.034}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, + Posef { position: Vector3f {x: -0.023, y:0.049, z:0.055}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256, w: -0.404}}, + Posef { position: Vector3f {x: -0.037, y:0.059, z:0.067}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165, w: -0.428}}, + Posef { position: Vector3f {x: -0.045, y:0.063, z:0.073}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110, w: -0.440}}, + + Posef { position: Vector3f {x: 0.021, y: -0.017, z: -0.007}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, + Posef { position: Vector3f {x: -0.011, y: 0.029, z:0.018}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, + Posef { position: Vector3f {x: -0.034, y:0.06, z:0.033}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371, w: -0.420}}, + Posef { position: Vector3f {x: -0.051, y: 0.072, z: 0.045}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245, w: -0.443}}, + Posef { position: Vector3f {x: -0.06, y: 0.077, z:0.051}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180, w: -0.450}}, + + Posef { position: Vector3f {x: 0.013, y:-0.017, z:-0.015}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, + Posef { position: Vector3f {x: -0.02, y: 0.025, z: 0.0}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, + Posef { position: Vector3f {x: -0.042, y:0.055, z:0.007}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432, w: -0.464}}, + Posef { position: Vector3f {x: -0.06, y:0.069, z: 0.015}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332, w: -0.477}}, + Posef { position: Vector3f {x: -0.069, y:0.075, z:0.02}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260, w: -0.482}}, + + Posef { position: Vector3f {x: 0.004, y:-0.022, z:-0.022}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, + Posef { position: Vector3f {x: -0.028, y:0.018, z:-0.015}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, + Posef { position: Vector3f {x: -0.046, y:0.042, z:-0.017}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534, w: -0.493}}, + Posef { position: Vector3f {x: -0.059, y:0.053, z:-0.015}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444, w: -0.498}}, + Posef { position: Vector3f {x: -0.068, y:0.059, z:-0.013}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378, w: -0.496}}, ]; + //log_hand(hand_pose); + + //cursed wrist math let wrist_dist = Vec3{ x: -0.01, y: -0.040, z: -0.015}; //cursed offset - let palm_negation = Vec3 { x: 0.548, y: 0.161, z: 0.137 }; let offset = right_translation; //old stuff dont touch for now let palm = hand_pose[HandJoint::PALM]; - gizmos.sphere(palm.position.to_vec3() + offset + palm_negation, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); + gizmos.sphere(palm.position.to_vec3() + offset, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); let rotated_wfp = palm.position.to_vec3() + right_quat.mul_quat(flip).mul_vec3(wrist_dist); - gizmos.sphere(offset + palm_negation + rotated_wfp, palm.orientation.to_quat(), 0.01, Color::GRAY); + gizmos.sphere(offset + rotated_wfp, palm.orientation.to_quat(), 0.01, Color::GRAY); let thumb_meta = hand_pose[HandJoint::THUMB_METACARPAL]; - draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, offset); let thumb_prox = hand_pose[HandJoint::THUMB_PROXIMAL]; - draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, offset); let thumb_dist = hand_pose[HandJoint::THUMB_DISTAL]; - draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, offset); let thumb_tip = hand_pose[HandJoint::THUMB_TIP]; - draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, offset); let index_meta = hand_pose[HandJoint::INDEX_METACARPAL]; - draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, offset); let index_prox = hand_pose[HandJoint::INDEX_PROXIMAL]; - draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, offset); let index_inter = hand_pose[HandJoint::INDEX_INTERMEDIATE]; - draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, offset); let index_dist = hand_pose[HandJoint::INDEX_DISTAL]; - draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, offset); let index_tip = hand_pose[HandJoint::INDEX_TIP]; - draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, offset); let middle_meta = hand_pose[HandJoint::MIDDLE_METACARPAL]; - draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, offset); let middle_prox = hand_pose[HandJoint::MIDDLE_PROXIMAL]; - draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, offset); let middle_inter = hand_pose[HandJoint::MIDDLE_INTERMEDIATE]; - draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, offset); let middle_dist = hand_pose[HandJoint::MIDDLE_DISTAL]; - draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, offset); let middle_tip = hand_pose[HandJoint::MIDDLE_TIP]; - draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, offset); let ring_meta = hand_pose[HandJoint::RING_METACARPAL]; - draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, offset); let ring_prox = hand_pose[HandJoint::RING_PROXIMAL]; - draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, offset); let ring_inter = hand_pose[HandJoint::RING_INTERMEDIATE]; - draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, offset); let ring_dist = hand_pose[HandJoint::RING_DISTAL]; - draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, offset); let ring_tip = hand_pose[HandJoint::RING_TIP]; - draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, offset); let little_meta = hand_pose[HandJoint::LITTLE_METACARPAL]; - draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, offset); let little_prox = hand_pose[HandJoint::LITTLE_PROXIMAL]; - draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, offset); let little_inter = hand_pose[HandJoint::LITTLE_INTERMEDIATE]; - draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, offset); let little_dist = hand_pose[HandJoint::LITTLE_DISTAL]; - draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, offset); let little_tip = hand_pose[HandJoint::LITTLE_TIP]; - draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, palm_negation, offset); + draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, offset); + + } -fn draw_joint(gizmos: &mut Gizmos, joint_pos: Vec3, joint_rot: Quat, radius: f32, color: Color, controller_backwards: Quat, palm_negation: Vec3, offset: Vec3) { - gizmos.sphere(controller_backwards.mul_vec3(joint_pos + palm_negation) + offset, joint_rot, radius, color); +fn draw_joint(gizmos: &mut Gizmos, joint_pos: Vec3, joint_rot: Quat, radius: f32, color: Color, controller_backwards: Quat, offset: Vec3) { + gizmos.sphere(controller_backwards.mul_vec3(joint_pos) + offset, joint_rot, radius, color); +} + +fn log_hand(hand_pose: [Posef; 26]) { + let palm_vec = hand_pose[HandJoint::PALM].position.to_vec3(); + info!("palm: {}", hand_pose[HandJoint::PALM].position.to_vec3() - palm_vec); + info!("wrist: {}", hand_pose[HandJoint::WRIST].position.to_vec3() - palm_vec); + + info!("tm: {}", hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() - palm_vec); + info!("tp: {}", hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() - palm_vec); + info!("td: {}", hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() - palm_vec); + info!("tt: {}", hand_pose[HandJoint::THUMB_TIP].position.to_vec3() - palm_vec); + + info!("im: {}", hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() - palm_vec); + info!("ip: {}", hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() - palm_vec); + info!("ii: {}", hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() - palm_vec); + info!("id: {}", hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() - palm_vec); + info!("it: {}", hand_pose[HandJoint::INDEX_TIP].position.to_vec3() - palm_vec); + + info!("mm: {}", hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() - palm_vec); + info!("mp: {}", hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() - palm_vec); + info!("mi: {}", hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() - palm_vec); + info!("md: {}", hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() - palm_vec); + info!("mt: {}", hand_pose[HandJoint::MIDDLE_TIP].position.to_vec3() - palm_vec); + + info!("rm: {}", hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() - palm_vec); + info!("rp: {}", hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() - palm_vec); + info!("ri: {}", hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() - palm_vec); + info!("rd: {}", hand_pose[HandJoint::RING_DISTAL].position.to_vec3() - palm_vec); + info!("rt: {}", hand_pose[HandJoint::RING_TIP].position.to_vec3() - palm_vec); + + info!("lm: {}", hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() - palm_vec); + info!("lp: {}", hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() - palm_vec); + info!("li: {}", hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() - palm_vec); + info!("ld: {}", hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() - palm_vec); + info!("lt: {}", hand_pose[HandJoint::LITTLE_TIP].position.to_vec3() - palm_vec); } From 68cdf193904edcbfed3437fc851d45e2f9ed9ef9 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Tue, 3 Oct 2023 15:47:18 -0400 Subject: [PATCH 04/14] both hands work --- examples/xr.rs | 117 ++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 49 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index b1305da..3d8058b 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,9 +1,10 @@ use std::f32::consts::PI; +use std::ops::Mul; use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; -use bevy_openxr::xr_input::{Vec3Conv, QuatConv}; +use bevy_openxr::xr_input::{Vec3Conv, QuatConv, Hand}; use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ @@ -24,7 +25,7 @@ fn main() { .add_systems(Startup, setup) .add_systems(Update, proto_locomotion) .add_systems(Startup, spawn_controllers_example) - .add_systems(Update, draw_skeleton_hand) + .add_systems(Update, draw_skeleton_hands) .insert_resource(PrototypeLocomotionConfig::default()) .run(); } @@ -72,24 +73,39 @@ fn setup( },)); } -fn draw_skeleton_hand(mut commands: Commands, +fn draw_skeleton_hands(mut commands: Commands, mut gizmos: Gizmos, right_controller_query: Query<( &GlobalTransform, With, -)>, ) { +)>, +left_controller_query: Query<( + &GlobalTransform, + With, +)>,) { + + let left_hand_transform = left_controller_query.get_single().unwrap().0.compute_transform(); + draw_hand(&mut gizmos, left_hand_transform, Hand::Left); + let right_hand_transform = right_controller_query.get_single().unwrap().0.compute_transform(); + draw_hand(&mut gizmos, right_hand_transform, Hand::Right) + + + +} + +fn draw_hand(mut gizmos: &mut Gizmos, + controller_transform: Transform, +hand: Hand) { //draw debug for controller grip center to match palm to - let right_transform = right_controller_query.get_single().unwrap().0; - let right_translation = right_transform.compute_transform().translation; - let right_quat = right_transform.compute_transform().rotation; + let hand_translation = controller_transform.translation; + let hand_quat = controller_transform.rotation; //we need to flip this i dont know why let flip = Quat::from_rotation_x(PI); - let flap = Quat::from_rotation_z(-45.0*(PI/180.0)); - let controller_backward = right_quat.mul_quat(flip).mul_quat(flap); + let controller_backward = hand_quat.mul_quat(flip); - let hand_pose: [Posef; 26] = [ + let test: [Posef; 26] = [ Posef { position: Vector3f {x: 0.0, y: 0.0, z: 0.0}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, //palm - Posef { position: Vector3f {x: 0.0, y: 0.32, z: 0.0}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, + Posef { position: Vector3f { x: 0.02, y: -0.040, z: -0.015}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, Posef { position: Vector3f {x: 0.019, y: -0.037, z: 0.011}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156, w: -0.376}}, Posef { position: Vector3f {x: 0.015, y: -0.014, z: 0.047}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126, w: -0.254}}, @@ -121,80 +137,83 @@ fn draw_skeleton_hand(mut commands: Commands, Posef { position: Vector3f {x: -0.068, y:0.059, z:-0.013}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378, w: -0.496}}, ]; + let hand_pose = flip_hand_pose(test.clone(), hand); + //log_hand(hand_pose); - - //cursed wrist math - let wrist_dist = Vec3{ x: -0.01, y: -0.040, z: -0.015}; - - //cursed offset - let offset = right_translation; - - //old stuff dont touch for now let palm = hand_pose[HandJoint::PALM]; - gizmos.sphere(palm.position.to_vec3() + offset, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); + gizmos.sphere(palm.position.to_vec3() + hand_translation, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); - let rotated_wfp = palm.position.to_vec3() + right_quat.mul_quat(flip).mul_vec3(wrist_dist); - gizmos.sphere(offset + rotated_wfp, palm.orientation.to_quat(), 0.01, Color::GRAY); + let wrist = hand_pose[HandJoint::WRIST]; + draw_joint(&mut gizmos, wrist.position.to_vec3(), wrist.orientation.to_quat(), 0.01, Color::GRAY, controller_backward, hand_translation); let thumb_meta = hand_pose[HandJoint::THUMB_METACARPAL]; - draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, offset); + draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, hand_translation); let thumb_prox = hand_pose[HandJoint::THUMB_PROXIMAL]; - draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, offset); + draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, hand_translation); let thumb_dist = hand_pose[HandJoint::THUMB_DISTAL]; - draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, offset); + draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, hand_translation); let thumb_tip = hand_pose[HandJoint::THUMB_TIP]; - draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, offset); + draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, hand_translation); let index_meta = hand_pose[HandJoint::INDEX_METACARPAL]; - draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, offset); + draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, hand_translation); let index_prox = hand_pose[HandJoint::INDEX_PROXIMAL]; - draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, offset); + draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, hand_translation); let index_inter = hand_pose[HandJoint::INDEX_INTERMEDIATE]; - draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, offset); + draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, hand_translation); let index_dist = hand_pose[HandJoint::INDEX_DISTAL]; - draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, offset); + draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, hand_translation); let index_tip = hand_pose[HandJoint::INDEX_TIP]; - draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, offset); + draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, hand_translation); let middle_meta = hand_pose[HandJoint::MIDDLE_METACARPAL]; - draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, offset); + draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, hand_translation); let middle_prox = hand_pose[HandJoint::MIDDLE_PROXIMAL]; - draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, offset); + draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, hand_translation); let middle_inter = hand_pose[HandJoint::MIDDLE_INTERMEDIATE]; - draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, offset); + draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, hand_translation); let middle_dist = hand_pose[HandJoint::MIDDLE_DISTAL]; - draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, offset); + draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, hand_translation); let middle_tip = hand_pose[HandJoint::MIDDLE_TIP]; - draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, offset); + draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, hand_translation); let ring_meta = hand_pose[HandJoint::RING_METACARPAL]; - draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, offset); + draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, hand_translation); let ring_prox = hand_pose[HandJoint::RING_PROXIMAL]; - draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, offset); + draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, hand_translation); let ring_inter = hand_pose[HandJoint::RING_INTERMEDIATE]; - draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, offset); + draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, hand_translation); let ring_dist = hand_pose[HandJoint::RING_DISTAL]; - draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, offset); + draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, hand_translation); let ring_tip = hand_pose[HandJoint::RING_TIP]; - draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, offset); + draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, hand_translation); let little_meta = hand_pose[HandJoint::LITTLE_METACARPAL]; - draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, offset); + draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, hand_translation); let little_prox = hand_pose[HandJoint::LITTLE_PROXIMAL]; - draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, offset); + draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, hand_translation); let little_inter = hand_pose[HandJoint::LITTLE_INTERMEDIATE]; - draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, offset); + draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, hand_translation); let little_dist = hand_pose[HandJoint::LITTLE_DISTAL]; - draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, offset); + draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, hand_translation); let little_tip = hand_pose[HandJoint::LITTLE_TIP]; - draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, offset); - - - + draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, hand_translation); +} +fn flip_hand_pose(hand_pose: [Posef; 26], hand: Hand) -> [Posef; 26] { + let mut new_pose = hand_pose; + match hand { + Hand::Left => { + for pose in new_pose.iter_mut() { + pose.position.x = -pose.position.x; + } + }, + Hand::Right => (), + } + return new_pose; } fn draw_joint(gizmos: &mut Gizmos, joint_pos: Vec3, joint_rot: Quat, radius: f32, color: Color, controller_backwards: Quat, offset: Vec3) { From 5818e57d4151fb7a169f8bc929d9ae74865b6d15 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Sat, 7 Oct 2023 17:05:47 -0400 Subject: [PATCH 05/14] left hand works too --- examples/xr.rs | 844 ++++++++++++++++++++++++++++++------- src/xr_input/hand_poses.rs | 520 +++++++++++++++++++++++ src/xr_input/mod.rs | 1 + 3 files changed, 1224 insertions(+), 141 deletions(-) create mode 100644 src/xr_input/hand_poses.rs diff --git a/examples/xr.rs b/examples/xr.rs index 3d8058b..d34c4b7 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -2,16 +2,18 @@ use std::f32::consts::PI; use std::ops::Mul; use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; -use bevy::prelude::*; +use bevy::math::vec3; use bevy::transform::components::Transform; -use bevy_openxr::xr_input::{Vec3Conv, QuatConv, Hand}; +use bevy::{gizmos, prelude::*}; use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; +use bevy_openxr::xr_input::hand_poses::*; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, }; +use bevy_openxr::xr_input::{Hand, QuatConv, Vec3Conv}; use bevy_openxr::DefaultXrPlugins; -use openxr::{Posef, Quaternionf, Vector3f, HandJoint}; +use openxr::{HandJoint, Posef, Quaternionf, Vector3f}; fn main() { color_eyre::install().unwrap(); @@ -68,34 +70,306 @@ fn setup( }); // camera commands.spawn((Camera3dBundle { - transform: Transform::from_xyz(0.25, 1.25, 0.0).looking_at(Vec3{x: -0.548, y: -0.161, z: -0.137}, Vec3::Y), + transform: Transform::from_xyz(0.25, 1.25, 0.0).looking_at( + Vec3 { + x: -0.548, + y: -0.161, + z: -0.137, + }, + Vec3::Y, + ), ..default() },)); } -fn draw_skeleton_hands(mut commands: Commands, +fn draw_skeleton_hands( + mut commands: Commands, mut gizmos: Gizmos, - right_controller_query: Query<( - &GlobalTransform, - With, -)>, -left_controller_query: Query<( - &GlobalTransform, - With, -)>,) { - - let left_hand_transform = left_controller_query.get_single().unwrap().0.compute_transform(); - draw_hand(&mut gizmos, left_hand_transform, Hand::Left); - let right_hand_transform = right_controller_query.get_single().unwrap().0.compute_transform(); - draw_hand(&mut gizmos, right_hand_transform, Hand::Right) - - - + right_controller_query: Query<(&GlobalTransform, With)>, + left_controller_query: Query<(&GlobalTransform, With)>, +) { + let left_hand_transform = left_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + draw_hand_bones(&mut gizmos, left_hand_transform, Hand::Left); + let right_hand_transform = right_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + // draw_hand(&mut gizmos, right_hand_transform, Hand::Right); + draw_hand_bones(&mut gizmos, right_hand_transform, Hand::Right); } -fn draw_hand(mut gizmos: &mut Gizmos, - controller_transform: Transform, -hand: Hand) { +fn pose_array_to_transform_array(hand_pose: [Posef; 26]) -> [Transform; 26] { + let mut result_array: [Transform; 26] = [Transform::default(); 26]; + for (place, data) in result_array.iter_mut().zip(hand_pose.iter()) { + *place = Transform { + translation: data.position.to_vec3(), + rotation: data.orientation.to_quat(), + scale: Vec3::splat(1.0), + } + } + return result_array; +} + +fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand) { + let left_hand_rot = Quat::from_rotation_y(180.0 * PI / 180.0); + let hand_translation: Vec3 = match hand { + Hand::Left => controller_transform.translation, + Hand::Right => controller_transform.translation, + }; + + let controller_quat: Quat = match hand { + Hand::Left => controller_transform.rotation.mul_quat(left_hand_rot), + Hand::Right => controller_transform.rotation, + }; + + let splay_direction = match hand { + Hand::Left => -1.0, + Hand::Right => 1.0, + }; + //get paml quat + let y = Quat::from_rotation_y(-90.0 * PI / 180.0); + let x = Quat::from_rotation_x(-90.0 * PI / 180.0); + let palm_quat = controller_quat.mul_quat(y).mul_quat(x); + //draw debug rays + gizmos.ray( + hand_translation, + palm_quat.mul_vec3(Vec3::Z * 0.2), + Color::BLUE, + ); + gizmos.ray( + hand_translation, + palm_quat.mul_vec3(Vec3::Y * 0.2), + Color::GREEN, + ); + gizmos.ray( + hand_translation, + palm_quat.mul_vec3(Vec3::X * 0.2), + Color::RED, + ); + //get simulated bones + let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); + //draw controller-palm bone(should be zero length) + let palm = hand_transform_array[HandJoint::PALM]; + gizmos.ray(hand_translation, palm.translation, Color::WHITE); + //draw palm-wrist + let wrist = hand_transform_array[HandJoint::WRIST]; + gizmos.ray( + hand_translation + palm.translation, + palm_quat.mul_vec3(wrist.translation), + Color::GRAY, + ); + + //thumb + //better finger drawing? + let thumb_joints = [ + HandJoint::THUMB_METACARPAL, + HandJoint::THUMB_PROXIMAL, + HandJoint::THUMB_DISTAL, + HandJoint::THUMB_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let color = Color::RED; + let splay = Quat::from_rotation_y(splay_direction * 30.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let tp_lrot = Quat::from_rotation_y(splay_direction * 5.0 * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + gizmos.ray(tp_start, tp_vector, color); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + gizmos.ray(tm_start, tm_vector, color); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + } + } + } + + //better finger drawing? + let thumb_joints = [ + HandJoint::INDEX_METACARPAL, + HandJoint::INDEX_PROXIMAL, + HandJoint::INDEX_INTERMEDIATE, + HandJoint::INDEX_DISTAL, + HandJoint::INDEX_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let color = Color::ORANGE; + let splay = Quat::from_rotation_y(splay_direction * 10.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + gizmos.ray(tp_start, tp_vector, color); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + gizmos.ray(tm_start, tm_vector, color); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + } + } + } + + //better finger drawing? + let thumb_joints = [ + HandJoint::MIDDLE_METACARPAL, + HandJoint::MIDDLE_PROXIMAL, + HandJoint::MIDDLE_INTERMEDIATE, + HandJoint::MIDDLE_DISTAL, + HandJoint::MIDDLE_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let color = Color::YELLOW; + let splay = Quat::from_rotation_y(splay_direction * 0.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + gizmos.ray(tp_start, tp_vector, color); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + gizmos.ray(tm_start, tm_vector, color); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + } + } + } + //better finger drawing? + let thumb_joints = [ + HandJoint::RING_METACARPAL, + HandJoint::RING_PROXIMAL, + HandJoint::RING_INTERMEDIATE, + HandJoint::RING_DISTAL, + HandJoint::RING_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let color = Color::GREEN; + let splay = Quat::from_rotation_y(splay_direction * -10.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + gizmos.ray(tp_start, tp_vector, color); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + gizmos.ray(tm_start, tm_vector, color); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + } + } + } + + //better finger drawing? + let thumb_joints = [ + HandJoint::LITTLE_METACARPAL, + HandJoint::LITTLE_PROXIMAL, + HandJoint::LITTLE_INTERMEDIATE, + HandJoint::LITTLE_DISTAL, + HandJoint::LITTLE_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let color = Color::BLUE; + let splay = Quat::from_rotation_y(splay_direction * -20.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + gizmos.ray(tp_start, tp_vector, color); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + gizmos.ray(tm_start, tm_vector, color); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + } + } + } +} + +fn draw_hand(mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand) { //draw debug for controller grip center to match palm to let hand_translation = controller_transform.translation; let hand_quat = controller_transform.rotation; @@ -103,104 +377,280 @@ hand: Hand) { let flip = Quat::from_rotation_x(PI); let controller_backward = hand_quat.mul_quat(flip); - let test: [Posef; 26] = [ - Posef { position: Vector3f {x: 0.0, y: 0.0, z: 0.0}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, //palm - Posef { position: Vector3f { x: 0.02, y: -0.040, z: -0.015}, orientation: Quaternionf {x: -0.267, y: 0.849, z: 0.204, w: 0.407}}, + let test_hand_pose = get_test_hand_pose_array(); - Posef { position: Vector3f {x: 0.019, y: -0.037, z: 0.011}, orientation: Quaternionf {x: -0.744, y: -0.530, z: 0.156, w: -0.376}}, - Posef { position: Vector3f {x: 0.015, y: -0.014, z: 0.047}, orientation: Quaternionf {x: -0.786, y: -0.550, z: 0.126, w: -0.254}}, - Posef { position: Vector3f {x: 0.004, y: 0.003, z: 0.068}, orientation: Quaternionf {x: -0.729, y: -0.564, z: 0.027, w: -0.387}}, - Posef { position: Vector3f {x: -0.009, y: 0.011, z: 0.072}, orientation: Quaternionf {x: -0.585, y: -0.548, z: -0.140,w: -0.582}}, + let hand_pose = flip_hand_pose(test_hand_pose.clone(), hand); - Posef { position: Vector3f {x: 0.027, y: -0.021, z: 0.001}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, - Posef { position: Vector3f {x: -0.002, y:0.026, z:0.034}, orientation: Quaternionf {x: -0.277, y: -0.826, z: 0.317, w: -0.376}}, - Posef { position: Vector3f {x: -0.023, y:0.049, z:0.055}, orientation: Quaternionf {x: -0.244, y: -0.843, z: 0.256, w: -0.404}}, - Posef { position: Vector3f {x: -0.037, y:0.059, z:0.067}, orientation: Quaternionf {x: -0.200, y: -0.866, z: 0.165, w: -0.428}}, - Posef { position: Vector3f {x: -0.045, y:0.063, z:0.073}, orientation: Quaternionf {x: -0.172, y: -0.874, z: 0.110, w: -0.440}}, + // let hand_transform_array: [Transform; 26] = pose_array_to_transform_array(hand_pose); + let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); - Posef { position: Vector3f {x: 0.021, y: -0.017, z: -0.007}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, - Posef { position: Vector3f {x: -0.011, y: 0.029, z:0.018}, orientation: Quaternionf {x: -0.185, y: -0.817, z: 0.370, w: -0.401}}, - Posef { position: Vector3f {x: -0.034, y:0.06, z:0.033}, orientation: Quaternionf {x: -0.175, y: -0.809, z: 0.371, w: -0.420}}, - Posef { position: Vector3f {x: -0.051, y: 0.072, z: 0.045}, orientation: Quaternionf {x: -0.109, y: -0.856, z: 0.245, w: -0.443}}, - Posef { position: Vector3f {x: -0.06, y: 0.077, z:0.051}, orientation: Quaternionf {x: -0.075, y: -0.871, z: 0.180, w: -0.450}}, + let palm = hand_transform_array[HandJoint::PALM]; + gizmos.sphere( + palm.translation + hand_translation, + palm.rotation.mul_quat(controller_backward), + 0.01, + Color::WHITE, + ); - Posef { position: Vector3f {x: 0.013, y:-0.017, z:-0.015}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, - Posef { position: Vector3f {x: -0.02, y: 0.025, z: 0.0}, orientation: Quaternionf {x: -0.132, y: -0.786, z: 0.408, w: -0.445}}, - Posef { position: Vector3f {x: -0.042, y:0.055, z:0.007}, orientation: Quaternionf {x: -0.131, y: -0.762, z: 0.432, w: -0.464}}, - Posef { position: Vector3f {x: -0.06, y:0.069, z: 0.015}, orientation: Quaternionf {x: -0.071, y: -0.810, z: 0.332, w: -0.477}}, - Posef { position: Vector3f {x: -0.069, y:0.075, z:0.02}, orientation: Quaternionf {x: -0.029, y: -0.836, z: 0.260, w: -0.482}}, + let wrist = hand_transform_array[HandJoint::WRIST]; + draw_joint( + &mut gizmos, + wrist.translation, + wrist.rotation, + 0.01, + Color::GRAY, + controller_backward, + hand_translation, + ); - Posef { position: Vector3f {x: 0.004, y:-0.022, z:-0.022}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, - Posef { position: Vector3f {x: -0.028, y:0.018, z:-0.015}, orientation: Quaternionf {x: -0.060, y: -0.749, z: 0.481, w: -0.452}}, - Posef { position: Vector3f {x: -0.046, y:0.042, z:-0.017}, orientation: Quaternionf {x: -0.061, y: -0.684, z: 0.534, w: -0.493}}, - Posef { position: Vector3f {x: -0.059, y:0.053, z:-0.015}, orientation: Quaternionf {x: 0.002, y: -0.745, z: 0.444, w: -0.498}}, - Posef { position: Vector3f {x: -0.068, y:0.059, z:-0.013}, orientation: Quaternionf {x: 0.045, y: -0.780, z: 0.378, w: -0.496}}, - ]; + let thumb_meta = hand_transform_array[HandJoint::THUMB_METACARPAL]; + draw_joint( + &mut gizmos, + thumb_meta.translation, + thumb_meta.rotation, + 0.01, + Color::RED, + controller_backward, + hand_translation, + ); - let hand_pose = flip_hand_pose(test.clone(), hand); + let thumb_prox = hand_transform_array[HandJoint::THUMB_PROXIMAL]; + draw_joint( + &mut gizmos, + thumb_prox.translation, + thumb_prox.rotation, + 0.008, + Color::RED, + controller_backward, + hand_translation, + ); + let thumb_dist = hand_transform_array[HandJoint::THUMB_DISTAL]; + draw_joint( + &mut gizmos, + thumb_dist.translation, + thumb_dist.rotation, + 0.006, + Color::RED, + controller_backward, + hand_translation, + ); + let thumb_tip = hand_transform_array[HandJoint::THUMB_TIP]; + draw_joint( + &mut gizmos, + thumb_tip.translation, + thumb_tip.rotation, + 0.004, + Color::RED, + controller_backward, + hand_translation, + ); - //log_hand(hand_pose); + let index_meta = hand_transform_array[HandJoint::INDEX_METACARPAL]; + draw_joint( + &mut gizmos, + index_meta.translation, + index_meta.rotation, + 0.01, + Color::ORANGE, + controller_backward, + hand_translation, + ); + let index_prox = hand_transform_array[HandJoint::INDEX_PROXIMAL]; + draw_joint( + &mut gizmos, + index_prox.translation, + index_prox.rotation, + 0.008, + Color::ORANGE, + controller_backward, + hand_translation, + ); - let palm = hand_pose[HandJoint::PALM]; - gizmos.sphere(palm.position.to_vec3() + hand_translation, palm.orientation.to_quat().mul_quat(controller_backward), 0.01, Color::WHITE); + let index_inter = hand_transform_array[HandJoint::INDEX_INTERMEDIATE]; + draw_joint( + &mut gizmos, + index_inter.translation, + index_inter.rotation, + 0.006, + Color::ORANGE, + controller_backward, + hand_translation, + ); - let wrist = hand_pose[HandJoint::WRIST]; - draw_joint(&mut gizmos, wrist.position.to_vec3(), wrist.orientation.to_quat(), 0.01, Color::GRAY, controller_backward, hand_translation); + let index_dist = hand_transform_array[HandJoint::INDEX_DISTAL]; + draw_joint( + &mut gizmos, + index_dist.translation, + index_dist.rotation, + 0.004, + Color::ORANGE, + controller_backward, + hand_translation, + ); + let index_tip = hand_transform_array[HandJoint::INDEX_TIP]; + draw_joint( + &mut gizmos, + index_tip.translation, + index_tip.rotation, + 0.002, + Color::ORANGE, + controller_backward, + hand_translation, + ); - let thumb_meta = hand_pose[HandJoint::THUMB_METACARPAL]; - draw_joint(&mut gizmos, thumb_meta.position.to_vec3(), thumb_meta.orientation.to_quat(), 0.01, Color::RED, controller_backward, hand_translation); + let middle_meta = hand_transform_array[HandJoint::MIDDLE_METACARPAL]; + draw_joint( + &mut gizmos, + middle_meta.translation, + middle_meta.rotation, + 0.01, + Color::YELLOW, + controller_backward, + hand_translation, + ); + let middle_prox = hand_transform_array[HandJoint::MIDDLE_PROXIMAL]; + draw_joint( + &mut gizmos, + middle_prox.translation, + middle_prox.rotation, + 0.008, + Color::YELLOW, + controller_backward, + hand_translation, + ); + let middle_inter = hand_transform_array[HandJoint::MIDDLE_INTERMEDIATE]; + draw_joint( + &mut gizmos, + middle_inter.translation, + middle_inter.rotation, + 0.006, + Color::YELLOW, + controller_backward, + hand_translation, + ); + let middle_dist = hand_transform_array[HandJoint::MIDDLE_DISTAL]; + draw_joint( + &mut gizmos, + middle_dist.translation, + middle_dist.rotation, + 0.004, + Color::YELLOW, + controller_backward, + hand_translation, + ); + let middle_tip = hand_transform_array[HandJoint::MIDDLE_TIP]; + draw_joint( + &mut gizmos, + middle_tip.translation, + middle_tip.rotation, + 0.002, + Color::YELLOW, + controller_backward, + hand_translation, + ); - let thumb_prox = hand_pose[HandJoint::THUMB_PROXIMAL]; - draw_joint(&mut gizmos, thumb_prox.position.to_vec3(), thumb_prox.orientation.to_quat(), 0.008, Color::RED, controller_backward, hand_translation); - let thumb_dist = hand_pose[HandJoint::THUMB_DISTAL]; - draw_joint(&mut gizmos, thumb_dist.position.to_vec3(), thumb_dist.orientation.to_quat(), 0.006, Color::RED, controller_backward, hand_translation); - let thumb_tip = hand_pose[HandJoint::THUMB_TIP]; - draw_joint(&mut gizmos, thumb_tip.position.to_vec3(), thumb_tip.orientation.to_quat(), 0.004, Color::RED, controller_backward, hand_translation); + let ring_meta = hand_transform_array[HandJoint::RING_METACARPAL]; + draw_joint( + &mut gizmos, + ring_meta.translation, + ring_meta.rotation, + 0.01, + Color::GREEN, + controller_backward, + hand_translation, + ); + let ring_prox = hand_transform_array[HandJoint::RING_PROXIMAL]; + draw_joint( + &mut gizmos, + ring_prox.translation, + ring_prox.rotation, + 0.008, + Color::GREEN, + controller_backward, + hand_translation, + ); + let ring_inter = hand_transform_array[HandJoint::RING_INTERMEDIATE]; + draw_joint( + &mut gizmos, + ring_inter.translation, + ring_inter.rotation, + 0.006, + Color::GREEN, + controller_backward, + hand_translation, + ); + let ring_dist = hand_transform_array[HandJoint::RING_DISTAL]; + draw_joint( + &mut gizmos, + ring_dist.translation, + ring_dist.rotation, + 0.004, + Color::GREEN, + controller_backward, + hand_translation, + ); + let ring_tip = hand_transform_array[HandJoint::RING_TIP]; + draw_joint( + &mut gizmos, + ring_tip.translation, + ring_tip.rotation, + 0.002, + Color::GREEN, + controller_backward, + hand_translation, + ); - let index_meta = hand_pose[HandJoint::INDEX_METACARPAL]; - draw_joint(&mut gizmos, index_meta.position.to_vec3(), index_meta.orientation.to_quat(), 0.01, Color::ORANGE, controller_backward, hand_translation); - let index_prox = hand_pose[HandJoint::INDEX_PROXIMAL]; - draw_joint(&mut gizmos, index_prox.position.to_vec3(), index_prox.orientation.to_quat(), 0.008, Color::ORANGE, controller_backward, hand_translation); - let index_inter = hand_pose[HandJoint::INDEX_INTERMEDIATE]; - draw_joint(&mut gizmos, index_inter.position.to_vec3(), index_inter.orientation.to_quat(), 0.006, Color::ORANGE, controller_backward, hand_translation); - let index_dist = hand_pose[HandJoint::INDEX_DISTAL]; - draw_joint(&mut gizmos, index_dist.position.to_vec3(), index_dist.orientation.to_quat(), 0.004, Color::ORANGE, controller_backward, hand_translation); - let index_tip = hand_pose[HandJoint::INDEX_TIP]; - draw_joint(&mut gizmos, index_tip.position.to_vec3(), index_tip.orientation.to_quat(), 0.002, Color::ORANGE, controller_backward, hand_translation); - - let middle_meta = hand_pose[HandJoint::MIDDLE_METACARPAL]; - draw_joint(&mut gizmos, middle_meta.position.to_vec3(), middle_meta.orientation.to_quat(), 0.01, Color::YELLOW, controller_backward, hand_translation); - let middle_prox = hand_pose[HandJoint::MIDDLE_PROXIMAL]; - draw_joint(&mut gizmos, middle_prox.position.to_vec3(), middle_prox.orientation.to_quat(), 0.008, Color::YELLOW, controller_backward, hand_translation); - let middle_inter = hand_pose[HandJoint::MIDDLE_INTERMEDIATE]; - draw_joint(&mut gizmos, middle_inter.position.to_vec3(), middle_inter.orientation.to_quat(), 0.006, Color::YELLOW, controller_backward, hand_translation); - let middle_dist = hand_pose[HandJoint::MIDDLE_DISTAL]; - draw_joint(&mut gizmos, middle_dist.position.to_vec3(), middle_dist.orientation.to_quat(), 0.004, Color::YELLOW, controller_backward, hand_translation); - let middle_tip = hand_pose[HandJoint::MIDDLE_TIP]; - draw_joint(&mut gizmos, middle_tip.position.to_vec3(), middle_tip.orientation.to_quat(), 0.002, Color::YELLOW, controller_backward, hand_translation); - - let ring_meta = hand_pose[HandJoint::RING_METACARPAL]; - draw_joint(&mut gizmos, ring_meta.position.to_vec3(), ring_meta.orientation.to_quat(), 0.01, Color::GREEN, controller_backward, hand_translation); - let ring_prox = hand_pose[HandJoint::RING_PROXIMAL]; - draw_joint(&mut gizmos, ring_prox.position.to_vec3(), ring_prox.orientation.to_quat(), 0.008, Color::GREEN, controller_backward, hand_translation); - let ring_inter = hand_pose[HandJoint::RING_INTERMEDIATE]; - draw_joint(&mut gizmos, ring_inter.position.to_vec3(), ring_inter.orientation.to_quat(), 0.006, Color::GREEN, controller_backward, hand_translation); - let ring_dist = hand_pose[HandJoint::RING_DISTAL]; - draw_joint(&mut gizmos, ring_dist.position.to_vec3(), ring_dist.orientation.to_quat(), 0.004, Color::GREEN, controller_backward, hand_translation); - let ring_tip = hand_pose[HandJoint::RING_TIP]; - draw_joint(&mut gizmos, ring_tip.position.to_vec3(), ring_tip.orientation.to_quat(), 0.002, Color::GREEN, controller_backward, hand_translation); - - let little_meta = hand_pose[HandJoint::LITTLE_METACARPAL]; - draw_joint(&mut gizmos, little_meta.position.to_vec3(), little_meta.orientation.to_quat(), 0.01, Color::BLUE, controller_backward, hand_translation); - let little_prox = hand_pose[HandJoint::LITTLE_PROXIMAL]; - draw_joint(&mut gizmos, little_prox.position.to_vec3(), little_prox.orientation.to_quat(), 0.008, Color::BLUE, controller_backward, hand_translation); - let little_inter = hand_pose[HandJoint::LITTLE_INTERMEDIATE]; - draw_joint(&mut gizmos, little_inter.position.to_vec3(), little_inter.orientation.to_quat(), 0.006, Color::BLUE, controller_backward, hand_translation); - let little_dist = hand_pose[HandJoint::LITTLE_DISTAL]; - draw_joint(&mut gizmos, little_dist.position.to_vec3(), little_dist.orientation.to_quat(), 0.004, Color::BLUE, controller_backward, hand_translation); - let little_tip = hand_pose[HandJoint::LITTLE_TIP]; - draw_joint(&mut gizmos, little_tip.position.to_vec3(), little_tip.orientation.to_quat(), 0.002, Color::BLUE, controller_backward, hand_translation); + let little_meta = hand_transform_array[HandJoint::LITTLE_METACARPAL]; + draw_joint( + &mut gizmos, + little_meta.translation, + little_meta.rotation, + 0.01, + Color::BLUE, + controller_backward, + hand_translation, + ); + let little_prox = hand_transform_array[HandJoint::LITTLE_PROXIMAL]; + draw_joint( + &mut gizmos, + little_prox.translation, + little_prox.rotation, + 0.008, + Color::BLUE, + controller_backward, + hand_translation, + ); + let little_inter = hand_transform_array[HandJoint::LITTLE_INTERMEDIATE]; + draw_joint( + &mut gizmos, + little_inter.translation, + little_inter.rotation, + 0.006, + Color::BLUE, + controller_backward, + hand_translation, + ); + let little_dist = hand_transform_array[HandJoint::LITTLE_DISTAL]; + draw_joint( + &mut gizmos, + little_dist.translation, + little_dist.rotation, + 0.004, + Color::BLUE, + controller_backward, + hand_translation, + ); + let little_tip = hand_transform_array[HandJoint::LITTLE_TIP]; + draw_joint( + &mut gizmos, + little_tip.translation, + little_tip.rotation, + 0.002, + Color::BLUE, + controller_backward, + hand_translation, + ); } fn flip_hand_pose(hand_pose: [Posef; 26], hand: Hand) -> [Posef; 26] { @@ -210,52 +660,164 @@ fn flip_hand_pose(hand_pose: [Posef; 26], hand: Hand) -> [Posef; 26] { for pose in new_pose.iter_mut() { pose.position.x = -pose.position.x; } - }, + } Hand::Right => (), } return new_pose; } -fn draw_joint(gizmos: &mut Gizmos, joint_pos: Vec3, joint_rot: Quat, radius: f32, color: Color, controller_backwards: Quat, offset: Vec3) { - gizmos.sphere(controller_backwards.mul_vec3(joint_pos) + offset, joint_rot, radius, color); +fn draw_joint( + gizmos: &mut Gizmos, + joint_pos: Vec3, + joint_rot: Quat, + radius: f32, + color: Color, + controller_backwards: Quat, + offset: Vec3, +) { + gizmos.sphere( + controller_backwards.mul_vec3(joint_pos) + offset, + joint_rot, + radius, + color, + ); } fn log_hand(hand_pose: [Posef; 26]) { - let palm_vec = hand_pose[HandJoint::PALM].position.to_vec3(); - info!("palm: {}", hand_pose[HandJoint::PALM].position.to_vec3() - palm_vec); - info!("wrist: {}", hand_pose[HandJoint::WRIST].position.to_vec3() - palm_vec); + let palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() + - hand_pose[HandJoint::PALM].position.to_vec3(); + info!( + "palm-wrist: {}", + hand_pose[HandJoint::WRIST].position.to_vec3() + - hand_pose[HandJoint::PALM].position.to_vec3() + ); - info!("tm: {}", hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() - palm_vec); - info!("tp: {}", hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() - palm_vec); - info!("td: {}", hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() - palm_vec); - info!("tt: {}", hand_pose[HandJoint::THUMB_TIP].position.to_vec3() - palm_vec); - - info!("im: {}", hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() - palm_vec); - info!("ip: {}", hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() - palm_vec); - info!("ii: {}", hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() - palm_vec); - info!("id: {}", hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() - palm_vec); - info!("it: {}", hand_pose[HandJoint::INDEX_TIP].position.to_vec3() - palm_vec); + info!( + "wrist-tm: {}", + hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "tm-tp: {}", + hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() + ); + info!( + "tp-td: {}", + hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() + - hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() + ); + info!( + "td-tt: {}", + hand_pose[HandJoint::THUMB_TIP].position.to_vec3() + - hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() + ); - info!("mm: {}", hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() - palm_vec); - info!("mp: {}", hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() - palm_vec); - info!("mi: {}", hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() - palm_vec); - info!("md: {}", hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() - palm_vec); - info!("mt: {}", hand_pose[HandJoint::MIDDLE_TIP].position.to_vec3() - palm_vec); + info!( + "wrist-im: {}", + hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "im-ip: {}", + hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() + ); + info!( + "ip-ii: {}", + hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() + ); + info!( + "ii-id: {}", + hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() + - hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() + ); + info!( + "id-it: {}", + hand_pose[HandJoint::INDEX_TIP].position.to_vec3() + - hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() + ); - info!("rm: {}", hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() - palm_vec); - info!("rp: {}", hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() - palm_vec); - info!("ri: {}", hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() - palm_vec); - info!("rd: {}", hand_pose[HandJoint::RING_DISTAL].position.to_vec3() - palm_vec); - info!("rt: {}", hand_pose[HandJoint::RING_TIP].position.to_vec3() - palm_vec); + info!( + "wrist-mm: {}", + hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "mm-mp: {}", + hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() + ); + info!( + "mp-mi: {}", + hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() + ); + info!( + "mi-md: {}", + hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() + ); + info!( + "md-mt: {}", + hand_pose[HandJoint::MIDDLE_TIP].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() + ); - info!("lm: {}", hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() - palm_vec); - info!("lp: {}", hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() - palm_vec); - info!("li: {}", hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() - palm_vec); - info!("ld: {}", hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() - palm_vec); - info!("lt: {}", hand_pose[HandJoint::LITTLE_TIP].position.to_vec3() - palm_vec); + info!( + "wrist-rm: {}", + hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "rm-rp: {}", + hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() + ); + info!( + "rp-ri: {}", + hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() + ); + info!( + "ri-rd: {}", + hand_pose[HandJoint::RING_DISTAL].position.to_vec3() + - hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() + ); + info!( + "rd-rt: {}", + hand_pose[HandJoint::RING_TIP].position.to_vec3() + - hand_pose[HandJoint::RING_DISTAL].position.to_vec3() + ); + + info!( + "wrist-lm: {}", + hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "lm-lp: {}", + hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() + ); + info!( + "lp-li: {}", + hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() + ); + info!( + "li-ld: {}", + hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() + - hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() + ); + info!( + "ld-lt: {}", + hand_pose[HandJoint::LITTLE_TIP].position.to_vec3() + - hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() + ); } - fn spawn_controllers_example(mut commands: Commands) { //left hand commands.spawn(( diff --git a/src/xr_input/hand_poses.rs b/src/xr_input/hand_poses.rs new file mode 100644 index 0000000..70544eb --- /dev/null +++ b/src/xr_input/hand_poses.rs @@ -0,0 +1,520 @@ +use bevy::prelude::{Quat, Transform, Vec3}; +use openxr::{Posef, Quaternionf, Vector3f}; + +use super::Hand; + +pub fn get_simulated_open_hand_transforms(hand: Hand) -> [Transform; 26] { + let test_hand_bones: [Vec3; 26] = [ + Vec3 { + x: 0.0, + y: 0.0, + z: 0.0, + }, //palm + Vec3 { + x: 0.0, + y: 0.0, + z: -0.04, + }, //wrist + Vec3 { + x: -0.02, + y: 0.00, + z: 0.015, + }, //thumb + Vec3 { + x: 0.0, + y: 0.0, + z: 0.03, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.024, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.024, + }, + Vec3 { + x: -0.01, + y: -0.015, + z: 0.0155, + }, //index + Vec3 { + x: 0.0, + y: 0.0, + z: 0.064, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.037, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.02, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.01, + }, + Vec3 { + x: 0.0, + y: -0.02, + z: 0.016, + }, //middle + Vec3 { + x: 0.0, + y: 0.0, + z: 0.064, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.037, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.02, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.01, + }, + Vec3 { + x: 0.01, + y: -0.015, + z: 0.015, + }, //ring + Vec3 { + x: 0.0, + y: 0.0, + z: 0.064, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.037, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.02, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.01, + }, + Vec3 { + x: 0.02, + y: -0.01, + z: 0.015, + }, //little + Vec3 { + x: 0.0, + y: 0.0, + z: 0.064, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.037, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.02, + }, + Vec3 { + x: 0.0, + y: 0.0, + z: 0.01, + }, + ]; + let result = bones_to_transforms(test_hand_bones, hand); + return result; +} + +fn bones_to_transforms(hand_bones: [Vec3; 26], hand: Hand) -> [Transform; 26] { + match hand { + Hand::Left => { + let mut result_array: [Transform; 26] = [Transform::default(); 26]; + for (place, data) in result_array.iter_mut().zip(hand_bones.iter()) { + *place = Transform { + translation: Vec3 { + x: -data.x, + y: -data.y, + z: -data.z, + }, + rotation: Quat::IDENTITY, + scale: Vec3::splat(1.0), + } + } + return result_array; + } + Hand::Right => { + let mut result_array: [Transform; 26] = [Transform::default(); 26]; + for (place, data) in result_array.iter_mut().zip(hand_bones.iter()) { + *place = Transform { + translation: Vec3 { + x: data.x, + y: -data.y, + z: -data.z, + }, + rotation: Quat::IDENTITY, + scale: Vec3::splat(1.0), + } + } + return result_array; + } + } +} + +pub fn get_test_hand_pose_array() -> [Posef; 26] { + let test_hand_pose: [Posef; 26] = [ + Posef { + position: Vector3f { + x: 0.0, + y: 0.0, + z: 0.0, + }, + orientation: Quaternionf { + x: -0.267, + y: 0.849, + z: 0.204, + w: 0.407, + }, + }, //palm + Posef { + position: Vector3f { + x: 0.02, + y: -0.040, + z: -0.015, + }, + orientation: Quaternionf { + x: -0.267, + y: 0.849, + z: 0.204, + w: 0.407, + }, + }, + Posef { + position: Vector3f { + x: 0.019, + y: -0.037, + z: 0.011, + }, + orientation: Quaternionf { + x: -0.744, + y: -0.530, + z: 0.156, + w: -0.376, + }, + }, + Posef { + position: Vector3f { + x: 0.015, + y: -0.014, + z: 0.047, + }, + orientation: Quaternionf { + x: -0.786, + y: -0.550, + z: 0.126, + w: -0.254, + }, + }, + Posef { + position: Vector3f { + x: 0.004, + y: 0.003, + z: 0.068, + }, + orientation: Quaternionf { + x: -0.729, + y: -0.564, + z: 0.027, + w: -0.387, + }, + }, + Posef { + position: Vector3f { + x: -0.009, + y: 0.011, + z: 0.072, + }, + orientation: Quaternionf { + x: -0.585, + y: -0.548, + z: -0.140, + w: -0.582, + }, + }, + Posef { + position: Vector3f { + x: 0.027, + y: -0.021, + z: 0.001, + }, + orientation: Quaternionf { + x: -0.277, + y: -0.826, + z: 0.317, + w: -0.376, + }, + }, + Posef { + position: Vector3f { + x: -0.002, + y: 0.026, + z: 0.034, + }, + orientation: Quaternionf { + x: -0.277, + y: -0.826, + z: 0.317, + w: -0.376, + }, + }, + Posef { + position: Vector3f { + x: -0.023, + y: 0.049, + z: 0.055, + }, + orientation: Quaternionf { + x: -0.244, + y: -0.843, + z: 0.256, + w: -0.404, + }, + }, + Posef { + position: Vector3f { + x: -0.037, + y: 0.059, + z: 0.067, + }, + orientation: Quaternionf { + x: -0.200, + y: -0.866, + z: 0.165, + w: -0.428, + }, + }, + Posef { + position: Vector3f { + x: -0.045, + y: 0.063, + z: 0.073, + }, + orientation: Quaternionf { + x: -0.172, + y: -0.874, + z: 0.110, + w: -0.440, + }, + }, + Posef { + position: Vector3f { + x: 0.021, + y: -0.017, + z: -0.007, + }, + orientation: Quaternionf { + x: -0.185, + y: -0.817, + z: 0.370, + w: -0.401, + }, + }, + Posef { + position: Vector3f { + x: -0.011, + y: 0.029, + z: 0.018, + }, + orientation: Quaternionf { + x: -0.185, + y: -0.817, + z: 0.370, + w: -0.401, + }, + }, + Posef { + position: Vector3f { + x: -0.034, + y: 0.06, + z: 0.033, + }, + orientation: Quaternionf { + x: -0.175, + y: -0.809, + z: 0.371, + w: -0.420, + }, + }, + Posef { + position: Vector3f { + x: -0.051, + y: 0.072, + z: 0.045, + }, + orientation: Quaternionf { + x: -0.109, + y: -0.856, + z: 0.245, + w: -0.443, + }, + }, + Posef { + position: Vector3f { + x: -0.06, + y: 0.077, + z: 0.051, + }, + orientation: Quaternionf { + x: -0.075, + y: -0.871, + z: 0.180, + w: -0.450, + }, + }, + Posef { + position: Vector3f { + x: 0.013, + y: -0.017, + z: -0.015, + }, + orientation: Quaternionf { + x: -0.132, + y: -0.786, + z: 0.408, + w: -0.445, + }, + }, + Posef { + position: Vector3f { + x: -0.02, + y: 0.025, + z: 0.0, + }, + orientation: Quaternionf { + x: -0.132, + y: -0.786, + z: 0.408, + w: -0.445, + }, + }, + Posef { + position: Vector3f { + x: -0.042, + y: 0.055, + z: 0.007, + }, + orientation: Quaternionf { + x: -0.131, + y: -0.762, + z: 0.432, + w: -0.464, + }, + }, + Posef { + position: Vector3f { + x: -0.06, + y: 0.069, + z: 0.015, + }, + orientation: Quaternionf { + x: -0.071, + y: -0.810, + z: 0.332, + w: -0.477, + }, + }, + Posef { + position: Vector3f { + x: -0.069, + y: 0.075, + z: 0.02, + }, + orientation: Quaternionf { + x: -0.029, + y: -0.836, + z: 0.260, + w: -0.482, + }, + }, + Posef { + position: Vector3f { + x: 0.004, + y: -0.022, + z: -0.022, + }, + orientation: Quaternionf { + x: -0.060, + y: -0.749, + z: 0.481, + w: -0.452, + }, + }, + Posef { + position: Vector3f { + x: -0.028, + y: 0.018, + z: -0.015, + }, + orientation: Quaternionf { + x: -0.060, + y: -0.749, + z: 0.481, + w: -0.452, + }, + }, + Posef { + position: Vector3f { + x: -0.046, + y: 0.042, + z: -0.017, + }, + orientation: Quaternionf { + x: -0.061, + y: -0.684, + z: 0.534, + w: -0.493, + }, + }, + Posef { + position: Vector3f { + x: -0.059, + y: 0.053, + z: -0.015, + }, + orientation: Quaternionf { + x: 0.002, + y: -0.745, + z: 0.444, + w: -0.498, + }, + }, + Posef { + position: Vector3f { + x: -0.068, + y: 0.059, + z: -0.013, + }, + orientation: Quaternionf { + x: 0.045, + y: -0.780, + z: 0.378, + w: -0.496, + }, + }, + ]; + return test_hand_pose; +} diff --git a/src/xr_input/mod.rs b/src/xr_input/mod.rs index 55e7c56..1ee3e71 100644 --- a/src/xr_input/mod.rs +++ b/src/xr_input/mod.rs @@ -4,6 +4,7 @@ pub mod oculus_touch; pub mod prototype_locomotion; pub mod trackers; pub mod xr_camera; +pub mod hand_poses; use crate::resources::XrSession; use crate::xr_begin_frame; From 319a2dcf215975679d02216d115eb350f8ccd081 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Sat, 7 Oct 2023 18:38:25 -0400 Subject: [PATCH 06/14] hand state resource is used to drive skeleton --- examples/xr.rs | 318 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 283 insertions(+), 35 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index d34c4b7..7f65bc4 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -5,8 +5,11 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::math::vec3; use bevy::transform::components::Transform; use bevy::{gizmos, prelude::*}; +use bevy_openxr::input::XrInput; +use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession}; use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; use bevy_openxr::xr_input::hand_poses::*; +use bevy_openxr::xr_input::oculus_touch::OculusController; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, @@ -28,7 +31,9 @@ fn main() { .add_systems(Update, proto_locomotion) .add_systems(Startup, spawn_controllers_example) .add_systems(Update, draw_skeleton_hands) + .add_systems(PreUpdate, update_hand_states) .insert_resource(PrototypeLocomotionConfig::default()) + .insert_resource(HandStatesResource::default()) .run(); } @@ -82,25 +87,144 @@ fn setup( },)); } +pub fn update_hand_states( + oculus_controller: Res, + hand_states_option: Option>, + frame_state: Res, + xr_input: Res, + instance: Res, + session: Res, +) { + match hand_states_option { + Some(mut hands) => { + //lock frame + let frame_state = *frame_state.lock().unwrap(); + //get controller + let controller = + oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input); + + //right hand + let squeeze = controller.squeeze(Hand::Right); + let trigger_state = controller.trigger(Hand::Right); + let calc_trigger_state = match controller.trigger_touched(Hand::Right) { + true => match trigger_state > 0.0 { + true => TriggerState::PULLED, + false => TriggerState::TOUCHED, + }, + false => TriggerState::OFF, + }; + //button a + let mut a_state = ButtonState::OFF; + if controller.a_button_touched() { + a_state = ButtonState::TOUCHED; + } + if controller.a_button() { + a_state = ButtonState::PRESSED; + } + + //button b + let mut b_state = ButtonState::OFF; + if controller.b_button_touched() { + b_state = ButtonState::TOUCHED; + } + if controller.b_button() { + b_state = ButtonState::PRESSED; + } + + let thumbstick_state = controller.thumbstick(Hand::Right); + let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Right) { + true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { + true => ThumbstickState::PRESSED, + false => ThumbstickState::TOUCHED, + }, + false => ThumbstickState::OFF, + }; + + let right_state = HandState { + grip: squeeze, + trigger_state: calc_trigger_state, + a_button: a_state, + b_button: b_state, + thumbstick: calc_thumbstick_state, + }; + + //left + let squeeze = controller.squeeze(Hand::Left); + let trigger_state = controller.trigger(Hand::Left); + let calc_trigger_state = match controller.trigger_touched(Hand::Left) { + true => match trigger_state > 0.0 { + true => TriggerState::PULLED, + false => TriggerState::TOUCHED, + }, + false => TriggerState::OFF, + }; + //button a + let mut a_state = ButtonState::OFF; + if controller.x_button_touched() { + a_state = ButtonState::TOUCHED; + } + if controller.x_button() { + a_state = ButtonState::PRESSED; + } + + //button b + let mut b_state = ButtonState::OFF; + if controller.y_button_touched() { + b_state = ButtonState::TOUCHED; + } + if controller.y_button() { + b_state = ButtonState::PRESSED; + } + + let thumbstick_state = controller.thumbstick(Hand::Left); + let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Left) { + true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { + true => ThumbstickState::PRESSED, + false => ThumbstickState::TOUCHED, + }, + false => ThumbstickState::OFF, + }; + + let left_state = HandState { + grip: squeeze, + trigger_state: calc_trigger_state, + a_button: a_state, + b_button: b_state, + thumbstick: calc_thumbstick_state, + }; + + hands.left = left_state; + hands.right = right_state; + } + None => info!("hand states resource not init yet"), + } +} + fn draw_skeleton_hands( mut commands: Commands, mut gizmos: Gizmos, right_controller_query: Query<(&GlobalTransform, With)>, left_controller_query: Query<(&GlobalTransform, With)>, + hand_states_option: Option>, ) { - let left_hand_transform = left_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - draw_hand_bones(&mut gizmos, left_hand_transform, Hand::Left); - let right_hand_transform = right_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - // draw_hand(&mut gizmos, right_hand_transform, Hand::Right); - draw_hand_bones(&mut gizmos, right_hand_transform, Hand::Right); + match hand_states_option { + Some(hands) => { + let left_hand_transform = left_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + draw_hand_bones(&mut gizmos, left_hand_transform, Hand::Left, hands.left); + let right_hand_transform = right_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + // draw_hand(&mut gizmos, right_hand_transform, Hand::Right); + draw_hand_bones(&mut gizmos, right_hand_transform, Hand::Right, hands.right); + } + None => info!("hand states resource not initialized yet"), + } } fn pose_array_to_transform_array(hand_pose: [Posef; 26]) -> [Transform; 26] { @@ -114,8 +238,108 @@ fn pose_array_to_transform_array(hand_pose: [Posef; 26]) -> [Transform; 26] { } return result_array; } +#[derive(Clone, Copy)] +pub enum ButtonState { + OFF, + TOUCHED, + PRESSED, +} -fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand) { +impl Default for ButtonState { + fn default() -> Self { + ButtonState::OFF + } +} +#[derive(Clone, Copy)] +pub enum ThumbstickState { + OFF, + TOUCHED, + PRESSED, +} + +impl Default for ThumbstickState { + fn default() -> Self { + ThumbstickState::OFF + } +} +#[derive(Clone, Copy)] +pub enum TriggerState { + OFF, + TOUCHED, + PULLED, +} + +impl Default for TriggerState { + fn default() -> Self { + TriggerState::OFF + } +} + +#[derive(Default, Resource)] +pub struct HandStatesResource { + pub left: HandState, + pub right: HandState, +} + +#[derive(Clone, Copy)] +pub struct HandState { + grip: f32, + trigger_state: TriggerState, + a_button: ButtonState, + b_button: ButtonState, + thumbstick: ThumbstickState, +} + +impl Default for HandState { + fn default() -> Self { + Self { + grip: Default::default(), + trigger_state: Default::default(), + a_button: Default::default(), + b_button: Default::default(), + thumbstick: Default::default(), + } + } +} + +impl HandState { + pub fn get_index_curl(&self) -> f32 { + match self.trigger_state { + TriggerState::OFF => 0.0, + TriggerState::TOUCHED => 0.50, + TriggerState::PULLED => 1.0, + } + } + + pub fn get_thumb_curl(&self) -> f32 { + match self.thumbstick { + ThumbstickState::OFF => (), + ThumbstickState::TOUCHED => return 0.25, + ThumbstickState::PRESSED => return 0.25, + }; + + match self.a_button { + ButtonState::OFF => (), + ButtonState::TOUCHED => return 0.25, + ButtonState::PRESSED => return 0.25, + }; + + match self.b_button { + ButtonState::OFF => (), + ButtonState::TOUCHED => return 0.25, + ButtonState::PRESSED => return 0.25, + }; + //if no thumb actions taken return open position + return 0.0; + } +} + +fn draw_hand_bones( + mut gizmos: &mut Gizmos, + controller_transform: Transform, + hand: Hand, + hand_state: HandState, +) { let left_hand_rot = Quat::from_rotation_y(180.0 * PI / 180.0); let hand_translation: Vec3 = match hand { Hand::Left => controller_transform.translation, @@ -131,26 +355,30 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han Hand::Left => -1.0, Hand::Right => 1.0, }; + //curl represents how closed the hand is from 0 to 1; + let grip_curl = hand_state.grip; + let index_curl = hand_state.get_index_curl(); + let thumb_curl = hand_state.get_thumb_curl(); //get paml quat let y = Quat::from_rotation_y(-90.0 * PI / 180.0); let x = Quat::from_rotation_x(-90.0 * PI / 180.0); let palm_quat = controller_quat.mul_quat(y).mul_quat(x); //draw debug rays - gizmos.ray( - hand_translation, - palm_quat.mul_vec3(Vec3::Z * 0.2), - Color::BLUE, - ); - gizmos.ray( - hand_translation, - palm_quat.mul_vec3(Vec3::Y * 0.2), - Color::GREEN, - ); - gizmos.ray( - hand_translation, - palm_quat.mul_vec3(Vec3::X * 0.2), - Color::RED, - ); + // gizmos.ray( + // hand_translation, + // palm_quat.mul_vec3(Vec3::Z * 0.2), + // Color::BLUE, + // ); + // gizmos.ray( + // hand_translation, + // palm_quat.mul_vec3(Vec3::Y * 0.2), + // Color::GREEN, + // ); + // gizmos.ray( + // hand_translation, + // palm_quat.mul_vec3(Vec3::X * 0.2), + // Color::RED, + // ); //get simulated bones let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); //draw controller-palm bone(should be zero length) @@ -177,11 +405,13 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han let mut prior_vector: Option = None; let color = Color::RED; let splay = Quat::from_rotation_y(splay_direction * 30.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(splay); + let huh = Quat::from_rotation_x(-35.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(huh).mul_quat(splay); for bone in thumb_joints.iter() { match prior_start { Some(start) => { - let tp_lrot = Quat::from_rotation_y(splay_direction * 5.0 * PI / 180.0); + let curl_angle: f32 = get_bone_curl_angle(*bone, thumb_curl); + let tp_lrot = Quat::from_rotation_y(splay_direction * curl_angle * PI / 180.0); let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); @@ -205,6 +435,7 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han } } + //index //better finger drawing? let thumb_joints = [ HandJoint::INDEX_METACARPAL, @@ -222,7 +453,8 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han for bone in thumb_joints.iter() { match prior_start { Some(start) => { - let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let curl_angle: f32 = get_bone_curl_angle(*bone, index_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); @@ -263,7 +495,8 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han for bone in thumb_joints.iter() { match prior_start { Some(start) => { - let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); @@ -303,7 +536,8 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han for bone in thumb_joints.iter() { match prior_start { Some(start) => { - let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); @@ -344,7 +578,8 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han for bone in thumb_joints.iter() { match prior_start { Some(start) => { - let tp_lrot = Quat::from_rotation_x(-5.0 * PI / 180.0); + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); @@ -369,6 +604,19 @@ fn draw_hand_bones(mut gizmos: &mut Gizmos, controller_transform: Transform, han } } +fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { + let mul: f32 = match bone { + HandJoint::INDEX_PROXIMAL => 0.0, + HandJoint::MIDDLE_PROXIMAL => 0.0, + HandJoint::RING_PROXIMAL => 0.0, + HandJoint::LITTLE_PROXIMAL => 0.0, + HandJoint::THUMB_PROXIMAL => 0.0, + _ => 1.0, + }; + let curl_angle = -((mul * curl * 80.0) + 5.0); + return curl_angle; +} + fn draw_hand(mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand) { //draw debug for controller grip center to match palm to let hand_translation = controller_transform.translation; From abfc376c9957267bc917a47651b54d83c4e4fb6a Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Mon, 9 Oct 2023 00:11:23 -0400 Subject: [PATCH 07/14] spawn bones and update --- examples/xr.rs | 556 ++++++++++++++++++-------------------------- src/xr_input/mod.rs | 4 +- 2 files changed, 231 insertions(+), 329 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 7f65bc4..23de2bd 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -32,6 +32,8 @@ fn main() { .add_systems(Startup, spawn_controllers_example) .add_systems(Update, draw_skeleton_hands) .add_systems(PreUpdate, update_hand_states) + .add_systems(PostUpdate, draw_hand_entities) + .add_systems(Startup, spawn_hand_entities) .insert_resource(PrototypeLocomotionConfig::default()) .insert_resource(HandStatesResource::default()) .run(); @@ -87,6 +89,49 @@ fn setup( },)); } +pub fn spawn_hand_entities(mut commands: Commands) { + let hands = [Hand::Left, Hand::Right]; + let bones = [ + HandBone::PALM, + HandBone::WRIST, + HandBone::THUMB_METACARPAL, + HandBone::THUMB_PROXIMAL, + HandBone::THUMB_DISTAL, + HandBone::THUMB_TIP, + HandBone::INDEX_METACARPAL, + HandBone::INDEX_PROXIMAL, + HandBone::INDEX_INTERMEDIATE, + HandBone::INDEX_DISTAL, + HandBone::INDEX_TIP, + HandBone::MIDDLE_METACARPAL, + HandBone::MIDDLE_PROXIMAL, + HandBone::MIDDLE_INTERMEDIATE, + HandBone::MIDDLE_DISTAL, + HandBone::MIDDLE_TIP, + HandBone::RING_METACARPAL, + HandBone::RING_PROXIMAL, + HandBone::RING_INTERMEDIATE, + HandBone::RING_DISTAL, + HandBone::RING_TIP, + HandBone::LITTLE_METACARPAL, + HandBone::LITTLE_PROXIMAL, + HandBone::LITTLE_INTERMEDIATE, + HandBone::LITTLE_DISTAL, + HandBone::LITTLE_TIP, + ]; + + for hand in hands.iter() { + for bone in bones.iter() { + commands.spawn(( + SpatialBundle::default(), + bone.clone(), + OpenXRTracker, + hand.clone(), + )); + } + } +} + pub fn update_hand_states( oculus_controller: Res, hand_states_option: Option>, @@ -201,11 +246,11 @@ pub fn update_hand_states( } fn draw_skeleton_hands( - mut commands: Commands, mut gizmos: Gizmos, right_controller_query: Query<(&GlobalTransform, With)>, left_controller_query: Query<(&GlobalTransform, With)>, hand_states_option: Option>, + mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>, ) { match hand_states_option { Some(hands) => { @@ -214,30 +259,61 @@ fn draw_skeleton_hands( .unwrap() .0 .compute_transform(); - draw_hand_bones(&mut gizmos, left_hand_transform, Hand::Left, hands.left); + draw_hand_bones( + &mut gizmos, + left_hand_transform, + Hand::Left, + hands.left, + &mut hand_bone_query, + ); let right_hand_transform = right_controller_query .get_single() .unwrap() .0 .compute_transform(); // draw_hand(&mut gizmos, right_hand_transform, Hand::Right); - draw_hand_bones(&mut gizmos, right_hand_transform, Hand::Right, hands.right); + draw_hand_bones( + &mut gizmos, + right_hand_transform, + Hand::Right, + hands.right, + &mut hand_bone_query, + ); } None => info!("hand states resource not initialized yet"), } } -fn pose_array_to_transform_array(hand_pose: [Posef; 26]) -> [Transform; 26] { - let mut result_array: [Transform; 26] = [Transform::default(); 26]; - for (place, data) in result_array.iter_mut().zip(hand_pose.iter()) { - *place = Transform { - translation: data.position.to_vec3(), - rotation: data.orientation.to_quat(), - scale: Vec3::splat(1.0), - } - } - return result_array; +#[derive(Component, Debug, Clone, Copy)] +pub enum HandBone { + PALM, + WRIST, + THUMB_METACARPAL, + THUMB_PROXIMAL, + THUMB_DISTAL, + THUMB_TIP, + INDEX_METACARPAL, + INDEX_PROXIMAL, + INDEX_INTERMEDIATE, + INDEX_DISTAL, + INDEX_TIP, + MIDDLE_METACARPAL, + MIDDLE_PROXIMAL, + MIDDLE_INTERMEDIATE, + MIDDLE_DISTAL, + MIDDLE_TIP, + RING_METACARPAL, + RING_PROXIMAL, + RING_INTERMEDIATE, + RING_DISTAL, + RING_TIP, + LITTLE_METACARPAL, + LITTLE_PROXIMAL, + LITTLE_INTERMEDIATE, + LITTLE_DISTAL, + LITTLE_TIP, } + #[derive(Clone, Copy)] pub enum ButtonState { OFF, @@ -334,11 +410,50 @@ impl HandState { } } +fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) { + for (transform, hand_bone) in query.iter() { + let (radius, color) = get_bone_gizmo_style(hand_bone); + gizmos.sphere(transform.translation, transform.rotation, radius, color); + } +} + +fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { + match hand_bone { + HandBone::PALM => (0.01, Color::WHITE), + HandBone::WRIST => (0.01, Color::GRAY), + HandBone::THUMB_METACARPAL => (0.01, Color::RED), + HandBone::THUMB_PROXIMAL => (0.008, Color::RED), + HandBone::THUMB_DISTAL => (0.006, Color::RED), + HandBone::THUMB_TIP => (0.004, Color::RED), + HandBone::INDEX_METACARPAL => (0.01, Color::ORANGE), + HandBone::INDEX_PROXIMAL => (0.008, Color::ORANGE), + HandBone::INDEX_INTERMEDIATE => (0.006, Color::ORANGE), + HandBone::INDEX_DISTAL => (0.004, Color::ORANGE), + HandBone::INDEX_TIP => (0.002, Color::ORANGE), + HandBone::MIDDLE_METACARPAL => (0.01, Color::YELLOW), + HandBone::MIDDLE_PROXIMAL => (0.008, Color::YELLOW), + HandBone::MIDDLE_INTERMEDIATE => (0.006, Color::YELLOW), + HandBone::MIDDLE_DISTAL => (0.004, Color::YELLOW), + HandBone::MIDDLE_TIP => (0.002, Color::YELLOW), + HandBone::RING_METACARPAL => (0.01, Color::GREEN), + HandBone::RING_PROXIMAL => (0.008, Color::GREEN), + HandBone::RING_INTERMEDIATE => (0.006, Color::GREEN), + HandBone::RING_DISTAL => (0.004, Color::GREEN), + HandBone::RING_TIP => (0.002, Color::GREEN), + HandBone::LITTLE_METACARPAL => (0.01, Color::BLUE), + HandBone::LITTLE_PROXIMAL => (0.008, Color::BLUE), + HandBone::LITTLE_INTERMEDIATE => (0.006, Color::BLUE), + HandBone::LITTLE_DISTAL => (0.004, Color::BLUE), + HandBone::LITTLE_TIP => (0.002, Color::BLUE), + } +} + fn draw_hand_bones( mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand, hand_state: HandState, + hand_bone_query: &mut Query<(&mut Transform, &HandBone, &Hand)>, ) { let left_hand_rot = Quat::from_rotation_y(180.0 * PI / 180.0); let hand_translation: Vec3 = match hand { @@ -355,6 +470,9 @@ fn draw_hand_bones( Hand::Left => -1.0, Hand::Right => 1.0, }; + //lets make a structure to hold our calculated transforms for now + let mut calc_transforms = [Transform::default(); 26]; + //curl represents how closed the hand is from 0 to 1; let grip_curl = hand_state.grip; let index_curl = hand_state.get_index_curl(); @@ -384,6 +502,10 @@ fn draw_hand_bones( //draw controller-palm bone(should be zero length) let palm = hand_transform_array[HandJoint::PALM]; gizmos.ray(hand_translation, palm.translation, Color::WHITE); + calc_transforms[HandJoint::PALM] = Transform { + translation: hand_translation + palm.translation, + ..default() + }; //draw palm-wrist let wrist = hand_transform_array[HandJoint::WRIST]; gizmos.ray( @@ -391,6 +513,10 @@ fn draw_hand_bones( palm_quat.mul_vec3(wrist.translation), Color::GRAY, ); + calc_transforms[HandJoint::WRIST] = Transform { + translation: hand_translation + palm.translation + palm_quat.mul_vec3(wrist.translation), + ..default() + }; //thumb //better finger drawing? @@ -420,6 +546,11 @@ fn draw_hand_bones( prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; } None => { let thumb_meta = hand_transform_array[*bone]; @@ -431,6 +562,11 @@ fn draw_hand_bones( prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; } } } @@ -463,6 +599,11 @@ fn draw_hand_bones( prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; } None => { let thumb_meta = hand_transform_array[*bone]; @@ -474,6 +615,11 @@ fn draw_hand_bones( prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; } } } @@ -505,6 +651,11 @@ fn draw_hand_bones( prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; } None => { let thumb_meta = hand_transform_array[*bone]; @@ -516,6 +667,11 @@ fn draw_hand_bones( prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; } } } @@ -546,6 +702,11 @@ fn draw_hand_bones( prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; } None => { let thumb_meta = hand_transform_array[*bone]; @@ -557,6 +718,11 @@ fn draw_hand_bones( prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; } } } @@ -588,6 +754,11 @@ fn draw_hand_bones( prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; } None => { let thumb_meta = hand_transform_array[*bone]; @@ -599,9 +770,54 @@ fn draw_hand_bones( prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; } } } + + //now that we have all the transforms lets assign them + for (mut transform, handbone, bonehand) in hand_bone_query.iter_mut() { + if *bonehand == hand { + //if the hands match lets go + let index = match_index(handbone); + *transform = calc_transforms[index]; + } + } +} + +fn match_index(handbone: &HandBone) -> HandJoint { + match handbone { + HandBone::PALM => HandJoint::PALM, + HandBone::WRIST => HandJoint::WRIST, + HandBone::THUMB_METACARPAL => HandJoint::THUMB_METACARPAL, + HandBone::THUMB_PROXIMAL => HandJoint::THUMB_PROXIMAL, + HandBone::THUMB_DISTAL => HandJoint::THUMB_DISTAL, + HandBone::THUMB_TIP => HandJoint::THUMB_TIP, + HandBone::INDEX_METACARPAL => HandJoint::INDEX_METACARPAL, + HandBone::INDEX_PROXIMAL => HandJoint::INDEX_PROXIMAL, + HandBone::INDEX_INTERMEDIATE => HandJoint::INDEX_INTERMEDIATE, + HandBone::INDEX_DISTAL => HandJoint::INDEX_DISTAL, + HandBone::INDEX_TIP => HandJoint::INDEX_TIP, + HandBone::MIDDLE_METACARPAL => HandJoint::MIDDLE_METACARPAL, + HandBone::MIDDLE_PROXIMAL => HandJoint::MIDDLE_PROXIMAL, + HandBone::MIDDLE_INTERMEDIATE => HandJoint::MIDDLE_INTERMEDIATE, + HandBone::MIDDLE_DISTAL => HandJoint::MIDDLE_DISTAL, + HandBone::MIDDLE_TIP => HandJoint::MIDDLE_TIP, + HandBone::RING_METACARPAL => HandJoint::RING_METACARPAL, + HandBone::RING_PROXIMAL => HandJoint::RING_PROXIMAL, + HandBone::RING_INTERMEDIATE => HandJoint::RING_INTERMEDIATE, + HandBone::RING_DISTAL => HandJoint::RING_DISTAL, + HandBone::RING_TIP => HandJoint::RING_TIP, + HandBone::LITTLE_METACARPAL => HandJoint::LITTLE_METACARPAL, + HandBone::LITTLE_PROXIMAL => HandJoint::LITTLE_PROXIMAL, + HandBone::LITTLE_INTERMEDIATE => HandJoint::LITTLE_INTERMEDIATE, + HandBone::LITTLE_DISTAL => HandJoint::LITTLE_DISTAL, + HandBone::LITTLE_TIP => HandJoint::LITTLE_TIP, + } } fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { @@ -617,320 +833,6 @@ fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { return curl_angle; } -fn draw_hand(mut gizmos: &mut Gizmos, controller_transform: Transform, hand: Hand) { - //draw debug for controller grip center to match palm to - let hand_translation = controller_transform.translation; - let hand_quat = controller_transform.rotation; - //we need to flip this i dont know why - let flip = Quat::from_rotation_x(PI); - let controller_backward = hand_quat.mul_quat(flip); - - let test_hand_pose = get_test_hand_pose_array(); - - let hand_pose = flip_hand_pose(test_hand_pose.clone(), hand); - - // let hand_transform_array: [Transform; 26] = pose_array_to_transform_array(hand_pose); - let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); - - let palm = hand_transform_array[HandJoint::PALM]; - gizmos.sphere( - palm.translation + hand_translation, - palm.rotation.mul_quat(controller_backward), - 0.01, - Color::WHITE, - ); - - let wrist = hand_transform_array[HandJoint::WRIST]; - draw_joint( - &mut gizmos, - wrist.translation, - wrist.rotation, - 0.01, - Color::GRAY, - controller_backward, - hand_translation, - ); - - let thumb_meta = hand_transform_array[HandJoint::THUMB_METACARPAL]; - draw_joint( - &mut gizmos, - thumb_meta.translation, - thumb_meta.rotation, - 0.01, - Color::RED, - controller_backward, - hand_translation, - ); - - let thumb_prox = hand_transform_array[HandJoint::THUMB_PROXIMAL]; - draw_joint( - &mut gizmos, - thumb_prox.translation, - thumb_prox.rotation, - 0.008, - Color::RED, - controller_backward, - hand_translation, - ); - let thumb_dist = hand_transform_array[HandJoint::THUMB_DISTAL]; - draw_joint( - &mut gizmos, - thumb_dist.translation, - thumb_dist.rotation, - 0.006, - Color::RED, - controller_backward, - hand_translation, - ); - let thumb_tip = hand_transform_array[HandJoint::THUMB_TIP]; - draw_joint( - &mut gizmos, - thumb_tip.translation, - thumb_tip.rotation, - 0.004, - Color::RED, - controller_backward, - hand_translation, - ); - - let index_meta = hand_transform_array[HandJoint::INDEX_METACARPAL]; - draw_joint( - &mut gizmos, - index_meta.translation, - index_meta.rotation, - 0.01, - Color::ORANGE, - controller_backward, - hand_translation, - ); - let index_prox = hand_transform_array[HandJoint::INDEX_PROXIMAL]; - draw_joint( - &mut gizmos, - index_prox.translation, - index_prox.rotation, - 0.008, - Color::ORANGE, - controller_backward, - hand_translation, - ); - - let index_inter = hand_transform_array[HandJoint::INDEX_INTERMEDIATE]; - draw_joint( - &mut gizmos, - index_inter.translation, - index_inter.rotation, - 0.006, - Color::ORANGE, - controller_backward, - hand_translation, - ); - - let index_dist = hand_transform_array[HandJoint::INDEX_DISTAL]; - draw_joint( - &mut gizmos, - index_dist.translation, - index_dist.rotation, - 0.004, - Color::ORANGE, - controller_backward, - hand_translation, - ); - - let index_tip = hand_transform_array[HandJoint::INDEX_TIP]; - draw_joint( - &mut gizmos, - index_tip.translation, - index_tip.rotation, - 0.002, - Color::ORANGE, - controller_backward, - hand_translation, - ); - - let middle_meta = hand_transform_array[HandJoint::MIDDLE_METACARPAL]; - draw_joint( - &mut gizmos, - middle_meta.translation, - middle_meta.rotation, - 0.01, - Color::YELLOW, - controller_backward, - hand_translation, - ); - let middle_prox = hand_transform_array[HandJoint::MIDDLE_PROXIMAL]; - draw_joint( - &mut gizmos, - middle_prox.translation, - middle_prox.rotation, - 0.008, - Color::YELLOW, - controller_backward, - hand_translation, - ); - let middle_inter = hand_transform_array[HandJoint::MIDDLE_INTERMEDIATE]; - draw_joint( - &mut gizmos, - middle_inter.translation, - middle_inter.rotation, - 0.006, - Color::YELLOW, - controller_backward, - hand_translation, - ); - let middle_dist = hand_transform_array[HandJoint::MIDDLE_DISTAL]; - draw_joint( - &mut gizmos, - middle_dist.translation, - middle_dist.rotation, - 0.004, - Color::YELLOW, - controller_backward, - hand_translation, - ); - let middle_tip = hand_transform_array[HandJoint::MIDDLE_TIP]; - draw_joint( - &mut gizmos, - middle_tip.translation, - middle_tip.rotation, - 0.002, - Color::YELLOW, - controller_backward, - hand_translation, - ); - - let ring_meta = hand_transform_array[HandJoint::RING_METACARPAL]; - draw_joint( - &mut gizmos, - ring_meta.translation, - ring_meta.rotation, - 0.01, - Color::GREEN, - controller_backward, - hand_translation, - ); - let ring_prox = hand_transform_array[HandJoint::RING_PROXIMAL]; - draw_joint( - &mut gizmos, - ring_prox.translation, - ring_prox.rotation, - 0.008, - Color::GREEN, - controller_backward, - hand_translation, - ); - let ring_inter = hand_transform_array[HandJoint::RING_INTERMEDIATE]; - draw_joint( - &mut gizmos, - ring_inter.translation, - ring_inter.rotation, - 0.006, - Color::GREEN, - controller_backward, - hand_translation, - ); - let ring_dist = hand_transform_array[HandJoint::RING_DISTAL]; - draw_joint( - &mut gizmos, - ring_dist.translation, - ring_dist.rotation, - 0.004, - Color::GREEN, - controller_backward, - hand_translation, - ); - let ring_tip = hand_transform_array[HandJoint::RING_TIP]; - draw_joint( - &mut gizmos, - ring_tip.translation, - ring_tip.rotation, - 0.002, - Color::GREEN, - controller_backward, - hand_translation, - ); - - let little_meta = hand_transform_array[HandJoint::LITTLE_METACARPAL]; - draw_joint( - &mut gizmos, - little_meta.translation, - little_meta.rotation, - 0.01, - Color::BLUE, - controller_backward, - hand_translation, - ); - let little_prox = hand_transform_array[HandJoint::LITTLE_PROXIMAL]; - draw_joint( - &mut gizmos, - little_prox.translation, - little_prox.rotation, - 0.008, - Color::BLUE, - controller_backward, - hand_translation, - ); - let little_inter = hand_transform_array[HandJoint::LITTLE_INTERMEDIATE]; - draw_joint( - &mut gizmos, - little_inter.translation, - little_inter.rotation, - 0.006, - Color::BLUE, - controller_backward, - hand_translation, - ); - let little_dist = hand_transform_array[HandJoint::LITTLE_DISTAL]; - draw_joint( - &mut gizmos, - little_dist.translation, - little_dist.rotation, - 0.004, - Color::BLUE, - controller_backward, - hand_translation, - ); - let little_tip = hand_transform_array[HandJoint::LITTLE_TIP]; - draw_joint( - &mut gizmos, - little_tip.translation, - little_tip.rotation, - 0.002, - Color::BLUE, - controller_backward, - hand_translation, - ); -} - -fn flip_hand_pose(hand_pose: [Posef; 26], hand: Hand) -> [Posef; 26] { - let mut new_pose = hand_pose; - match hand { - Hand::Left => { - for pose in new_pose.iter_mut() { - pose.position.x = -pose.position.x; - } - } - Hand::Right => (), - } - return new_pose; -} - -fn draw_joint( - gizmos: &mut Gizmos, - joint_pos: Vec3, - joint_rot: Quat, - radius: f32, - color: Color, - controller_backwards: Quat, - offset: Vec3, -) { - gizmos.sphere( - controller_backwards.mul_vec3(joint_pos) + offset, - joint_rot, - radius, - color, - ); -} - fn log_hand(hand_pose: [Posef; 26]) { let palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() - hand_pose[HandJoint::PALM].position.to_vec3(); diff --git a/src/xr_input/mod.rs b/src/xr_input/mod.rs index 1ee3e71..a81badc 100644 --- a/src/xr_input/mod.rs +++ b/src/xr_input/mod.rs @@ -13,7 +13,7 @@ use crate::xr_input::oculus_touch::{setup_oculus_controller, ActionSets}; use crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCameraBundle}; use bevy::app::{App, PostUpdate, Startup}; use bevy::log::warn; -use bevy::prelude::{BuildChildren, IntoSystemConfigs}; +use bevy::prelude::{BuildChildren, IntoSystemConfigs, Component}; use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, SpatialBundle, Update, Vec3}; use bevy::render::camera::CameraProjectionPlugin; use bevy::render::view::{update_frusta, VisibilitySystems}; @@ -28,7 +28,7 @@ use self::trackers::{ pub struct OpenXrInput { pub controller_type: XrControllerType, } -#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Component)] pub enum Hand { Left, Right, From cae43d417569658455fbb35205f7bc585bb78b2c Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Mon, 9 Oct 2023 00:16:14 -0400 Subject: [PATCH 08/14] slight cleaning --- examples/xr.rs | 66 +++++++++----------------------------------------- 1 file changed, 11 insertions(+), 55 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 23de2bd..91f6587 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -30,7 +30,7 @@ fn main() { .add_systems(Startup, setup) .add_systems(Update, proto_locomotion) .add_systems(Startup, spawn_controllers_example) - .add_systems(Update, draw_skeleton_hands) + .add_systems(Update, update_emulated_hand_skeletons) .add_systems(PreUpdate, update_hand_states) .add_systems(PostUpdate, draw_hand_entities) .add_systems(Startup, spawn_hand_entities) @@ -245,8 +245,7 @@ pub fn update_hand_states( } } -fn draw_skeleton_hands( - mut gizmos: Gizmos, +fn update_emulated_hand_skeletons( right_controller_query: Query<(&GlobalTransform, With)>, left_controller_query: Query<(&GlobalTransform, With)>, hand_states_option: Option>, @@ -259,8 +258,7 @@ fn draw_skeleton_hands( .unwrap() .0 .compute_transform(); - draw_hand_bones( - &mut gizmos, + update_hand_bones_emulated( left_hand_transform, Hand::Left, hands.left, @@ -271,9 +269,7 @@ fn draw_skeleton_hands( .unwrap() .0 .compute_transform(); - // draw_hand(&mut gizmos, right_hand_transform, Hand::Right); - draw_hand_bones( - &mut gizmos, + update_hand_bones_emulated( right_hand_transform, Hand::Right, hands.right, @@ -448,8 +444,7 @@ fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { } } -fn draw_hand_bones( - mut gizmos: &mut Gizmos, +fn update_hand_bones_emulated( controller_transform: Transform, hand: Hand, hand_state: HandState, @@ -477,49 +472,26 @@ fn draw_hand_bones( let grip_curl = hand_state.grip; let index_curl = hand_state.get_index_curl(); let thumb_curl = hand_state.get_thumb_curl(); - //get paml quat + //get palm quat let y = Quat::from_rotation_y(-90.0 * PI / 180.0); let x = Quat::from_rotation_x(-90.0 * PI / 180.0); let palm_quat = controller_quat.mul_quat(y).mul_quat(x); - //draw debug rays - // gizmos.ray( - // hand_translation, - // palm_quat.mul_vec3(Vec3::Z * 0.2), - // Color::BLUE, - // ); - // gizmos.ray( - // hand_translation, - // palm_quat.mul_vec3(Vec3::Y * 0.2), - // Color::GREEN, - // ); - // gizmos.ray( - // hand_translation, - // palm_quat.mul_vec3(Vec3::X * 0.2), - // Color::RED, - // ); //get simulated bones let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); - //draw controller-palm bone(should be zero length) + //palm let palm = hand_transform_array[HandJoint::PALM]; - gizmos.ray(hand_translation, palm.translation, Color::WHITE); calc_transforms[HandJoint::PALM] = Transform { translation: hand_translation + palm.translation, ..default() }; - //draw palm-wrist + //wrist let wrist = hand_transform_array[HandJoint::WRIST]; - gizmos.ray( - hand_translation + palm.translation, - palm_quat.mul_vec3(wrist.translation), - Color::GRAY, - ); calc_transforms[HandJoint::WRIST] = Transform { translation: hand_translation + palm.translation + palm_quat.mul_vec3(wrist.translation), ..default() }; //thumb - //better finger drawing? let thumb_joints = [ HandJoint::THUMB_METACARPAL, HandJoint::THUMB_PROXIMAL, @@ -529,7 +501,6 @@ fn draw_hand_bones( let mut prior_start: Option = None; let mut prior_quat: Option = None; let mut prior_vector: Option = None; - let color = Color::RED; let splay = Quat::from_rotation_y(splay_direction * 30.0 * PI / 180.0); let huh = Quat::from_rotation_x(-35.0 * PI / 180.0); let splay_quat = palm_quat.mul_quat(huh).mul_quat(splay); @@ -542,7 +513,6 @@ fn draw_hand_bones( let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - gizmos.ray(tp_start, tp_vector, color); prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); @@ -558,7 +528,6 @@ fn draw_hand_bones( + palm_quat.mul_vec3(palm.translation) + palm_quat.mul_vec3(wrist.translation); let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - gizmos.ray(tm_start, tm_vector, color); prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); @@ -572,7 +541,6 @@ fn draw_hand_bones( } //index - //better finger drawing? let thumb_joints = [ HandJoint::INDEX_METACARPAL, HandJoint::INDEX_PROXIMAL, @@ -583,7 +551,6 @@ fn draw_hand_bones( let mut prior_start: Option = None; let mut prior_quat: Option = None; let mut prior_vector: Option = None; - let color = Color::ORANGE; let splay = Quat::from_rotation_y(splay_direction * 10.0 * PI / 180.0); let splay_quat = palm_quat.mul_quat(splay); for bone in thumb_joints.iter() { @@ -595,7 +562,6 @@ fn draw_hand_bones( let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - gizmos.ray(tp_start, tp_vector, color); prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); @@ -611,7 +577,6 @@ fn draw_hand_bones( + palm_quat.mul_vec3(palm.translation) + palm_quat.mul_vec3(wrist.translation); let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - gizmos.ray(tm_start, tm_vector, color); prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); @@ -624,7 +589,7 @@ fn draw_hand_bones( } } - //better finger drawing? + //middle let thumb_joints = [ HandJoint::MIDDLE_METACARPAL, HandJoint::MIDDLE_PROXIMAL, @@ -635,7 +600,6 @@ fn draw_hand_bones( let mut prior_start: Option = None; let mut prior_quat: Option = None; let mut prior_vector: Option = None; - let color = Color::YELLOW; let splay = Quat::from_rotation_y(splay_direction * 0.0 * PI / 180.0); let splay_quat = palm_quat.mul_quat(splay); for bone in thumb_joints.iter() { @@ -647,7 +611,6 @@ fn draw_hand_bones( let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - gizmos.ray(tp_start, tp_vector, color); prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); @@ -663,7 +626,6 @@ fn draw_hand_bones( + palm_quat.mul_vec3(palm.translation) + palm_quat.mul_vec3(wrist.translation); let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - gizmos.ray(tm_start, tm_vector, color); prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); @@ -675,7 +637,7 @@ fn draw_hand_bones( } } } - //better finger drawing? + //ring let thumb_joints = [ HandJoint::RING_METACARPAL, HandJoint::RING_PROXIMAL, @@ -686,7 +648,6 @@ fn draw_hand_bones( let mut prior_start: Option = None; let mut prior_quat: Option = None; let mut prior_vector: Option = None; - let color = Color::GREEN; let splay = Quat::from_rotation_y(splay_direction * -10.0 * PI / 180.0); let splay_quat = palm_quat.mul_quat(splay); for bone in thumb_joints.iter() { @@ -698,7 +659,6 @@ fn draw_hand_bones( let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - gizmos.ray(tp_start, tp_vector, color); prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); @@ -714,7 +674,6 @@ fn draw_hand_bones( + palm_quat.mul_vec3(palm.translation) + palm_quat.mul_vec3(wrist.translation); let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - gizmos.ray(tm_start, tm_vector, color); prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); @@ -727,7 +686,7 @@ fn draw_hand_bones( } } - //better finger drawing? + //little let thumb_joints = [ HandJoint::LITTLE_METACARPAL, HandJoint::LITTLE_PROXIMAL, @@ -738,7 +697,6 @@ fn draw_hand_bones( let mut prior_start: Option = None; let mut prior_quat: Option = None; let mut prior_vector: Option = None; - let color = Color::BLUE; let splay = Quat::from_rotation_y(splay_direction * -20.0 * PI / 180.0); let splay_quat = palm_quat.mul_quat(splay); for bone in thumb_joints.iter() { @@ -750,7 +708,6 @@ fn draw_hand_bones( let thumb_prox = hand_transform_array[*bone]; let tp_start = start + prior_vector.unwrap(); let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - gizmos.ray(tp_start, tp_vector, color); prior_start = Some(tp_start); prior_quat = Some(tp_quat); prior_vector = Some(tp_vector); @@ -766,7 +723,6 @@ fn draw_hand_bones( + palm_quat.mul_vec3(palm.translation) + palm_quat.mul_vec3(wrist.translation); let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - gizmos.ray(tm_start, tm_vector, color); prior_start = Some(tm_start); prior_quat = Some(splay_quat); prior_vector = Some(tm_vector); From 5c507413c840f22b60a9439f2a20fca9cb077630 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Mon, 9 Oct 2023 00:17:31 -0400 Subject: [PATCH 09/14] cargo fix --- examples/xr.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 91f6587..ad738a9 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,22 +1,22 @@ use std::f32::consts::PI; -use std::ops::Mul; + use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; -use bevy::math::vec3; + use bevy::transform::components::Transform; -use bevy::{gizmos, prelude::*}; +use bevy::{prelude::*}; use bevy_openxr::input::XrInput; use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession}; -use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; + use bevy_openxr::xr_input::hand_poses::*; use bevy_openxr::xr_input::oculus_touch::OculusController; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, }; -use bevy_openxr::xr_input::{Hand, QuatConv, Vec3Conv}; +use bevy_openxr::xr_input::{Hand, Vec3Conv}; use bevy_openxr::DefaultXrPlugins; -use openxr::{HandJoint, Posef, Quaternionf, Vector3f}; +use openxr::{HandJoint, Posef}; fn main() { color_eyre::install().unwrap(); @@ -790,7 +790,7 @@ fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { } fn log_hand(hand_pose: [Posef; 26]) { - let palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() + let _palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() - hand_pose[HandJoint::PALM].position.to_vec3(); info!( "palm-wrist: {}", From 539753790a385e978698ec21dcffd72d56ef8ffa Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Mon, 9 Oct 2023 00:19:43 -0400 Subject: [PATCH 10/14] mass rename --- examples/xr.rs | 210 ++++++++++++++++++++++++------------------------- 1 file changed, 105 insertions(+), 105 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index ad738a9..d47b6f8 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -4,7 +4,7 @@ use std::f32::consts::PI; use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::transform::components::Transform; -use bevy::{prelude::*}; +use bevy::prelude::*; use bevy_openxr::input::XrInput; use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession}; @@ -92,32 +92,32 @@ fn setup( pub fn spawn_hand_entities(mut commands: Commands) { let hands = [Hand::Left, Hand::Right]; let bones = [ - HandBone::PALM, - HandBone::WRIST, - HandBone::THUMB_METACARPAL, - HandBone::THUMB_PROXIMAL, - HandBone::THUMB_DISTAL, - HandBone::THUMB_TIP, - HandBone::INDEX_METACARPAL, - HandBone::INDEX_PROXIMAL, - HandBone::INDEX_INTERMEDIATE, - HandBone::INDEX_DISTAL, - HandBone::INDEX_TIP, - HandBone::MIDDLE_METACARPAL, - HandBone::MIDDLE_PROXIMAL, - HandBone::MIDDLE_INTERMEDIATE, - HandBone::MIDDLE_DISTAL, - HandBone::MIDDLE_TIP, - HandBone::RING_METACARPAL, - HandBone::RING_PROXIMAL, - HandBone::RING_INTERMEDIATE, - HandBone::RING_DISTAL, - HandBone::RING_TIP, - HandBone::LITTLE_METACARPAL, - HandBone::LITTLE_PROXIMAL, - HandBone::LITTLE_INTERMEDIATE, - HandBone::LITTLE_DISTAL, - HandBone::LITTLE_TIP, + HandBone::Palm, + HandBone::Wrist, + HandBone::ThumbMetacarpal, + HandBone::ThumbProximal, + HandBone::ThumbDistal, + HandBone::ThumbTip, + HandBone::IndexMetacarpal, + HandBone::IndexProximal, + HandBone::IndexIntermediate, + HandBone::IndexDistal, + HandBone::IndexTip, + HandBone::MiddleMetacarpal, + HandBone::MiddleProximal, + HandBone::MiddleIntermediate, + HandBone::MiddleDistal, + HandBone::MiddleTip, + HandBone::RingMetacarpal, + HandBone::RingProximal, + HandBone::RingIntermediate, + HandBone::RingDistal, + HandBone::RingTip, + HandBone::LittleMetacarpal, + HandBone::LittleProximal, + HandBone::LittleIntermediate, + HandBone::LittleDistal, + HandBone::LittleTip, ]; for hand in hands.iter() { @@ -282,32 +282,32 @@ fn update_emulated_hand_skeletons( #[derive(Component, Debug, Clone, Copy)] pub enum HandBone { - PALM, - WRIST, - THUMB_METACARPAL, - THUMB_PROXIMAL, - THUMB_DISTAL, - THUMB_TIP, - INDEX_METACARPAL, - INDEX_PROXIMAL, - INDEX_INTERMEDIATE, - INDEX_DISTAL, - INDEX_TIP, - MIDDLE_METACARPAL, - MIDDLE_PROXIMAL, - MIDDLE_INTERMEDIATE, - MIDDLE_DISTAL, - MIDDLE_TIP, - RING_METACARPAL, - RING_PROXIMAL, - RING_INTERMEDIATE, - RING_DISTAL, - RING_TIP, - LITTLE_METACARPAL, - LITTLE_PROXIMAL, - LITTLE_INTERMEDIATE, - LITTLE_DISTAL, - LITTLE_TIP, + 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(Clone, Copy)] @@ -415,32 +415,32 @@ fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { match hand_bone { - HandBone::PALM => (0.01, Color::WHITE), - HandBone::WRIST => (0.01, Color::GRAY), - HandBone::THUMB_METACARPAL => (0.01, Color::RED), - HandBone::THUMB_PROXIMAL => (0.008, Color::RED), - HandBone::THUMB_DISTAL => (0.006, Color::RED), - HandBone::THUMB_TIP => (0.004, Color::RED), - HandBone::INDEX_METACARPAL => (0.01, Color::ORANGE), - HandBone::INDEX_PROXIMAL => (0.008, Color::ORANGE), - HandBone::INDEX_INTERMEDIATE => (0.006, Color::ORANGE), - HandBone::INDEX_DISTAL => (0.004, Color::ORANGE), - HandBone::INDEX_TIP => (0.002, Color::ORANGE), - HandBone::MIDDLE_METACARPAL => (0.01, Color::YELLOW), - HandBone::MIDDLE_PROXIMAL => (0.008, Color::YELLOW), - HandBone::MIDDLE_INTERMEDIATE => (0.006, Color::YELLOW), - HandBone::MIDDLE_DISTAL => (0.004, Color::YELLOW), - HandBone::MIDDLE_TIP => (0.002, Color::YELLOW), - HandBone::RING_METACARPAL => (0.01, Color::GREEN), - HandBone::RING_PROXIMAL => (0.008, Color::GREEN), - HandBone::RING_INTERMEDIATE => (0.006, Color::GREEN), - HandBone::RING_DISTAL => (0.004, Color::GREEN), - HandBone::RING_TIP => (0.002, Color::GREEN), - HandBone::LITTLE_METACARPAL => (0.01, Color::BLUE), - HandBone::LITTLE_PROXIMAL => (0.008, Color::BLUE), - HandBone::LITTLE_INTERMEDIATE => (0.006, Color::BLUE), - HandBone::LITTLE_DISTAL => (0.004, Color::BLUE), - HandBone::LITTLE_TIP => (0.002, Color::BLUE), + HandBone::Palm => (0.01, Color::WHITE), + HandBone::Wrist => (0.01, Color::GRAY), + HandBone::ThumbMetacarpal => (0.01, Color::RED), + HandBone::ThumbProximal => (0.008, Color::RED), + HandBone::ThumbDistal => (0.006, Color::RED), + HandBone::ThumbTip => (0.004, Color::RED), + HandBone::IndexMetacarpal => (0.01, Color::ORANGE), + HandBone::IndexProximal => (0.008, Color::ORANGE), + HandBone::IndexIntermediate => (0.006, Color::ORANGE), + HandBone::IndexDistal => (0.004, Color::ORANGE), + HandBone::IndexTip => (0.002, Color::ORANGE), + HandBone::MiddleMetacarpal => (0.01, Color::YELLOW), + HandBone::MiddleProximal => (0.008, Color::YELLOW), + HandBone::MiddleIntermediate => (0.006, Color::YELLOW), + HandBone::MiddleDistal => (0.004, Color::YELLOW), + HandBone::MiddleTip => (0.002, Color::YELLOW), + HandBone::RingMetacarpal => (0.01, Color::GREEN), + HandBone::RingProximal => (0.008, Color::GREEN), + HandBone::RingIntermediate => (0.006, Color::GREEN), + HandBone::RingDistal => (0.004, Color::GREEN), + HandBone::RingTip => (0.002, Color::GREEN), + HandBone::LittleMetacarpal => (0.01, Color::BLUE), + HandBone::LittleProximal => (0.008, Color::BLUE), + HandBone::LittleIntermediate => (0.006, Color::BLUE), + HandBone::LittleDistal => (0.004, Color::BLUE), + HandBone::LittleTip => (0.002, Color::BLUE), } } @@ -747,32 +747,32 @@ fn update_hand_bones_emulated( fn match_index(handbone: &HandBone) -> HandJoint { match handbone { - HandBone::PALM => HandJoint::PALM, - HandBone::WRIST => HandJoint::WRIST, - HandBone::THUMB_METACARPAL => HandJoint::THUMB_METACARPAL, - HandBone::THUMB_PROXIMAL => HandJoint::THUMB_PROXIMAL, - HandBone::THUMB_DISTAL => HandJoint::THUMB_DISTAL, - HandBone::THUMB_TIP => HandJoint::THUMB_TIP, - HandBone::INDEX_METACARPAL => HandJoint::INDEX_METACARPAL, - HandBone::INDEX_PROXIMAL => HandJoint::INDEX_PROXIMAL, - HandBone::INDEX_INTERMEDIATE => HandJoint::INDEX_INTERMEDIATE, - HandBone::INDEX_DISTAL => HandJoint::INDEX_DISTAL, - HandBone::INDEX_TIP => HandJoint::INDEX_TIP, - HandBone::MIDDLE_METACARPAL => HandJoint::MIDDLE_METACARPAL, - HandBone::MIDDLE_PROXIMAL => HandJoint::MIDDLE_PROXIMAL, - HandBone::MIDDLE_INTERMEDIATE => HandJoint::MIDDLE_INTERMEDIATE, - HandBone::MIDDLE_DISTAL => HandJoint::MIDDLE_DISTAL, - HandBone::MIDDLE_TIP => HandJoint::MIDDLE_TIP, - HandBone::RING_METACARPAL => HandJoint::RING_METACARPAL, - HandBone::RING_PROXIMAL => HandJoint::RING_PROXIMAL, - HandBone::RING_INTERMEDIATE => HandJoint::RING_INTERMEDIATE, - HandBone::RING_DISTAL => HandJoint::RING_DISTAL, - HandBone::RING_TIP => HandJoint::RING_TIP, - HandBone::LITTLE_METACARPAL => HandJoint::LITTLE_METACARPAL, - HandBone::LITTLE_PROXIMAL => HandJoint::LITTLE_PROXIMAL, - HandBone::LITTLE_INTERMEDIATE => HandJoint::LITTLE_INTERMEDIATE, - HandBone::LITTLE_DISTAL => HandJoint::LITTLE_DISTAL, - HandBone::LITTLE_TIP => HandJoint::LITTLE_TIP, + HandBone::Palm => HandJoint::PALM, + HandBone::Wrist => HandJoint::WRIST, + HandBone::ThumbMetacarpal => HandJoint::THUMB_METACARPAL, + HandBone::ThumbProximal => HandJoint::THUMB_PROXIMAL, + HandBone::ThumbDistal => HandJoint::THUMB_DISTAL, + HandBone::ThumbTip => HandJoint::THUMB_TIP, + HandBone::IndexMetacarpal => HandJoint::INDEX_METACARPAL, + HandBone::IndexProximal => HandJoint::INDEX_PROXIMAL, + HandBone::IndexIntermediate => HandJoint::INDEX_INTERMEDIATE, + HandBone::IndexDistal => HandJoint::INDEX_DISTAL, + HandBone::IndexTip => HandJoint::INDEX_TIP, + HandBone::MiddleMetacarpal => HandJoint::MIDDLE_METACARPAL, + HandBone::MiddleProximal => HandJoint::MIDDLE_PROXIMAL, + HandBone::MiddleIntermediate => HandJoint::MIDDLE_INTERMEDIATE, + HandBone::MiddleDistal => HandJoint::MIDDLE_DISTAL, + HandBone::MiddleTip => HandJoint::MIDDLE_TIP, + HandBone::RingMetacarpal => HandJoint::RING_METACARPAL, + HandBone::RingProximal => HandJoint::RING_PROXIMAL, + HandBone::RingIntermediate => HandJoint::RING_INTERMEDIATE, + HandBone::RingDistal => HandJoint::RING_DISTAL, + HandBone::RingTip => HandJoint::RING_TIP, + HandBone::LittleMetacarpal => HandJoint::LITTLE_METACARPAL, + HandBone::LittleProximal => HandJoint::LITTLE_PROXIMAL, + HandBone::LittleIntermediate => HandJoint::LITTLE_INTERMEDIATE, + HandBone::LittleDistal => HandJoint::LITTLE_DISTAL, + HandBone::LittleTip => HandJoint::LITTLE_TIP, } } From 2f06a01f15f86a9647c93056ab32136dae8b5a81 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Mon, 9 Oct 2023 21:04:20 -0400 Subject: [PATCH 11/14] create resource to help with entity selection --- examples/xr.rs | 80 +++++++++++++++++++++++++++++++++++----- src/xr_input/hand.rs | 88 ++++++++++++++++++++++++++++++++++++++++++-- src/xr_input/mod.rs | 1 + 3 files changed, 157 insertions(+), 12 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index d47b6f8..4fb94bb 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,13 +1,13 @@ use std::f32::consts::PI; - use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; -use bevy::transform::components::Transform; use bevy::prelude::*; +use bevy::transform::components::Transform; use bevy_openxr::input::XrInput; use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession}; +use bevy_openxr::xr_input::hand::HandsResource; use bevy_openxr::xr_input::hand_poses::*; use bevy_openxr::xr_input::oculus_touch::OculusController; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; @@ -119,17 +119,79 @@ pub fn spawn_hand_entities(mut commands: Commands) { HandBone::LittleDistal, HandBone::LittleTip, ]; - + //hand resource + let mut hand_resource = HandsResource { ..default() }; for hand in hands.iter() { for bone in bones.iter() { - commands.spawn(( - SpatialBundle::default(), - bone.clone(), - OpenXRTracker, - hand.clone(), - )); + let boneid = commands + .spawn(( + SpatialBundle::default(), + bone.clone(), + OpenXRTracker, + hand.clone(), + )) + .id(); + match hand { + Hand::Left => match bone { + HandBone::Palm => hand_resource.left.palm = boneid, + HandBone::Wrist => hand_resource.left.wrist = boneid, + HandBone::ThumbMetacarpal => hand_resource.left.thumb.metacarpal = boneid, + HandBone::ThumbProximal => hand_resource.left.thumb.proximal = boneid, + HandBone::ThumbDistal => hand_resource.left.thumb.distal = boneid, + HandBone::ThumbTip => hand_resource.left.thumb.tip = boneid, + HandBone::IndexMetacarpal => hand_resource.left.index.metacarpal = boneid, + HandBone::IndexProximal => hand_resource.left.index.proximal = boneid, + HandBone::IndexIntermediate => hand_resource.left.index.intermediate = boneid, + HandBone::IndexDistal => hand_resource.left.index.distal = boneid, + HandBone::IndexTip => hand_resource.left.index.tip = boneid, + HandBone::MiddleMetacarpal => hand_resource.left.middle.metacarpal = boneid, + HandBone::MiddleProximal => hand_resource.left.middle.proximal = boneid, + HandBone::MiddleIntermediate => hand_resource.left.middle.intermediate = boneid, + HandBone::MiddleDistal => hand_resource.left.middle.distal = boneid, + HandBone::MiddleTip => hand_resource.left.middle.tip = boneid, + HandBone::RingMetacarpal => hand_resource.left.ring.metacarpal = boneid, + HandBone::RingProximal => hand_resource.left.ring.proximal = boneid, + HandBone::RingIntermediate => hand_resource.left.ring.intermediate = boneid, + HandBone::RingDistal => hand_resource.left.ring.distal = boneid, + HandBone::RingTip => hand_resource.left.ring.tip = boneid, + HandBone::LittleMetacarpal => hand_resource.left.little.metacarpal = boneid, + HandBone::LittleProximal => hand_resource.left.little.proximal = boneid, + HandBone::LittleIntermediate => hand_resource.left.little.intermediate = boneid, + HandBone::LittleDistal => hand_resource.left.little.distal = boneid, + HandBone::LittleTip => hand_resource.left.little.tip = boneid, + }, + Hand::Right => match bone { + HandBone::Palm => hand_resource.right.palm = boneid, + HandBone::Wrist => hand_resource.right.wrist = boneid, + HandBone::ThumbMetacarpal => hand_resource.right.thumb.metacarpal = boneid, + HandBone::ThumbProximal => hand_resource.right.thumb.proximal = boneid, + HandBone::ThumbDistal => hand_resource.right.thumb.distal = boneid, + HandBone::ThumbTip => hand_resource.right.thumb.tip = boneid, + HandBone::IndexMetacarpal => hand_resource.right.index.metacarpal = boneid, + HandBone::IndexProximal => hand_resource.right.index.proximal = boneid, + HandBone::IndexIntermediate => hand_resource.right.index.intermediate = boneid, + HandBone::IndexDistal => hand_resource.right.index.distal = boneid, + HandBone::IndexTip => hand_resource.right.index.tip = boneid, + HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid, + HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid, + HandBone::MiddleIntermediate => hand_resource.right.middle.intermediate = boneid, + HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid, + HandBone::MiddleTip => hand_resource.right.middle.tip = boneid, + HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid, + HandBone::RingProximal => hand_resource.right.ring.proximal = boneid, + HandBone::RingIntermediate => hand_resource.right.ring.intermediate = boneid, + HandBone::RingDistal => hand_resource.right.ring.distal = boneid, + HandBone::RingTip => hand_resource.right.ring.tip = boneid, + HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid, + HandBone::LittleProximal => hand_resource.right.little.proximal = boneid, + HandBone::LittleIntermediate => hand_resource.right.little.intermediate = boneid, + HandBone::LittleDistal => hand_resource.right.little.distal = boneid, + HandBone::LittleTip => hand_resource.right.little.tip = boneid, + }, + } } } + commands.insert_resource(hand_resource); } pub fn update_hand_states( diff --git a/src/xr_input/hand.rs b/src/xr_input/hand.rs index 0fcd267..b84e0dc 100644 --- a/src/xr_input/hand.rs +++ b/src/xr_input/hand.rs @@ -1,3 +1,85 @@ -pub struct Hand { - pub HandJointPoses: [Posef; 26] -} \ No newline at end of file +use bevy::prelude::{Entity, Resource}; + +#[derive(Resource, Default)] +pub struct HandsResource { + pub left: HandResource, + pub right: HandResource, +} + +pub struct HandResource { + pub palm: Entity, + pub wrist: Entity, + pub thumb: ThumbResource, + pub index: IndexResource, + pub middle: MiddleResource, + pub ring: RingResource, + pub little: LittleResource, +} + +impl Default for HandResource { + fn default() -> Self { + Self { palm: Entity::PLACEHOLDER, wrist: Entity::PLACEHOLDER, thumb: Default::default(), index: Default::default(), middle: Default::default(), ring: Default::default(), little: Default::default() } + } +} + +pub struct ThumbResource { + pub metacarpal: Entity, + pub proximal: Entity, + pub distal: Entity, + pub tip: Entity, +} + +impl Default for ThumbResource { + fn default() -> Self { + Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + } +} +pub struct IndexResource { + pub metacarpal: Entity, + pub proximal: Entity, + pub intermediate: Entity, + pub distal: Entity, + pub tip: Entity, +} + +impl Default for IndexResource { + fn default() -> Self { + Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + } +} +pub struct MiddleResource { + pub metacarpal: Entity, + pub proximal: Entity, + pub intermediate: Entity, + pub distal: Entity, + pub tip: Entity, +} +impl Default for MiddleResource { + fn default() -> Self { + Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + } +} +pub struct RingResource { + pub metacarpal: Entity, + pub proximal: Entity, + pub intermediate: Entity, + pub distal: Entity, + pub tip: Entity, +} +impl Default for RingResource { + fn default() -> Self { + Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + } +} +pub struct LittleResource { + pub metacarpal: Entity, + pub proximal: Entity, + pub intermediate: Entity, + pub distal: Entity, + pub tip: Entity, +} +impl Default for LittleResource { + fn default() -> Self { + Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + } +} diff --git a/src/xr_input/mod.rs b/src/xr_input/mod.rs index a81badc..65eef19 100644 --- a/src/xr_input/mod.rs +++ b/src/xr_input/mod.rs @@ -5,6 +5,7 @@ pub mod prototype_locomotion; pub mod trackers; pub mod xr_camera; pub mod hand_poses; +pub mod hand; use crate::resources::XrSession; use crate::xr_begin_frame; From 398c65f1fd0f3f1f89220059768d913fee114e9b Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Tue, 10 Oct 2023 13:05:09 -0400 Subject: [PATCH 12/14] moved everything to the hand file --- examples/xr.rs | 909 +------------------------------------------ src/xr_input/hand.rs | 907 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 909 insertions(+), 907 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 4fb94bb..cbc7ac7 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -1,22 +1,16 @@ -use std::f32::consts::PI; + use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; -use bevy_openxr::input::XrInput; -use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession}; -use bevy_openxr::xr_input::hand::HandsResource; -use bevy_openxr::xr_input::hand_poses::*; -use bevy_openxr::xr_input::oculus_touch::OculusController; +use bevy_openxr::xr_input::hand::{spawn_hand_entities, update_hand_states, HandStatesResource, update_emulated_hand_skeletons, draw_hand_entities}; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, }; -use bevy_openxr::xr_input::{Hand, Vec3Conv}; use bevy_openxr::DefaultXrPlugins; -use openxr::{HandJoint, Posef}; fn main() { color_eyre::install().unwrap(); @@ -89,903 +83,6 @@ fn setup( },)); } -pub fn spawn_hand_entities(mut commands: Commands) { - let hands = [Hand::Left, Hand::Right]; - let bones = [ - HandBone::Palm, - HandBone::Wrist, - HandBone::ThumbMetacarpal, - HandBone::ThumbProximal, - HandBone::ThumbDistal, - HandBone::ThumbTip, - HandBone::IndexMetacarpal, - HandBone::IndexProximal, - HandBone::IndexIntermediate, - HandBone::IndexDistal, - HandBone::IndexTip, - HandBone::MiddleMetacarpal, - HandBone::MiddleProximal, - HandBone::MiddleIntermediate, - HandBone::MiddleDistal, - HandBone::MiddleTip, - HandBone::RingMetacarpal, - HandBone::RingProximal, - HandBone::RingIntermediate, - HandBone::RingDistal, - HandBone::RingTip, - HandBone::LittleMetacarpal, - HandBone::LittleProximal, - HandBone::LittleIntermediate, - HandBone::LittleDistal, - HandBone::LittleTip, - ]; - //hand resource - let mut hand_resource = HandsResource { ..default() }; - for hand in hands.iter() { - for bone in bones.iter() { - let boneid = commands - .spawn(( - SpatialBundle::default(), - bone.clone(), - OpenXRTracker, - hand.clone(), - )) - .id(); - match hand { - Hand::Left => match bone { - HandBone::Palm => hand_resource.left.palm = boneid, - HandBone::Wrist => hand_resource.left.wrist = boneid, - HandBone::ThumbMetacarpal => hand_resource.left.thumb.metacarpal = boneid, - HandBone::ThumbProximal => hand_resource.left.thumb.proximal = boneid, - HandBone::ThumbDistal => hand_resource.left.thumb.distal = boneid, - HandBone::ThumbTip => hand_resource.left.thumb.tip = boneid, - HandBone::IndexMetacarpal => hand_resource.left.index.metacarpal = boneid, - HandBone::IndexProximal => hand_resource.left.index.proximal = boneid, - HandBone::IndexIntermediate => hand_resource.left.index.intermediate = boneid, - HandBone::IndexDistal => hand_resource.left.index.distal = boneid, - HandBone::IndexTip => hand_resource.left.index.tip = boneid, - HandBone::MiddleMetacarpal => hand_resource.left.middle.metacarpal = boneid, - HandBone::MiddleProximal => hand_resource.left.middle.proximal = boneid, - HandBone::MiddleIntermediate => hand_resource.left.middle.intermediate = boneid, - HandBone::MiddleDistal => hand_resource.left.middle.distal = boneid, - HandBone::MiddleTip => hand_resource.left.middle.tip = boneid, - HandBone::RingMetacarpal => hand_resource.left.ring.metacarpal = boneid, - HandBone::RingProximal => hand_resource.left.ring.proximal = boneid, - HandBone::RingIntermediate => hand_resource.left.ring.intermediate = boneid, - HandBone::RingDistal => hand_resource.left.ring.distal = boneid, - HandBone::RingTip => hand_resource.left.ring.tip = boneid, - HandBone::LittleMetacarpal => hand_resource.left.little.metacarpal = boneid, - HandBone::LittleProximal => hand_resource.left.little.proximal = boneid, - HandBone::LittleIntermediate => hand_resource.left.little.intermediate = boneid, - HandBone::LittleDistal => hand_resource.left.little.distal = boneid, - HandBone::LittleTip => hand_resource.left.little.tip = boneid, - }, - Hand::Right => match bone { - HandBone::Palm => hand_resource.right.palm = boneid, - HandBone::Wrist => hand_resource.right.wrist = boneid, - HandBone::ThumbMetacarpal => hand_resource.right.thumb.metacarpal = boneid, - HandBone::ThumbProximal => hand_resource.right.thumb.proximal = boneid, - HandBone::ThumbDistal => hand_resource.right.thumb.distal = boneid, - HandBone::ThumbTip => hand_resource.right.thumb.tip = boneid, - HandBone::IndexMetacarpal => hand_resource.right.index.metacarpal = boneid, - HandBone::IndexProximal => hand_resource.right.index.proximal = boneid, - HandBone::IndexIntermediate => hand_resource.right.index.intermediate = boneid, - HandBone::IndexDistal => hand_resource.right.index.distal = boneid, - HandBone::IndexTip => hand_resource.right.index.tip = boneid, - HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid, - HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid, - HandBone::MiddleIntermediate => hand_resource.right.middle.intermediate = boneid, - HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid, - HandBone::MiddleTip => hand_resource.right.middle.tip = boneid, - HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid, - HandBone::RingProximal => hand_resource.right.ring.proximal = boneid, - HandBone::RingIntermediate => hand_resource.right.ring.intermediate = boneid, - HandBone::RingDistal => hand_resource.right.ring.distal = boneid, - HandBone::RingTip => hand_resource.right.ring.tip = boneid, - HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid, - HandBone::LittleProximal => hand_resource.right.little.proximal = boneid, - HandBone::LittleIntermediate => hand_resource.right.little.intermediate = boneid, - HandBone::LittleDistal => hand_resource.right.little.distal = boneid, - HandBone::LittleTip => hand_resource.right.little.tip = boneid, - }, - } - } - } - commands.insert_resource(hand_resource); -} - -pub fn update_hand_states( - oculus_controller: Res, - hand_states_option: Option>, - frame_state: Res, - xr_input: Res, - instance: Res, - session: Res, -) { - match hand_states_option { - Some(mut hands) => { - //lock frame - let frame_state = *frame_state.lock().unwrap(); - //get controller - let controller = - oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input); - - //right hand - let squeeze = controller.squeeze(Hand::Right); - let trigger_state = controller.trigger(Hand::Right); - let calc_trigger_state = match controller.trigger_touched(Hand::Right) { - true => match trigger_state > 0.0 { - true => TriggerState::PULLED, - false => TriggerState::TOUCHED, - }, - false => TriggerState::OFF, - }; - //button a - let mut a_state = ButtonState::OFF; - if controller.a_button_touched() { - a_state = ButtonState::TOUCHED; - } - if controller.a_button() { - a_state = ButtonState::PRESSED; - } - - //button b - let mut b_state = ButtonState::OFF; - if controller.b_button_touched() { - b_state = ButtonState::TOUCHED; - } - if controller.b_button() { - b_state = ButtonState::PRESSED; - } - - let thumbstick_state = controller.thumbstick(Hand::Right); - let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Right) { - true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { - true => ThumbstickState::PRESSED, - false => ThumbstickState::TOUCHED, - }, - false => ThumbstickState::OFF, - }; - - let right_state = HandState { - grip: squeeze, - trigger_state: calc_trigger_state, - a_button: a_state, - b_button: b_state, - thumbstick: calc_thumbstick_state, - }; - - //left - let squeeze = controller.squeeze(Hand::Left); - let trigger_state = controller.trigger(Hand::Left); - let calc_trigger_state = match controller.trigger_touched(Hand::Left) { - true => match trigger_state > 0.0 { - true => TriggerState::PULLED, - false => TriggerState::TOUCHED, - }, - false => TriggerState::OFF, - }; - //button a - let mut a_state = ButtonState::OFF; - if controller.x_button_touched() { - a_state = ButtonState::TOUCHED; - } - if controller.x_button() { - a_state = ButtonState::PRESSED; - } - - //button b - let mut b_state = ButtonState::OFF; - if controller.y_button_touched() { - b_state = ButtonState::TOUCHED; - } - if controller.y_button() { - b_state = ButtonState::PRESSED; - } - - let thumbstick_state = controller.thumbstick(Hand::Left); - let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Left) { - true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { - true => ThumbstickState::PRESSED, - false => ThumbstickState::TOUCHED, - }, - false => ThumbstickState::OFF, - }; - - let left_state = HandState { - grip: squeeze, - trigger_state: calc_trigger_state, - a_button: a_state, - b_button: b_state, - thumbstick: calc_thumbstick_state, - }; - - hands.left = left_state; - hands.right = right_state; - } - None => info!("hand states resource not init yet"), - } -} - -fn update_emulated_hand_skeletons( - right_controller_query: Query<(&GlobalTransform, With)>, - left_controller_query: Query<(&GlobalTransform, With)>, - hand_states_option: Option>, - mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>, -) { - match hand_states_option { - Some(hands) => { - let left_hand_transform = left_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - update_hand_bones_emulated( - left_hand_transform, - Hand::Left, - hands.left, - &mut hand_bone_query, - ); - let right_hand_transform = right_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - update_hand_bones_emulated( - right_hand_transform, - Hand::Right, - hands.right, - &mut hand_bone_query, - ); - } - None => info!("hand states resource not initialized yet"), - } -} - -#[derive(Component, Debug, Clone, Copy)] -pub enum HandBone { - 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(Clone, Copy)] -pub enum ButtonState { - OFF, - TOUCHED, - PRESSED, -} - -impl Default for ButtonState { - fn default() -> Self { - ButtonState::OFF - } -} -#[derive(Clone, Copy)] -pub enum ThumbstickState { - OFF, - TOUCHED, - PRESSED, -} - -impl Default for ThumbstickState { - fn default() -> Self { - ThumbstickState::OFF - } -} -#[derive(Clone, Copy)] -pub enum TriggerState { - OFF, - TOUCHED, - PULLED, -} - -impl Default for TriggerState { - fn default() -> Self { - TriggerState::OFF - } -} - -#[derive(Default, Resource)] -pub struct HandStatesResource { - pub left: HandState, - pub right: HandState, -} - -#[derive(Clone, Copy)] -pub struct HandState { - grip: f32, - trigger_state: TriggerState, - a_button: ButtonState, - b_button: ButtonState, - thumbstick: ThumbstickState, -} - -impl Default for HandState { - fn default() -> Self { - Self { - grip: Default::default(), - trigger_state: Default::default(), - a_button: Default::default(), - b_button: Default::default(), - thumbstick: Default::default(), - } - } -} - -impl HandState { - pub fn get_index_curl(&self) -> f32 { - match self.trigger_state { - TriggerState::OFF => 0.0, - TriggerState::TOUCHED => 0.50, - TriggerState::PULLED => 1.0, - } - } - - pub fn get_thumb_curl(&self) -> f32 { - match self.thumbstick { - ThumbstickState::OFF => (), - ThumbstickState::TOUCHED => return 0.25, - ThumbstickState::PRESSED => return 0.25, - }; - - match self.a_button { - ButtonState::OFF => (), - ButtonState::TOUCHED => return 0.25, - ButtonState::PRESSED => return 0.25, - }; - - match self.b_button { - ButtonState::OFF => (), - ButtonState::TOUCHED => return 0.25, - ButtonState::PRESSED => return 0.25, - }; - //if no thumb actions taken return open position - return 0.0; - } -} - -fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) { - for (transform, hand_bone) in query.iter() { - let (radius, color) = get_bone_gizmo_style(hand_bone); - gizmos.sphere(transform.translation, transform.rotation, radius, color); - } -} - -fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { - match hand_bone { - HandBone::Palm => (0.01, Color::WHITE), - HandBone::Wrist => (0.01, Color::GRAY), - HandBone::ThumbMetacarpal => (0.01, Color::RED), - HandBone::ThumbProximal => (0.008, Color::RED), - HandBone::ThumbDistal => (0.006, Color::RED), - HandBone::ThumbTip => (0.004, Color::RED), - HandBone::IndexMetacarpal => (0.01, Color::ORANGE), - HandBone::IndexProximal => (0.008, Color::ORANGE), - HandBone::IndexIntermediate => (0.006, Color::ORANGE), - HandBone::IndexDistal => (0.004, Color::ORANGE), - HandBone::IndexTip => (0.002, Color::ORANGE), - HandBone::MiddleMetacarpal => (0.01, Color::YELLOW), - HandBone::MiddleProximal => (0.008, Color::YELLOW), - HandBone::MiddleIntermediate => (0.006, Color::YELLOW), - HandBone::MiddleDistal => (0.004, Color::YELLOW), - HandBone::MiddleTip => (0.002, Color::YELLOW), - HandBone::RingMetacarpal => (0.01, Color::GREEN), - HandBone::RingProximal => (0.008, Color::GREEN), - HandBone::RingIntermediate => (0.006, Color::GREEN), - HandBone::RingDistal => (0.004, Color::GREEN), - HandBone::RingTip => (0.002, Color::GREEN), - HandBone::LittleMetacarpal => (0.01, Color::BLUE), - HandBone::LittleProximal => (0.008, Color::BLUE), - HandBone::LittleIntermediate => (0.006, Color::BLUE), - HandBone::LittleDistal => (0.004, Color::BLUE), - HandBone::LittleTip => (0.002, Color::BLUE), - } -} - -fn update_hand_bones_emulated( - controller_transform: Transform, - hand: Hand, - hand_state: HandState, - hand_bone_query: &mut Query<(&mut Transform, &HandBone, &Hand)>, -) { - let left_hand_rot = Quat::from_rotation_y(180.0 * PI / 180.0); - let hand_translation: Vec3 = match hand { - Hand::Left => controller_transform.translation, - Hand::Right => controller_transform.translation, - }; - - let controller_quat: Quat = match hand { - Hand::Left => controller_transform.rotation.mul_quat(left_hand_rot), - Hand::Right => controller_transform.rotation, - }; - - let splay_direction = match hand { - Hand::Left => -1.0, - Hand::Right => 1.0, - }; - //lets make a structure to hold our calculated transforms for now - let mut calc_transforms = [Transform::default(); 26]; - - //curl represents how closed the hand is from 0 to 1; - let grip_curl = hand_state.grip; - let index_curl = hand_state.get_index_curl(); - let thumb_curl = hand_state.get_thumb_curl(); - //get palm quat - let y = Quat::from_rotation_y(-90.0 * PI / 180.0); - let x = Quat::from_rotation_x(-90.0 * PI / 180.0); - let palm_quat = controller_quat.mul_quat(y).mul_quat(x); - //get simulated bones - let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); - //palm - let palm = hand_transform_array[HandJoint::PALM]; - calc_transforms[HandJoint::PALM] = Transform { - translation: hand_translation + palm.translation, - ..default() - }; - //wrist - let wrist = hand_transform_array[HandJoint::WRIST]; - calc_transforms[HandJoint::WRIST] = Transform { - translation: hand_translation + palm.translation + palm_quat.mul_vec3(wrist.translation), - ..default() - }; - - //thumb - let thumb_joints = [ - HandJoint::THUMB_METACARPAL, - HandJoint::THUMB_PROXIMAL, - HandJoint::THUMB_DISTAL, - HandJoint::THUMB_TIP, - ]; - let mut prior_start: Option = None; - let mut prior_quat: Option = None; - let mut prior_vector: Option = None; - let splay = Quat::from_rotation_y(splay_direction * 30.0 * PI / 180.0); - let huh = Quat::from_rotation_x(-35.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(huh).mul_quat(splay); - for bone in thumb_joints.iter() { - match prior_start { - Some(start) => { - let curl_angle: f32 = get_bone_curl_angle(*bone, thumb_curl); - let tp_lrot = Quat::from_rotation_y(splay_direction * curl_angle * PI / 180.0); - let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); - let thumb_prox = hand_transform_array[*bone]; - let tp_start = start + prior_vector.unwrap(); - let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - prior_start = Some(tp_start); - prior_quat = Some(tp_quat); - prior_vector = Some(tp_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tp_start + tp_vector, - ..default() - }; - } - None => { - let thumb_meta = hand_transform_array[*bone]; - let tm_start = hand_translation - + palm_quat.mul_vec3(palm.translation) - + palm_quat.mul_vec3(wrist.translation); - let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - prior_start = Some(tm_start); - prior_quat = Some(splay_quat); - prior_vector = Some(tm_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tm_start + tm_vector, - ..default() - }; - } - } - } - - //index - let thumb_joints = [ - HandJoint::INDEX_METACARPAL, - HandJoint::INDEX_PROXIMAL, - HandJoint::INDEX_INTERMEDIATE, - HandJoint::INDEX_DISTAL, - HandJoint::INDEX_TIP, - ]; - let mut prior_start: Option = None; - let mut prior_quat: Option = None; - let mut prior_vector: Option = None; - let splay = Quat::from_rotation_y(splay_direction * 10.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(splay); - for bone in thumb_joints.iter() { - match prior_start { - Some(start) => { - let curl_angle: f32 = get_bone_curl_angle(*bone, index_curl); - let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); - let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); - let thumb_prox = hand_transform_array[*bone]; - let tp_start = start + prior_vector.unwrap(); - let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - prior_start = Some(tp_start); - prior_quat = Some(tp_quat); - prior_vector = Some(tp_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tp_start + tp_vector, - ..default() - }; - } - None => { - let thumb_meta = hand_transform_array[*bone]; - let tm_start = hand_translation - + palm_quat.mul_vec3(palm.translation) - + palm_quat.mul_vec3(wrist.translation); - let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - prior_start = Some(tm_start); - prior_quat = Some(splay_quat); - prior_vector = Some(tm_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tm_start + tm_vector, - ..default() - }; - } - } - } - - //middle - let thumb_joints = [ - HandJoint::MIDDLE_METACARPAL, - HandJoint::MIDDLE_PROXIMAL, - HandJoint::MIDDLE_INTERMEDIATE, - HandJoint::MIDDLE_DISTAL, - HandJoint::MIDDLE_TIP, - ]; - let mut prior_start: Option = None; - let mut prior_quat: Option = None; - let mut prior_vector: Option = None; - let splay = Quat::from_rotation_y(splay_direction * 0.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(splay); - for bone in thumb_joints.iter() { - match prior_start { - Some(start) => { - let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); - let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); - let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); - let thumb_prox = hand_transform_array[*bone]; - let tp_start = start + prior_vector.unwrap(); - let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - prior_start = Some(tp_start); - prior_quat = Some(tp_quat); - prior_vector = Some(tp_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tp_start + tp_vector, - ..default() - }; - } - None => { - let thumb_meta = hand_transform_array[*bone]; - let tm_start = hand_translation - + palm_quat.mul_vec3(palm.translation) - + palm_quat.mul_vec3(wrist.translation); - let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - prior_start = Some(tm_start); - prior_quat = Some(splay_quat); - prior_vector = Some(tm_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tm_start + tm_vector, - ..default() - }; - } - } - } - //ring - let thumb_joints = [ - HandJoint::RING_METACARPAL, - HandJoint::RING_PROXIMAL, - HandJoint::RING_INTERMEDIATE, - HandJoint::RING_DISTAL, - HandJoint::RING_TIP, - ]; - let mut prior_start: Option = None; - let mut prior_quat: Option = None; - let mut prior_vector: Option = None; - let splay = Quat::from_rotation_y(splay_direction * -10.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(splay); - for bone in thumb_joints.iter() { - match prior_start { - Some(start) => { - let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); - let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); - let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); - let thumb_prox = hand_transform_array[*bone]; - let tp_start = start + prior_vector.unwrap(); - let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - prior_start = Some(tp_start); - prior_quat = Some(tp_quat); - prior_vector = Some(tp_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tp_start + tp_vector, - ..default() - }; - } - None => { - let thumb_meta = hand_transform_array[*bone]; - let tm_start = hand_translation - + palm_quat.mul_vec3(palm.translation) - + palm_quat.mul_vec3(wrist.translation); - let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - prior_start = Some(tm_start); - prior_quat = Some(splay_quat); - prior_vector = Some(tm_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tm_start + tm_vector, - ..default() - }; - } - } - } - - //little - let thumb_joints = [ - HandJoint::LITTLE_METACARPAL, - HandJoint::LITTLE_PROXIMAL, - HandJoint::LITTLE_INTERMEDIATE, - HandJoint::LITTLE_DISTAL, - HandJoint::LITTLE_TIP, - ]; - let mut prior_start: Option = None; - let mut prior_quat: Option = None; - let mut prior_vector: Option = None; - let splay = Quat::from_rotation_y(splay_direction * -20.0 * PI / 180.0); - let splay_quat = palm_quat.mul_quat(splay); - for bone in thumb_joints.iter() { - match prior_start { - Some(start) => { - let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); - let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); - let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); - let thumb_prox = hand_transform_array[*bone]; - let tp_start = start + prior_vector.unwrap(); - let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); - prior_start = Some(tp_start); - prior_quat = Some(tp_quat); - prior_vector = Some(tp_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tp_start + tp_vector, - ..default() - }; - } - None => { - let thumb_meta = hand_transform_array[*bone]; - let tm_start = hand_translation - + palm_quat.mul_vec3(palm.translation) - + palm_quat.mul_vec3(wrist.translation); - let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); - prior_start = Some(tm_start); - prior_quat = Some(splay_quat); - prior_vector = Some(tm_vector); - //store it - calc_transforms[*bone] = Transform { - translation: tm_start + tm_vector, - ..default() - }; - } - } - } - - //now that we have all the transforms lets assign them - for (mut transform, handbone, bonehand) in hand_bone_query.iter_mut() { - if *bonehand == hand { - //if the hands match lets go - let index = match_index(handbone); - *transform = calc_transforms[index]; - } - } -} - -fn match_index(handbone: &HandBone) -> HandJoint { - match handbone { - HandBone::Palm => HandJoint::PALM, - HandBone::Wrist => HandJoint::WRIST, - HandBone::ThumbMetacarpal => HandJoint::THUMB_METACARPAL, - HandBone::ThumbProximal => HandJoint::THUMB_PROXIMAL, - HandBone::ThumbDistal => HandJoint::THUMB_DISTAL, - HandBone::ThumbTip => HandJoint::THUMB_TIP, - HandBone::IndexMetacarpal => HandJoint::INDEX_METACARPAL, - HandBone::IndexProximal => HandJoint::INDEX_PROXIMAL, - HandBone::IndexIntermediate => HandJoint::INDEX_INTERMEDIATE, - HandBone::IndexDistal => HandJoint::INDEX_DISTAL, - HandBone::IndexTip => HandJoint::INDEX_TIP, - HandBone::MiddleMetacarpal => HandJoint::MIDDLE_METACARPAL, - HandBone::MiddleProximal => HandJoint::MIDDLE_PROXIMAL, - HandBone::MiddleIntermediate => HandJoint::MIDDLE_INTERMEDIATE, - HandBone::MiddleDistal => HandJoint::MIDDLE_DISTAL, - HandBone::MiddleTip => HandJoint::MIDDLE_TIP, - HandBone::RingMetacarpal => HandJoint::RING_METACARPAL, - HandBone::RingProximal => HandJoint::RING_PROXIMAL, - HandBone::RingIntermediate => HandJoint::RING_INTERMEDIATE, - HandBone::RingDistal => HandJoint::RING_DISTAL, - HandBone::RingTip => HandJoint::RING_TIP, - HandBone::LittleMetacarpal => HandJoint::LITTLE_METACARPAL, - HandBone::LittleProximal => HandJoint::LITTLE_PROXIMAL, - HandBone::LittleIntermediate => HandJoint::LITTLE_INTERMEDIATE, - HandBone::LittleDistal => HandJoint::LITTLE_DISTAL, - HandBone::LittleTip => HandJoint::LITTLE_TIP, - } -} - -fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { - let mul: f32 = match bone { - HandJoint::INDEX_PROXIMAL => 0.0, - HandJoint::MIDDLE_PROXIMAL => 0.0, - HandJoint::RING_PROXIMAL => 0.0, - HandJoint::LITTLE_PROXIMAL => 0.0, - HandJoint::THUMB_PROXIMAL => 0.0, - _ => 1.0, - }; - let curl_angle = -((mul * curl * 80.0) + 5.0); - return curl_angle; -} - -fn log_hand(hand_pose: [Posef; 26]) { - let _palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() - - hand_pose[HandJoint::PALM].position.to_vec3(); - info!( - "palm-wrist: {}", - hand_pose[HandJoint::WRIST].position.to_vec3() - - hand_pose[HandJoint::PALM].position.to_vec3() - ); - - info!( - "wrist-tm: {}", - hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() - - hand_pose[HandJoint::WRIST].position.to_vec3() - ); - info!( - "tm-tp: {}", - hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() - - hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() - ); - info!( - "tp-td: {}", - hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() - - hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() - ); - info!( - "td-tt: {}", - hand_pose[HandJoint::THUMB_TIP].position.to_vec3() - - hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() - ); - - info!( - "wrist-im: {}", - hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() - - hand_pose[HandJoint::WRIST].position.to_vec3() - ); - info!( - "im-ip: {}", - hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() - - hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() - ); - info!( - "ip-ii: {}", - hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() - - hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() - ); - info!( - "ii-id: {}", - hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() - - hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() - ); - info!( - "id-it: {}", - hand_pose[HandJoint::INDEX_TIP].position.to_vec3() - - hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() - ); - - info!( - "wrist-mm: {}", - hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() - - hand_pose[HandJoint::WRIST].position.to_vec3() - ); - info!( - "mm-mp: {}", - hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() - - hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() - ); - info!( - "mp-mi: {}", - hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() - - hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() - ); - info!( - "mi-md: {}", - hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() - - hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() - ); - info!( - "md-mt: {}", - hand_pose[HandJoint::MIDDLE_TIP].position.to_vec3() - - hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() - ); - - info!( - "wrist-rm: {}", - hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() - - hand_pose[HandJoint::WRIST].position.to_vec3() - ); - info!( - "rm-rp: {}", - hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() - - hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() - ); - info!( - "rp-ri: {}", - hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() - - hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() - ); - info!( - "ri-rd: {}", - hand_pose[HandJoint::RING_DISTAL].position.to_vec3() - - hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() - ); - info!( - "rd-rt: {}", - hand_pose[HandJoint::RING_TIP].position.to_vec3() - - hand_pose[HandJoint::RING_DISTAL].position.to_vec3() - ); - - info!( - "wrist-lm: {}", - hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() - - hand_pose[HandJoint::WRIST].position.to_vec3() - ); - info!( - "lm-lp: {}", - hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() - - hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() - ); - info!( - "lp-li: {}", - hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() - - hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() - ); - info!( - "li-ld: {}", - hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() - - hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() - ); - info!( - "ld-lt: {}", - hand_pose[HandJoint::LITTLE_TIP].position.to_vec3() - - hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() - ); -} - fn spawn_controllers_example(mut commands: Commands) { //left hand commands.spawn(( @@ -1001,4 +98,4 @@ fn spawn_controllers_example(mut commands: Commands) { OpenXRTracker, SpatialBundle::default(), )); -} +} \ No newline at end of file diff --git a/src/xr_input/hand.rs b/src/xr_input/hand.rs index b84e0dc..f02b5c4 100644 --- a/src/xr_input/hand.rs +++ b/src/xr_input/hand.rs @@ -1,4 +1,11 @@ -use bevy::prelude::{Entity, Resource}; +use std::f32::consts::PI; + +use bevy::prelude::{Entity, Resource, Component, default, SpatialBundle, Commands, Res, ResMut, info, Transform, Query, Quat, Vec3, GlobalTransform, With, Gizmos, Color}; +use openxr::{HandJoint, Posef}; + +use crate::{resources::{XrFrameState, XrInstance, XrSession}, input::XrInput, xr_input::Vec3Conv}; + +use super::{trackers::{OpenXRTracker, OpenXRRightController, OpenXRLeftController}, Hand, oculus_touch::OculusController, hand_poses::get_simulated_open_hand_transforms}; #[derive(Resource, Default)] pub struct HandsResource { @@ -83,3 +90,901 @@ impl Default for LittleResource { Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } } } + +pub fn spawn_hand_entities(mut commands: Commands) { + let hands = [Hand::Left, Hand::Right]; + let bones = [ + HandBone::Palm, + HandBone::Wrist, + HandBone::ThumbMetacarpal, + HandBone::ThumbProximal, + HandBone::ThumbDistal, + HandBone::ThumbTip, + HandBone::IndexMetacarpal, + HandBone::IndexProximal, + HandBone::IndexIntermediate, + HandBone::IndexDistal, + HandBone::IndexTip, + HandBone::MiddleMetacarpal, + HandBone::MiddleProximal, + HandBone::MiddleIntermediate, + HandBone::MiddleDistal, + HandBone::MiddleTip, + HandBone::RingMetacarpal, + HandBone::RingProximal, + HandBone::RingIntermediate, + HandBone::RingDistal, + HandBone::RingTip, + HandBone::LittleMetacarpal, + HandBone::LittleProximal, + HandBone::LittleIntermediate, + HandBone::LittleDistal, + HandBone::LittleTip, + ]; + //hand resource + let mut hand_resource = HandsResource { ..default() }; + for hand in hands.iter() { + for bone in bones.iter() { + let boneid = commands + .spawn(( + SpatialBundle::default(), + bone.clone(), + OpenXRTracker, + hand.clone(), + )) + .id(); + match hand { + Hand::Left => match bone { + HandBone::Palm => hand_resource.left.palm = boneid, + HandBone::Wrist => hand_resource.left.wrist = boneid, + HandBone::ThumbMetacarpal => hand_resource.left.thumb.metacarpal = boneid, + HandBone::ThumbProximal => hand_resource.left.thumb.proximal = boneid, + HandBone::ThumbDistal => hand_resource.left.thumb.distal = boneid, + HandBone::ThumbTip => hand_resource.left.thumb.tip = boneid, + HandBone::IndexMetacarpal => hand_resource.left.index.metacarpal = boneid, + HandBone::IndexProximal => hand_resource.left.index.proximal = boneid, + HandBone::IndexIntermediate => hand_resource.left.index.intermediate = boneid, + HandBone::IndexDistal => hand_resource.left.index.distal = boneid, + HandBone::IndexTip => hand_resource.left.index.tip = boneid, + HandBone::MiddleMetacarpal => hand_resource.left.middle.metacarpal = boneid, + HandBone::MiddleProximal => hand_resource.left.middle.proximal = boneid, + HandBone::MiddleIntermediate => hand_resource.left.middle.intermediate = boneid, + HandBone::MiddleDistal => hand_resource.left.middle.distal = boneid, + HandBone::MiddleTip => hand_resource.left.middle.tip = boneid, + HandBone::RingMetacarpal => hand_resource.left.ring.metacarpal = boneid, + HandBone::RingProximal => hand_resource.left.ring.proximal = boneid, + HandBone::RingIntermediate => hand_resource.left.ring.intermediate = boneid, + HandBone::RingDistal => hand_resource.left.ring.distal = boneid, + HandBone::RingTip => hand_resource.left.ring.tip = boneid, + HandBone::LittleMetacarpal => hand_resource.left.little.metacarpal = boneid, + HandBone::LittleProximal => hand_resource.left.little.proximal = boneid, + HandBone::LittleIntermediate => hand_resource.left.little.intermediate = boneid, + HandBone::LittleDistal => hand_resource.left.little.distal = boneid, + HandBone::LittleTip => hand_resource.left.little.tip = boneid, + }, + Hand::Right => match bone { + HandBone::Palm => hand_resource.right.palm = boneid, + HandBone::Wrist => hand_resource.right.wrist = boneid, + HandBone::ThumbMetacarpal => hand_resource.right.thumb.metacarpal = boneid, + HandBone::ThumbProximal => hand_resource.right.thumb.proximal = boneid, + HandBone::ThumbDistal => hand_resource.right.thumb.distal = boneid, + HandBone::ThumbTip => hand_resource.right.thumb.tip = boneid, + HandBone::IndexMetacarpal => hand_resource.right.index.metacarpal = boneid, + HandBone::IndexProximal => hand_resource.right.index.proximal = boneid, + HandBone::IndexIntermediate => hand_resource.right.index.intermediate = boneid, + HandBone::IndexDistal => hand_resource.right.index.distal = boneid, + HandBone::IndexTip => hand_resource.right.index.tip = boneid, + HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid, + HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid, + HandBone::MiddleIntermediate => hand_resource.right.middle.intermediate = boneid, + HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid, + HandBone::MiddleTip => hand_resource.right.middle.tip = boneid, + HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid, + HandBone::RingProximal => hand_resource.right.ring.proximal = boneid, + HandBone::RingIntermediate => hand_resource.right.ring.intermediate = boneid, + HandBone::RingDistal => hand_resource.right.ring.distal = boneid, + HandBone::RingTip => hand_resource.right.ring.tip = boneid, + HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid, + HandBone::LittleProximal => hand_resource.right.little.proximal = boneid, + HandBone::LittleIntermediate => hand_resource.right.little.intermediate = boneid, + HandBone::LittleDistal => hand_resource.right.little.distal = boneid, + HandBone::LittleTip => hand_resource.right.little.tip = boneid, + }, + } + } + } + commands.insert_resource(hand_resource); +} + +#[derive(Component, Debug, Clone, Copy)] +pub enum HandBone { + 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, +} + +pub fn update_hand_states( + oculus_controller: Res, + hand_states_option: Option>, + frame_state: Res, + xr_input: Res, + instance: Res, + session: Res, +) { + match hand_states_option { + Some(mut hands) => { + //lock frame + let frame_state = *frame_state.lock().unwrap(); + //get controller + let controller = + oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input); + + //right hand + let squeeze = controller.squeeze(Hand::Right); + let trigger_state = controller.trigger(Hand::Right); + let calc_trigger_state = match controller.trigger_touched(Hand::Right) { + true => match trigger_state > 0.0 { + true => TriggerState::PULLED, + false => TriggerState::TOUCHED, + }, + false => TriggerState::OFF, + }; + //button a + let mut a_state = ButtonState::OFF; + if controller.a_button_touched() { + a_state = ButtonState::TOUCHED; + } + if controller.a_button() { + a_state = ButtonState::PRESSED; + } + + //button b + let mut b_state = ButtonState::OFF; + if controller.b_button_touched() { + b_state = ButtonState::TOUCHED; + } + if controller.b_button() { + b_state = ButtonState::PRESSED; + } + + let thumbstick_state = controller.thumbstick(Hand::Right); + let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Right) { + true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { + true => ThumbstickState::PRESSED, + false => ThumbstickState::TOUCHED, + }, + false => ThumbstickState::OFF, + }; + + let right_state = HandState { + grip: squeeze, + trigger_state: calc_trigger_state, + a_button: a_state, + b_button: b_state, + thumbstick: calc_thumbstick_state, + }; + + //left + let squeeze = controller.squeeze(Hand::Left); + let trigger_state = controller.trigger(Hand::Left); + let calc_trigger_state = match controller.trigger_touched(Hand::Left) { + true => match trigger_state > 0.0 { + true => TriggerState::PULLED, + false => TriggerState::TOUCHED, + }, + false => TriggerState::OFF, + }; + //button a + let mut a_state = ButtonState::OFF; + if controller.x_button_touched() { + a_state = ButtonState::TOUCHED; + } + if controller.x_button() { + a_state = ButtonState::PRESSED; + } + + //button b + let mut b_state = ButtonState::OFF; + if controller.y_button_touched() { + b_state = ButtonState::TOUCHED; + } + if controller.y_button() { + b_state = ButtonState::PRESSED; + } + + let thumbstick_state = controller.thumbstick(Hand::Left); + let calc_thumbstick_state = match controller.thumbstick_touch(Hand::Left) { + true => match thumbstick_state.x > 0.0 || thumbstick_state.y > 0.0 { + true => ThumbstickState::PRESSED, + false => ThumbstickState::TOUCHED, + }, + false => ThumbstickState::OFF, + }; + + let left_state = HandState { + grip: squeeze, + trigger_state: calc_trigger_state, + a_button: a_state, + b_button: b_state, + thumbstick: calc_thumbstick_state, + }; + + hands.left = left_state; + hands.right = right_state; + } + None => info!("hand states resource not init yet"), + } +} + +#[derive(Clone, Copy)] +pub enum ButtonState { + OFF, + TOUCHED, + PRESSED, +} + +impl Default for ButtonState { + fn default() -> Self { + ButtonState::OFF + } +} +#[derive(Clone, Copy)] +pub enum ThumbstickState { + OFF, + TOUCHED, + PRESSED, +} + +impl Default for ThumbstickState { + fn default() -> Self { + ThumbstickState::OFF + } +} +#[derive(Clone, Copy)] +pub enum TriggerState { + OFF, + TOUCHED, + PULLED, +} + +impl Default for TriggerState { + fn default() -> Self { + TriggerState::OFF + } +} + +#[derive(Default, Resource)] +pub struct HandStatesResource { + pub left: HandState, + pub right: HandState, +} + +#[derive(Clone, Copy)] +pub struct HandState { + pub grip: f32, + pub trigger_state: TriggerState, + pub a_button: ButtonState, + pub b_button: ButtonState, + pub thumbstick: ThumbstickState, +} + +impl HandState { + pub fn get_index_curl(&self) -> f32 { + match self.trigger_state { + TriggerState::OFF => 0.0, + TriggerState::TOUCHED => 0.50, + TriggerState::PULLED => 1.0, + } + } + + pub fn get_thumb_curl(&self) -> f32 { + match self.thumbstick { + ThumbstickState::OFF => (), + ThumbstickState::TOUCHED => return 0.25, + ThumbstickState::PRESSED => return 0.25, + }; + + match self.a_button { + ButtonState::OFF => (), + ButtonState::TOUCHED => return 0.25, + ButtonState::PRESSED => return 0.25, + }; + + match self.b_button { + ButtonState::OFF => (), + ButtonState::TOUCHED => return 0.25, + ButtonState::PRESSED => return 0.25, + }; + //if no thumb actions taken return open position + return 0.0; + } +} + +impl Default for HandState { + fn default() -> Self { + Self { + grip: Default::default(), + trigger_state: Default::default(), + a_button: Default::default(), + b_button: Default::default(), + thumbstick: Default::default(), + } + } +} + +pub fn update_hand_bones_emulated( + controller_transform: Transform, + hand: Hand, + hand_state: HandState, + hand_bone_query: &mut Query<(&mut Transform, &HandBone, &Hand)>, +) { + let left_hand_rot = Quat::from_rotation_y(180.0 * PI / 180.0); + let hand_translation: Vec3 = match hand { + Hand::Left => controller_transform.translation, + Hand::Right => controller_transform.translation, + }; + + let controller_quat: Quat = match hand { + Hand::Left => controller_transform.rotation.mul_quat(left_hand_rot), + Hand::Right => controller_transform.rotation, + }; + + let splay_direction = match hand { + Hand::Left => -1.0, + Hand::Right => 1.0, + }; + //lets make a structure to hold our calculated transforms for now + let mut calc_transforms = [Transform::default(); 26]; + + //curl represents how closed the hand is from 0 to 1; + let grip_curl = hand_state.grip; + let index_curl = hand_state.get_index_curl(); + let thumb_curl = hand_state.get_thumb_curl(); + //get palm quat + let y = Quat::from_rotation_y(-90.0 * PI / 180.0); + let x = Quat::from_rotation_x(-90.0 * PI / 180.0); + let palm_quat = controller_quat.mul_quat(y).mul_quat(x); + //get simulated bones + let hand_transform_array: [Transform; 26] = get_simulated_open_hand_transforms(hand); + //palm + let palm = hand_transform_array[HandJoint::PALM]; + calc_transforms[HandJoint::PALM] = Transform { + translation: hand_translation + palm.translation, + ..default() + }; + //wrist + let wrist = hand_transform_array[HandJoint::WRIST]; + calc_transforms[HandJoint::WRIST] = Transform { + translation: hand_translation + palm.translation + palm_quat.mul_vec3(wrist.translation), + ..default() + }; + + //thumb + let thumb_joints = [ + HandJoint::THUMB_METACARPAL, + HandJoint::THUMB_PROXIMAL, + HandJoint::THUMB_DISTAL, + HandJoint::THUMB_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let splay = Quat::from_rotation_y(splay_direction * 30.0 * PI / 180.0); + let huh = Quat::from_rotation_x(-35.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(huh).mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let curl_angle: f32 = get_bone_curl_angle(*bone, thumb_curl); + let tp_lrot = Quat::from_rotation_y(splay_direction * curl_angle * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; + } + } + } + + //index + let thumb_joints = [ + HandJoint::INDEX_METACARPAL, + HandJoint::INDEX_PROXIMAL, + HandJoint::INDEX_INTERMEDIATE, + HandJoint::INDEX_DISTAL, + HandJoint::INDEX_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let splay = Quat::from_rotation_y(splay_direction * 10.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let curl_angle: f32 = get_bone_curl_angle(*bone, index_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; + } + } + } + + //middle + let thumb_joints = [ + HandJoint::MIDDLE_METACARPAL, + HandJoint::MIDDLE_PROXIMAL, + HandJoint::MIDDLE_INTERMEDIATE, + HandJoint::MIDDLE_DISTAL, + HandJoint::MIDDLE_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let splay = Quat::from_rotation_y(splay_direction * 0.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; + } + } + } + //ring + let thumb_joints = [ + HandJoint::RING_METACARPAL, + HandJoint::RING_PROXIMAL, + HandJoint::RING_INTERMEDIATE, + HandJoint::RING_DISTAL, + HandJoint::RING_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let splay = Quat::from_rotation_y(splay_direction * -10.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; + } + } + } + + //little + let thumb_joints = [ + HandJoint::LITTLE_METACARPAL, + HandJoint::LITTLE_PROXIMAL, + HandJoint::LITTLE_INTERMEDIATE, + HandJoint::LITTLE_DISTAL, + HandJoint::LITTLE_TIP, + ]; + let mut prior_start: Option = None; + let mut prior_quat: Option = None; + let mut prior_vector: Option = None; + let splay = Quat::from_rotation_y(splay_direction * -20.0 * PI / 180.0); + let splay_quat = palm_quat.mul_quat(splay); + for bone in thumb_joints.iter() { + match prior_start { + Some(start) => { + let curl_angle: f32 = get_bone_curl_angle(*bone, grip_curl); + let tp_lrot = Quat::from_rotation_x(curl_angle * PI / 180.0); + let tp_quat = prior_quat.unwrap().mul_quat(tp_lrot); + let thumb_prox = hand_transform_array[*bone]; + let tp_start = start + prior_vector.unwrap(); + let tp_vector = tp_quat.mul_vec3(thumb_prox.translation); + prior_start = Some(tp_start); + prior_quat = Some(tp_quat); + prior_vector = Some(tp_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tp_start + tp_vector, + ..default() + }; + } + None => { + let thumb_meta = hand_transform_array[*bone]; + let tm_start = hand_translation + + palm_quat.mul_vec3(palm.translation) + + palm_quat.mul_vec3(wrist.translation); + let tm_vector = palm_quat.mul_vec3(thumb_meta.translation); + prior_start = Some(tm_start); + prior_quat = Some(splay_quat); + prior_vector = Some(tm_vector); + //store it + calc_transforms[*bone] = Transform { + translation: tm_start + tm_vector, + ..default() + }; + } + } + } + + //now that we have all the transforms lets assign them + for (mut transform, handbone, bonehand) in hand_bone_query.iter_mut() { + if *bonehand == hand { + //if the hands match lets go + let index = match_index(handbone); + *transform = calc_transforms[index]; + } + } +} + +fn match_index(handbone: &HandBone) -> HandJoint { + match handbone { + HandBone::Palm => HandJoint::PALM, + HandBone::Wrist => HandJoint::WRIST, + HandBone::ThumbMetacarpal => HandJoint::THUMB_METACARPAL, + HandBone::ThumbProximal => HandJoint::THUMB_PROXIMAL, + HandBone::ThumbDistal => HandJoint::THUMB_DISTAL, + HandBone::ThumbTip => HandJoint::THUMB_TIP, + HandBone::IndexMetacarpal => HandJoint::INDEX_METACARPAL, + HandBone::IndexProximal => HandJoint::INDEX_PROXIMAL, + HandBone::IndexIntermediate => HandJoint::INDEX_INTERMEDIATE, + HandBone::IndexDistal => HandJoint::INDEX_DISTAL, + HandBone::IndexTip => HandJoint::INDEX_TIP, + HandBone::MiddleMetacarpal => HandJoint::MIDDLE_METACARPAL, + HandBone::MiddleProximal => HandJoint::MIDDLE_PROXIMAL, + HandBone::MiddleIntermediate => HandJoint::MIDDLE_INTERMEDIATE, + HandBone::MiddleDistal => HandJoint::MIDDLE_DISTAL, + HandBone::MiddleTip => HandJoint::MIDDLE_TIP, + HandBone::RingMetacarpal => HandJoint::RING_METACARPAL, + HandBone::RingProximal => HandJoint::RING_PROXIMAL, + HandBone::RingIntermediate => HandJoint::RING_INTERMEDIATE, + HandBone::RingDistal => HandJoint::RING_DISTAL, + HandBone::RingTip => HandJoint::RING_TIP, + HandBone::LittleMetacarpal => HandJoint::LITTLE_METACARPAL, + HandBone::LittleProximal => HandJoint::LITTLE_PROXIMAL, + HandBone::LittleIntermediate => HandJoint::LITTLE_INTERMEDIATE, + HandBone::LittleDistal => HandJoint::LITTLE_DISTAL, + HandBone::LittleTip => HandJoint::LITTLE_TIP, + } +} + +fn get_bone_curl_angle(bone: HandJoint, curl: f32) -> f32 { + let mul: f32 = match bone { + HandJoint::INDEX_PROXIMAL => 0.0, + HandJoint::MIDDLE_PROXIMAL => 0.0, + HandJoint::RING_PROXIMAL => 0.0, + HandJoint::LITTLE_PROXIMAL => 0.0, + HandJoint::THUMB_PROXIMAL => 0.0, + _ => 1.0, + }; + let curl_angle = -((mul * curl * 80.0) + 5.0); + return curl_angle; +} + +fn log_hand(hand_pose: [Posef; 26]) { + let _palm_wrist = hand_pose[HandJoint::WRIST].position.to_vec3() + - hand_pose[HandJoint::PALM].position.to_vec3(); + info!( + "palm-wrist: {}", + hand_pose[HandJoint::WRIST].position.to_vec3() + - hand_pose[HandJoint::PALM].position.to_vec3() + ); + + info!( + "wrist-tm: {}", + hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "tm-tp: {}", + hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::THUMB_METACARPAL].position.to_vec3() + ); + info!( + "tp-td: {}", + hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() + - hand_pose[HandJoint::THUMB_PROXIMAL].position.to_vec3() + ); + info!( + "td-tt: {}", + hand_pose[HandJoint::THUMB_TIP].position.to_vec3() + - hand_pose[HandJoint::THUMB_DISTAL].position.to_vec3() + ); + + info!( + "wrist-im: {}", + hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "im-ip: {}", + hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::INDEX_METACARPAL].position.to_vec3() + ); + info!( + "ip-ii: {}", + hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::INDEX_PROXIMAL].position.to_vec3() + ); + info!( + "ii-id: {}", + hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() + - hand_pose[HandJoint::INDEX_INTERMEDIATE].position.to_vec3() + ); + info!( + "id-it: {}", + hand_pose[HandJoint::INDEX_TIP].position.to_vec3() + - hand_pose[HandJoint::INDEX_DISTAL].position.to_vec3() + ); + + info!( + "wrist-mm: {}", + hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "mm-mp: {}", + hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_METACARPAL].position.to_vec3() + ); + info!( + "mp-mi: {}", + hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_PROXIMAL].position.to_vec3() + ); + info!( + "mi-md: {}", + hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_INTERMEDIATE].position.to_vec3() + ); + info!( + "md-mt: {}", + hand_pose[HandJoint::MIDDLE_TIP].position.to_vec3() + - hand_pose[HandJoint::MIDDLE_DISTAL].position.to_vec3() + ); + + info!( + "wrist-rm: {}", + hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "rm-rp: {}", + hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::RING_METACARPAL].position.to_vec3() + ); + info!( + "rp-ri: {}", + hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::RING_PROXIMAL].position.to_vec3() + ); + info!( + "ri-rd: {}", + hand_pose[HandJoint::RING_DISTAL].position.to_vec3() + - hand_pose[HandJoint::RING_INTERMEDIATE].position.to_vec3() + ); + info!( + "rd-rt: {}", + hand_pose[HandJoint::RING_TIP].position.to_vec3() + - hand_pose[HandJoint::RING_DISTAL].position.to_vec3() + ); + + info!( + "wrist-lm: {}", + hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() + - hand_pose[HandJoint::WRIST].position.to_vec3() + ); + info!( + "lm-lp: {}", + hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() + - hand_pose[HandJoint::LITTLE_METACARPAL].position.to_vec3() + ); + info!( + "lp-li: {}", + hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() + - hand_pose[HandJoint::LITTLE_PROXIMAL].position.to_vec3() + ); + info!( + "li-ld: {}", + hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() + - hand_pose[HandJoint::LITTLE_INTERMEDIATE].position.to_vec3() + ); + info!( + "ld-lt: {}", + hand_pose[HandJoint::LITTLE_TIP].position.to_vec3() + - hand_pose[HandJoint::LITTLE_DISTAL].position.to_vec3() + ); +} + + +pub fn update_emulated_hand_skeletons( + right_controller_query: Query<(&GlobalTransform, With)>, + left_controller_query: Query<(&GlobalTransform, With)>, + hand_states_option: Option>, + mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>, +) { + match hand_states_option { + Some(hands) => { + let left_hand_transform = left_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + update_hand_bones_emulated( + left_hand_transform, + Hand::Left, + hands.left, + &mut hand_bone_query, + ); + let right_hand_transform = right_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + update_hand_bones_emulated( + right_hand_transform, + Hand::Right, + hands.right, + &mut hand_bone_query, + ); + } + None => info!("hand states resource not initialized yet"), + } +} + +pub fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) { + for (transform, hand_bone) in query.iter() { + let (radius, color) = get_bone_gizmo_style(hand_bone); + gizmos.sphere(transform.translation, transform.rotation, radius, color); + } +} + +fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { + match hand_bone { + HandBone::Palm => (0.01, Color::WHITE), + HandBone::Wrist => (0.01, Color::GRAY), + HandBone::ThumbMetacarpal => (0.01, Color::RED), + HandBone::ThumbProximal => (0.008, Color::RED), + HandBone::ThumbDistal => (0.006, Color::RED), + HandBone::ThumbTip => (0.004, Color::RED), + HandBone::IndexMetacarpal => (0.01, Color::ORANGE), + HandBone::IndexProximal => (0.008, Color::ORANGE), + HandBone::IndexIntermediate => (0.006, Color::ORANGE), + HandBone::IndexDistal => (0.004, Color::ORANGE), + HandBone::IndexTip => (0.002, Color::ORANGE), + HandBone::MiddleMetacarpal => (0.01, Color::YELLOW), + HandBone::MiddleProximal => (0.008, Color::YELLOW), + HandBone::MiddleIntermediate => (0.006, Color::YELLOW), + HandBone::MiddleDistal => (0.004, Color::YELLOW), + HandBone::MiddleTip => (0.002, Color::YELLOW), + HandBone::RingMetacarpal => (0.01, Color::GREEN), + HandBone::RingProximal => (0.008, Color::GREEN), + HandBone::RingIntermediate => (0.006, Color::GREEN), + HandBone::RingDistal => (0.004, Color::GREEN), + HandBone::RingTip => (0.002, Color::GREEN), + HandBone::LittleMetacarpal => (0.01, Color::BLUE), + HandBone::LittleProximal => (0.008, Color::BLUE), + HandBone::LittleIntermediate => (0.006, Color::BLUE), + HandBone::LittleDistal => (0.004, Color::BLUE), + HandBone::LittleTip => (0.002, Color::BLUE), + } +} \ No newline at end of file From 508a2acbdd63d883aa7feacdc4f1e94065686083 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Sat, 14 Oct 2023 11:02:49 -0400 Subject: [PATCH 13/14] look upon my bad code --- examples/xr.rs | 10 +-- src/xr_input/hand.rs | 174 +++++++++++++++++++++++++++++++++---------- 2 files changed, 138 insertions(+), 46 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index cbc7ac7..363cbe9 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -5,7 +5,7 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; -use bevy_openxr::xr_input::hand::{spawn_hand_entities, update_hand_states, HandStatesResource, update_emulated_hand_skeletons, draw_hand_entities}; +use bevy_openxr::xr_input::hand::OpenXrHandInput; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, @@ -23,13 +23,9 @@ fn main() { .add_plugins(FrameTimeDiagnosticsPlugin) .add_systems(Startup, setup) .add_systems(Update, proto_locomotion) - .add_systems(Startup, spawn_controllers_example) - .add_systems(Update, update_emulated_hand_skeletons) - .add_systems(PreUpdate, update_hand_states) - .add_systems(PostUpdate, draw_hand_entities) - .add_systems(Startup, spawn_hand_entities) .insert_resource(PrototypeLocomotionConfig::default()) - .insert_resource(HandStatesResource::default()) + .add_systems(Startup, spawn_controllers_example) + .add_plugins(OpenXrHandInput) .run(); } diff --git a/src/xr_input/hand.rs b/src/xr_input/hand.rs index f02b5c4..63e5ef8 100644 --- a/src/xr_input/hand.rs +++ b/src/xr_input/hand.rs @@ -1,11 +1,51 @@ use std::f32::consts::PI; -use bevy::prelude::{Entity, Resource, Component, default, SpatialBundle, Commands, Res, ResMut, info, Transform, Query, Quat, Vec3, GlobalTransform, With, Gizmos, Color}; +use bevy::prelude::{ + default, info, Color, Commands, Component, Entity, Gizmos, GlobalTransform, Plugin, PostUpdate, + PreUpdate, Quat, Query, Res, ResMut, Resource, SpatialBundle, Startup, Transform, Update, Vec3, + With, +}; use openxr::{HandJoint, Posef}; -use crate::{resources::{XrFrameState, XrInstance, XrSession}, input::XrInput, xr_input::Vec3Conv}; +use crate::{ + input::XrInput, + resources::{XrFrameState, XrInstance, XrSession}, + xr_input::Vec3Conv, +}; -use super::{trackers::{OpenXRTracker, OpenXRRightController, OpenXRLeftController}, Hand, oculus_touch::OculusController, hand_poses::get_simulated_open_hand_transforms}; +use super::{ + hand_poses::get_simulated_open_hand_transforms, + oculus_touch::OculusController, + trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTracker}, + Hand, +}; + +/// add debug renderer for controllers +#[derive(Default)] +pub struct OpenXrHandInput; + +impl Plugin for OpenXrHandInput { + fn build(&self, app: &mut bevy::prelude::App) { + app.add_systems(Update, update_hand_skeletons) + .add_systems(PreUpdate, update_hand_states) + .add_systems(PostUpdate, draw_hand_entities) + .add_systems(Startup, spawn_hand_entities) + .insert_resource(HandStatesResource::default()) + .insert_resource(HandInputSource::default()); + } +} + +#[derive(Resource)] +pub enum HandInputSource { + Emulated, + OpenXr, +} + +impl Default for HandInputSource { + fn default() -> Self { + HandInputSource::Emulated + } +} #[derive(Resource, Default)] pub struct HandsResource { @@ -25,7 +65,15 @@ pub struct HandResource { impl Default for HandResource { fn default() -> Self { - Self { palm: Entity::PLACEHOLDER, wrist: Entity::PLACEHOLDER, thumb: Default::default(), index: Default::default(), middle: Default::default(), ring: Default::default(), little: Default::default() } + Self { + palm: Entity::PLACEHOLDER, + wrist: Entity::PLACEHOLDER, + thumb: Default::default(), + index: Default::default(), + middle: Default::default(), + ring: Default::default(), + little: Default::default(), + } } } @@ -38,7 +86,12 @@ pub struct ThumbResource { impl Default for ThumbResource { fn default() -> Self { - Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + Self { + metacarpal: Entity::PLACEHOLDER, + proximal: Entity::PLACEHOLDER, + distal: Entity::PLACEHOLDER, + tip: Entity::PLACEHOLDER, + } } } pub struct IndexResource { @@ -51,7 +104,13 @@ pub struct IndexResource { impl Default for IndexResource { fn default() -> Self { - Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + Self { + metacarpal: Entity::PLACEHOLDER, + proximal: Entity::PLACEHOLDER, + intermediate: Entity::PLACEHOLDER, + distal: Entity::PLACEHOLDER, + tip: Entity::PLACEHOLDER, + } } } pub struct MiddleResource { @@ -63,7 +122,13 @@ pub struct MiddleResource { } impl Default for MiddleResource { fn default() -> Self { - Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + Self { + metacarpal: Entity::PLACEHOLDER, + proximal: Entity::PLACEHOLDER, + intermediate: Entity::PLACEHOLDER, + distal: Entity::PLACEHOLDER, + tip: Entity::PLACEHOLDER, + } } } pub struct RingResource { @@ -75,7 +140,13 @@ pub struct RingResource { } impl Default for RingResource { fn default() -> Self { - Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + Self { + metacarpal: Entity::PLACEHOLDER, + proximal: Entity::PLACEHOLDER, + intermediate: Entity::PLACEHOLDER, + distal: Entity::PLACEHOLDER, + tip: Entity::PLACEHOLDER, + } } } pub struct LittleResource { @@ -87,7 +158,13 @@ pub struct LittleResource { } impl Default for LittleResource { fn default() -> Self { - Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } + Self { + metacarpal: Entity::PLACEHOLDER, + proximal: Entity::PLACEHOLDER, + intermediate: Entity::PLACEHOLDER, + distal: Entity::PLACEHOLDER, + tip: Entity::PLACEHOLDER, + } } } @@ -176,7 +253,9 @@ pub fn spawn_hand_entities(mut commands: Commands) { HandBone::IndexTip => hand_resource.right.index.tip = boneid, HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid, HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid, - HandBone::MiddleIntermediate => hand_resource.right.middle.intermediate = boneid, + HandBone::MiddleIntermediate => { + hand_resource.right.middle.intermediate = boneid + } HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid, HandBone::MiddleTip => hand_resource.right.middle.tip = boneid, HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid, @@ -186,7 +265,9 @@ pub fn spawn_hand_entities(mut commands: Commands) { HandBone::RingTip => hand_resource.right.ring.tip = boneid, HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid, HandBone::LittleProximal => hand_resource.right.little.proximal = boneid, - HandBone::LittleIntermediate => hand_resource.right.little.intermediate = boneid, + HandBone::LittleIntermediate => { + hand_resource.right.little.intermediate = boneid + } HandBone::LittleDistal => hand_resource.right.little.distal = boneid, HandBone::LittleTip => hand_resource.right.little.tip = boneid, }, @@ -915,39 +996,54 @@ fn log_hand(hand_pose: [Posef; 26]) { ); } - -pub fn update_emulated_hand_skeletons( +pub fn update_hand_skeletons( right_controller_query: Query<(&GlobalTransform, With)>, left_controller_query: Query<(&GlobalTransform, With)>, hand_states_option: Option>, mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>, + input_source: Option>, ) { - match hand_states_option { - Some(hands) => { - let left_hand_transform = left_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - update_hand_bones_emulated( - left_hand_transform, - Hand::Left, - hands.left, - &mut hand_bone_query, - ); - let right_hand_transform = right_controller_query - .get_single() - .unwrap() - .0 - .compute_transform(); - update_hand_bones_emulated( - right_hand_transform, - Hand::Right, - hands.right, - &mut hand_bone_query, - ); + match input_source { + Some(res) => match *res { + HandInputSource::Emulated => { + info!("hand input source is emulated"); + match hand_states_option { + Some(hands) => { + let left_hand_transform = left_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + update_hand_bones_emulated( + left_hand_transform, + Hand::Left, + hands.left, + &mut hand_bone_query, + ); + let right_hand_transform = right_controller_query + .get_single() + .unwrap() + .0 + .compute_transform(); + update_hand_bones_emulated( + right_hand_transform, + Hand::Right, + hands.right, + &mut hand_bone_query, + ); + } + None => info!("hand states resource not initialized yet"), + } + } + HandInputSource::OpenXr => { + info!("hand input source is open XR: this is not implemented yet"); + return; + } + }, + None => { + info!("hand input source not initialized"); + return; } - None => info!("hand states resource not initialized yet"), } } @@ -987,4 +1083,4 @@ fn get_bone_gizmo_style(hand_bone: &HandBone) -> (f32, Color) { HandBone::LittleDistal => (0.004, Color::BLUE), HandBone::LittleTip => (0.002, Color::BLUE), } -} \ No newline at end of file +} From 6770c975d76f482121caa04991f51b41b5f8b0f9 Mon Sep 17 00:00:00 2001 From: Jay Christy Date: Sat, 14 Oct 2023 11:05:06 -0400 Subject: [PATCH 14/14] pulled debug rendering into its own plugin --- examples/xr.rs | 3 ++- src/xr_input/hand.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/xr.rs b/examples/xr.rs index 363cbe9..f0d468d 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -5,7 +5,7 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::prelude::*; use bevy::transform::components::Transform; -use bevy_openxr::xr_input::hand::OpenXrHandInput; +use bevy_openxr::xr_input::hand::{OpenXrHandInput, HandInputDebugRenderer}; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::trackers::{ OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, @@ -26,6 +26,7 @@ fn main() { .insert_resource(PrototypeLocomotionConfig::default()) .add_systems(Startup, spawn_controllers_example) .add_plugins(OpenXrHandInput) + .add_plugins(HandInputDebugRenderer) .run(); } diff --git a/src/xr_input/hand.rs b/src/xr_input/hand.rs index 63e5ef8..f8a70ac 100644 --- a/src/xr_input/hand.rs +++ b/src/xr_input/hand.rs @@ -28,13 +28,22 @@ impl Plugin for OpenXrHandInput { fn build(&self, app: &mut bevy::prelude::App) { app.add_systems(Update, update_hand_skeletons) .add_systems(PreUpdate, update_hand_states) - .add_systems(PostUpdate, draw_hand_entities) .add_systems(Startup, spawn_hand_entities) .insert_resource(HandStatesResource::default()) .insert_resource(HandInputSource::default()); } } +/// add debug renderer for controllers +#[derive(Default)] +pub struct HandInputDebugRenderer; + +impl Plugin for HandInputDebugRenderer{ + fn build(&self, app: &mut bevy::prelude::App) { + app.add_systems(PostUpdate, draw_hand_entities); + } +} + #[derive(Resource)] pub enum HandInputSource { Emulated,