proto smooth rotation

This commit is contained in:
Jay Christy
2023-09-20 11:42:23 -04:00
parent 9043ebeb4d
commit 0476736f5c
2 changed files with 93 additions and 16 deletions

View File

@@ -1,11 +1,14 @@
use std::f32::consts::PI;
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::Gizmos;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::transform::components::Transform; use bevy::transform::components::Transform;
use bevy_openxr::input::XrInput; use bevy_openxr::input::XrInput;
use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession, XrViews}; use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession, XrViews};
use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer; use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
use bevy_openxr::xr_input::oculus_touch::OculusController; use bevy_openxr::xr_input::oculus_touch::OculusController;
use bevy_openxr::xr_input::{Hand, QuatConv, TrackingRoot}; use bevy_openxr::xr_input::{Hand, QuatConv, TrackingRoot, Vec3Conv};
use bevy_openxr::DefaultXrPlugins; use bevy_openxr::DefaultXrPlugins;
fn main() { fn main() {
@@ -70,6 +73,11 @@ pub enum LocomotionType {
Hand, Hand,
} }
pub enum RotationType {
Smooth,
Snap,
}
fn proto_locomotion( fn proto_locomotion(
time: Res<Time>, time: Res<Time>,
mut tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>, mut tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>,
@@ -79,6 +87,7 @@ fn proto_locomotion(
instance: Res<XrInstance>, instance: Res<XrInstance>,
session: Res<XrSession>, session: Res<XrSession>,
views: ResMut<XrViews>, views: ResMut<XrViews>,
mut gizmos: Gizmos,
) { ) {
//lock frame //lock frame
let frame_state = *frame_state.lock().unwrap(); let frame_state = *frame_state.lock().unwrap();
@@ -93,7 +102,7 @@ fn proto_locomotion(
let speed = 1.0; let speed = 1.0;
//now the question is how do we do hmd based locomotion //now the question is how do we do hmd based locomotion
//or controller based for that matter //or controller based for that matter
let locomotion_type = LocomotionType::Hand; let locomotion_type = LocomotionType::Head;
let mut reference_quat = Quat::IDENTITY; let mut reference_quat = Quat::IDENTITY;
match locomotion_type { match locomotion_type {
LocomotionType::Head => { LocomotionType::Head => {
@@ -101,19 +110,65 @@ fn proto_locomotion(
let views = v.get(0); let views = v.get(0);
match views { match views {
Some(view) => { Some(view) => {
reference_quat = view.pose.orientation.to_quat(); reference_quat = view
.pose
.orientation
.to_quat()
.mul_quat(position.0.rotation);
} }
None => return, None => return,
} }
} }
LocomotionType::Hand => { LocomotionType::Hand => {
let grip = controller.grip_space(Hand::Left); let grip = controller.grip_space(Hand::Left);
reference_quat = grip.0.pose.orientation.to_quat(); reference_quat = grip
.0
.pose
.orientation
.to_quat()
.mul_quat(position.0.rotation); //TODO add root tracking quat to this so we simulate the global rotation
} }
} }
let mut locomotion_vec = reference_quat.mul_vec3(input); let mut locomotion_vec = reference_quat.mul_vec3(input);
locomotion_vec.y = 0.0; locomotion_vec.y = 0.0;
position.0.translation += locomotion_vec * speed * time.delta_seconds() position.0.translation += locomotion_vec * speed * time.delta_seconds();
//now time for rotation
let snap_angle = 45.0;
let smooth_rotation_speed = 0.5 * PI;
let rotation_setting = RotationType::Smooth;
match rotation_setting {
RotationType::Smooth => {
//once again with the math
let control_stick = controller.thumbstick(Hand::Right);
let rot_input = -control_stick.x; //why is this negative i dont know
if rot_input.abs() <= 0.2 {
//return;
}
let smoth_rot = Quat::from_rotation_y(
rot_input * smooth_rotation_speed * time.delta_seconds(),
);
//apply rotation
let v = views.lock().unwrap();
let views = v.get(0);
let views = v.get(0);
match views {
Some(view) => {
let mut hmd_translation = view.pose.position.to_vec3();
hmd_translation.y = 0.0;
let local = position.0.translation;
let global = position.0.rotation.mul_vec3(hmd_translation) + local;
gizmos.circle(global, Vec3::Y, 0.1, Color::GREEN);
position.0.rotate_around(global, smoth_rot);
}
None => return,
}
}
RotationType::Snap => {
//yup even more math i cant remember
todo!();
}
}
} }
Err(_) => info!("too many tracking roots"), Err(_) => info!("too many tracking roots"),
} }

View File

@@ -1,4 +1,6 @@
use bevy::prelude::{Color, Gizmos, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With, info}; use bevy::prelude::{
info, Color, Gizmos, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With,
};
use crate::{ use crate::{
input::XrInput, input::XrInput,
@@ -34,25 +36,35 @@ pub fn draw_gizmos(
//get controller //get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input); let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
//tracking root? //tracking root?
let mut tracking_position = Vec3::ZERO; let mut tracking_transform = &Transform::IDENTITY;
let root = tracking_root_query.get_single(); let root = tracking_root_query.get_single();
match root { match root {
Ok(position) => { Ok(position) => {
gizmos.circle(position.0.translation, Vec3::Y, 0.2, Color::RED); gizmos.circle(
tracking_position = position.0.translation; position.0.translation
+ Vec3 {
x: 0.0,
y: 0.01,
z: 0.0,
},
Vec3::Y,
0.2,
Color::RED,
);
tracking_transform = position.0;
} }
Err(_) => info!("too many tracking roots"), Err(_) => info!("too many tracking roots"),
} }
//draw the hands //draw the hands
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, tracking_position); draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, tracking_transform);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, tracking_position); draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, tracking_transform);
} }
fn draw_hand_gizmo( fn draw_hand_gizmo(
gizmos: &mut Gizmos, gizmos: &mut Gizmos,
controller: &OculusControllerRef<'_>, controller: &OculusControllerRef<'_>,
hand: Hand, hand: Hand,
tracking_root: Vec3, tracking_root: &Transform,
) { ) {
match hand { match hand {
Hand::Left => { Hand::Left => {
@@ -67,8 +79,13 @@ fn draw_hand_gizmo(
let grip_quat_offset = Quat::from_rotation_x(-1.4); let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05); let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = left_controller.0.pose.position.to_vec3() + tracking_root; let controller_vec3 = tracking_root
let controller_quat = left_controller.0.pose.orientation.to_quat(); .rotation
.mul_vec3(left_controller.0.pose.position.to_vec3())
+ tracking_root.translation;
let controller_quat = tracking_root
.rotation
.mul_quat(left_controller.0.pose.orientation.to_quat());
let face_quat = controller_quat.mul_quat(face_quat_offset); let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z); let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
@@ -174,8 +191,13 @@ fn draw_hand_gizmo(
let grip_quat_offset = Quat::from_rotation_x(-1.4); let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05); let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = right_controller.0.pose.position.to_vec3() + tracking_root; let controller_vec3 = tracking_root
let controller_quat = right_controller.0.pose.orientation.to_quat(); .rotation
.mul_vec3(right_controller.0.pose.position.to_vec3())
+ tracking_root.translation;
let controller_quat = tracking_root
.rotation
.mul_quat(right_controller.0.pose.orientation.to_quat());
let face_quat = controller_quat.mul_quat(face_quat_offset); let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z); let face_quat_normal = face_quat.mul_vec3(Vec3::Z);