added transform utils for snapping to position and rotation

This commit is contained in:
Jay Christy
2024-06-11 13:17:16 -04:00
parent 8807b5f927
commit 8ecebb7d2c
4 changed files with 127 additions and 41 deletions

View File

@@ -1,9 +1,12 @@
//! A simple 3D scene with light shining over a cube sitting on a plane.
//! A simple example of how to use the transform utils to set the players position and orientation
use bevy::prelude::*;
use bevy_openxr::add_xr_plugins;
use bevy_xr_utils::xr_utils_actions::{ActiveSet, XRUtilsAction, XRUtilsActionSet, XRUtilsActionState, XRUtilsActionSystemSet, XRUtilsActionsPlugin, XRUtilsBinding};
use bevy_xr_utils::transform_utils;
use bevy_xr_utils::transform_utils::{self, SnapToPosition, SnapToRotation};
use bevy_xr_utils::xr_utils_actions::{
ActiveSet, XRUtilsAction, XRUtilsActionSet, XRUtilsActionState, XRUtilsActionSystemSet,
XRUtilsActionsPlugin, XRUtilsBinding,
};
fn main() {
App::new()
@@ -18,8 +21,16 @@ fn main() {
)
.add_systems(
Update,
read_action_with_marker_component.after(XRUtilsActionSystemSet::SyncActionStates),
send_look_at_red_cube_event.after(XRUtilsActionSystemSet::SyncActionStates),
)
.add_systems(
Update,
send_recenter.after(XRUtilsActionSystemSet::SyncActionStates),
)
.insert_resource(AmbientLight {
color: Default::default(),
brightness: 500.0,
})
.run();
}
@@ -43,7 +54,7 @@ fn setup(
transform: Transform::from_xyz(4.0, 0.5, 0.0).with_scale(Vec3::splat(0.5)),
..default()
});
// red cube
// blue cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::rgb_u8(3, 28, 252)),
@@ -64,15 +75,14 @@ fn setup(
transform: Transform::from_xyz(0.0, 0.5, -4.0).with_scale(Vec3::splat(0.5)),
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
// black cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::rgb_u8(0, 0, 0)),
transform: Transform::from_xyz(0.0, 0.1, 0.0).with_scale(Vec3::splat(0.2)),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
@@ -110,7 +120,31 @@ fn create_action_entities(mut commands: Commands) {
let binding = commands
.spawn(XRUtilsBinding {
profile: "/interaction_profiles/valve/index_controller".into(),
binding: "/user/hand/left//input/a/click".into(),
binding: "/user/hand/left/input/a/click".into(),
})
.id();
//add action to set, this isnt the best
//TODO look into a better system
commands.entity(action).add_child(binding);
commands.entity(set).add_child(action);
let action = commands
.spawn((
XRUtilsAction {
action_name: "center".into(),
localized_name: "center_localized".into(),
action_type: bevy_xr::actions::ActionType::Bool,
},
Center, //lets try a marker component
))
.id();
//create a binding
let binding = commands
.spawn(XRUtilsBinding {
profile: "/interaction_profiles/valve/index_controller".into(),
binding: "/user/hand/right/input/a/click".into(),
})
.id();
@@ -120,11 +154,48 @@ fn create_action_entities(mut commands: Commands) {
commands.entity(set).add_child(action);
}
fn read_action_with_marker_component(
fn send_look_at_red_cube_event(
mut action_query: Query<&XRUtilsActionState, With<FaceRedAction>>,
mut event_writer: EventWriter<SnapToRotation>,
) {
//now for the actual checking
for state in action_query.iter_mut() {
info!("action state is: {:?}", state);
match state {
XRUtilsActionState::Bool(state) => {
let send = state.current_state && state.changed_since_last_sync;
if send {
info!("send facing");
let quat = Transform::default()
.looking_at(Transform::from_xyz(4.0, 0.0, 0.0).translation, Vec3::Y); //this is a transform facing the red cube from the center of the scene, you should use the HMD posision but I was lazy.
event_writer.send(SnapToRotation(quat.rotation));
}
}
XRUtilsActionState::Float(_) => (),
XRUtilsActionState::Vector(_) => (),
}
}
}
#[derive(Component)]
pub struct Center;
fn send_recenter(
mut action_query: Query<&XRUtilsActionState, With<Center>>,
mut event_writer: EventWriter<SnapToPosition>,
) {
//now for the actual checking
for state in action_query.iter_mut() {
match state {
XRUtilsActionState::Bool(state) => {
let send = state.current_state && state.changed_since_last_sync;
if send {
let center = Transform::default().translation;
event_writer.send(SnapToPosition(center));
}
}
XRUtilsActionState::Float(_) => (),
XRUtilsActionState::Vector(_) => (),
}
}
}