first attempt at locomotion

This commit is contained in:
Jay Christy
2023-09-19 16:18:20 -04:00
parent d3b1fe2f4b
commit 263168fc26
4 changed files with 77 additions and 12 deletions

View File

@@ -1,7 +1,11 @@
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::debug_gizmos::OpenXrDebugRenderer;
use bevy_openxr::xr_input::oculus_touch::OculusController;
use bevy_openxr::xr_input::{Hand, TrackingRoot};
use bevy_openxr::DefaultXrPlugins;
fn main() {
@@ -14,6 +18,7 @@ fn main() {
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup)
.add_systems(Update, proto_locomotion)
.run();
}
@@ -36,6 +41,13 @@ fn setup(
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
material: materials.add(Color::rgb(0.8, 0.0, 0.0).into()),
transform: Transform::from_xyz(0.0, 0.5, 1.0),
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
@@ -52,3 +64,29 @@ fn setup(
..default()
},));
}
fn proto_locomotion(
time: Res<Time>,
mut tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>,
oculus_controller: Res<OculusController>,
frame_state: Res<XrFrameState>,
xr_input: Res<XrInput>,
instance: Res<XrInstance>,
session: Res<XrSession>,
) {
//lock frame
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
let root = tracking_root_query.get_single_mut();
match root {
Ok(mut position) => {
//do work here?
let stick = controller.thumbstick(Hand::Left);
let input = Vec3::new(stick.x, 0.0, -stick.y);
let speed = 1.0;
position.0.translation += input * speed * time.delta_seconds()
}
Err(_) => info!("too many tracking roots"),
}
}

View File

@@ -1,4 +1,6 @@
use bevy::prelude::{Color, Gizmos, Plugin, Quat, Res, Transform, Update, Vec2, Vec3};
use bevy::prelude::{
info, Color, Gizmos, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With, PreUpdate,
};
use crate::{
input::XrInput,
@@ -7,7 +9,7 @@ use crate::{
use crate::xr_input::{
oculus_touch::{OculusController, OculusControllerRef},
Hand, QuatConv, Vec3Conv,
Hand, QuatConv, TrackingRoot, Vec3Conv,
};
/// add debug renderer for controllers
@@ -27,17 +29,33 @@ pub fn draw_gizmos(
xr_input: Res<XrInput>,
instance: Res<XrInstance>,
session: Res<XrSession>,
tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>,
) {
//lock frame
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
//tracking root?
let mut tracking_position = Vec3::ZERO;
let root = tracking_root_query.get_single();
match root {
Ok(position) => {
gizmos.circle(position.0.translation, Vec3::Y, 0.2, Color::RED);
tracking_position = position.0.translation;
}
Err(_) => info!("too many tracking roots"),
}
//draw the hands
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, tracking_position);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, tracking_position);
}
fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, hand: Hand) {
fn draw_hand_gizmo(
gizmos: &mut Gizmos,
controller: &OculusControllerRef<'_>,
hand: Hand,
tracking_root: Vec3,
) {
match hand {
Hand::Left => {
//get left controller
@@ -51,7 +69,7 @@ fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, ha
let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = left_controller.0.pose.position.to_vec3();
let controller_vec3 = left_controller.0.pose.position.to_vec3() + tracking_root;
let controller_quat = left_controller.0.pose.orientation.to_quat();
let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
@@ -158,7 +176,7 @@ fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, ha
let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = right_controller.0.pose.position.to_vec3();
let controller_vec3 = right_controller.0.pose.position.to_vec3() + tracking_root;
let controller_quat = right_controller.0.pose.orientation.to_quat();
let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);

View File

@@ -10,11 +10,11 @@ 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::IntoSystemConfigs;
use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, Vec3};
use bevy::prelude::{default, Commands, Component, Plugin, PreUpdate, Quat, Res, Vec3};
use bevy::prelude::{BuildChildren, IntoSystemConfigs};
use bevy::render::camera::CameraProjectionPlugin;
use bevy::render::view::{update_frusta, VisibilitySystems};
use bevy::transform::TransformSystem;
use bevy::transform::{TransformBundle, TransformSystem};
#[derive(Copy, Clone)]
pub struct OpenXrInput {
@@ -25,6 +25,10 @@ pub enum Hand {
Left,
Right,
}
#[derive(Component)]
pub struct TrackingRoot;
impl OpenXrInput {
pub fn new(controller_type: XrControllerType) -> Self {
Self { controller_type }
@@ -52,8 +56,12 @@ impl Plugin for OpenXrInput {
}
fn setup_xr_cameras(mut commands: Commands) {
commands.spawn(XrCameraBundle::new(Eye::Right));
commands.spawn(XrCameraBundle::new(Eye::Left));
//this needs to do the whole xr tracking volume not just cameras
//get the root?
let tracking_root = commands.spawn((TransformBundle { ..default() }, TrackingRoot)).id();
let right = commands.spawn(XrCameraBundle::new(Eye::Right)).id();
let left = commands.spawn(XrCameraBundle::new(Eye::Left)).id();
commands.entity(tracking_root).push_children(&[right, left]);
}
fn action_set_system(action_sets: Res<ActionSets>, session: Res<XrSession>) {

View File

@@ -46,6 +46,7 @@ pub enum XrCameraType {
Xr(Eye),
Flatscreen,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum Eye {
Left = 0,