Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some comments and deduplicate or simplify logic #123

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/globe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
) {
// plane
// globe
let radius = 5.0;
commands.spawn((
PbrBundle {
Expand Down
155 changes: 58 additions & 97 deletions src/xr_input/prototype_locomotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,109 +66,70 @@ pub fn proto_locomotion(
session: Res<XrSession>,
views: ResMut<XrViews>,
mut gizmos: Gizmos,
config_option: Option<ResMut<PrototypeLocomotionConfig>>,
mut config: ResMut<PrototypeLocomotionConfig>,
action_sets: Res<XrActionSets>,
) {
let mut config = match config_option {
Some(c) => c,
None => {
info!("no locomotion config");
return;
}
};
//get controller
let controller = oculus_controller.get_ref(&session, &frame_state, &xr_input, &action_sets);
let root = tracking_root_query.get_single_mut();
match root {
Ok(mut position) => {
//get the stick input and do some maths
let stick = controller.thumbstick(Hand::Left);
let input = stick.x * *position.right() + stick.y * *position.forward();
let reference_quat;
match config.locomotion_type {
LocomotionType::Head => {
let views = views.first();
match views {
Some(view) => {
reference_quat = view.pose.orientation.to_quat();
}
None => return,
}
}
LocomotionType::Hand => {
let grip = controller.grip_space(Hand::Left);
reference_quat = grip.0.pose.orientation.to_quat();
}
}
let (yaw, _pitch, _roll) = reference_quat.to_euler(EulerRot::YXZ);
let reference_quat = Quat::from_axis_angle(*position.up(), yaw);
let locomotion_vec = reference_quat.mul_vec3(input);
position.translation += locomotion_vec * config.locomotion_speed * time.delta_seconds();
let mut position = tracking_root_query
.get_single_mut()
.expect("too many tracking roots");
let Some(view) = views.first() else {
info!("locomotion found no head to use for relative movement");
return;
};
// Get the stick input
let stick = controller.thumbstick(Hand::Left);
let input = stick.x * *position.right() + stick.y * *position.forward();
let reference_quat = match config.locomotion_type {
LocomotionType::Head => view.pose,
LocomotionType::Hand => {
let (loc, _vel) = controller.grip_space(Hand::Left);
loc.pose
}
}
.orientation
.to_quat();
// Get rotation around "up" axis (y).
let (yaw, _pitch, _roll) = reference_quat.to_euler(EulerRot::YXZ);
// A quat representing the global position and rotation, ignoring pitch and roll
let reference_quat = Quat::from_axis_angle(*position.up(), yaw);
// Direction to move towards, in global orientation.
let locomotion_vec = reference_quat.mul_vec3(input);
// Actually move the player
position.translation += locomotion_vec * config.locomotion_speed * time.delta_seconds();

//now time for rotation
// Now rotate the player

match config.rotation_type {
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() <= config.rotation_stick_deadzone {
return;
}
let smoth_rot = Quat::from_axis_angle(
*position.up(),
rot_input * config.smooth_rotation_speed * time.delta_seconds(),
);
//apply rotation
let views = views.first();
match views {
Some(view) => {
let mut hmd_translation = view.pose.position.to_vec3();
hmd_translation.y = 0.0;
let local = position.translation;
let global = position.rotation.mul_vec3(hmd_translation) + local;
gizmos.circle(global, position.up(), 0.1, Color::GREEN);
position.rotate_around(global, smoth_rot);
}
None => return,
}
}
RotationType::Snap => {
//tick the timer
config.rotation_timer.timer.tick(time.delta());
if config.rotation_timer.timer.finished() {
//now we can snap turn?
//once again with the math
let control_stick = controller.thumbstick(Hand::Right);
let rot_input = -control_stick.x;
if rot_input.abs() <= config.rotation_stick_deadzone {
return;
}
let dir: f32 = match rot_input > 0.0 {
true => 1.0,
false => -1.0,
};
let smoth_rot =
Quat::from_axis_angle(*position.up(), config.snap_angle * dir);
//apply rotation
let v = views;
let views = v.first();
match views {
Some(view) => {
let mut hmd_translation = view.pose.position.to_vec3();
hmd_translation.y = 0.0;
let local = position.translation;
let global = position.rotation.mul_vec3(hmd_translation) + local;
gizmos.circle(global, position.up(), 0.1, Color::GREEN);
position.rotate_around(global, smoth_rot);
}
None => return,
}
config.rotation_timer.timer.reset();
}
}
// Get controller direction and "turning strength"
let control_stick = controller.thumbstick(Hand::Right);
let rot_input = -control_stick.x; //why is this negative i dont know
if rot_input.abs() <= config.rotation_stick_deadzone {
return;
}
let angle = match config.rotation_type {
RotationType::Smooth => rot_input * config.smooth_rotation_speed * time.delta_seconds(),
RotationType::Snap => {
// The timer is needed so we don't move by the snap amount every frame.
// FIXME: use no timer at all and require moving the controller stick back to the default
// location. This is what most VR games do.
config.rotation_timer.timer.tick(time.delta());
if config.rotation_timer.timer.finished() {
config.rotation_timer.timer.reset();
return;
} else {
config.snap_angle * rot_input.signum()
}
}
Err(_) => info!("too many tracking roots"),
}
};

// Rotate around the body, not the head
let smoth_rot = Quat::from_axis_angle(*position.up(), angle);
// Apply rotation
let mut hmd_translation = view.pose.position.to_vec3();
hmd_translation.y = 0.0;
let local = position.translation;
let global = position.rotation.mul_vec3(hmd_translation) + local;
gizmos.circle(global, position.up(), 0.1, Color::GREEN);
position.rotate_around(global, smoth_rot);
}
Loading