Skip to content

Commit

Permalink
Comment and deduplicate prototype input rotation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 19, 2024
1 parent 361f145 commit eb92e9c
Showing 1 changed file with 36 additions and 63 deletions.
99 changes: 36 additions & 63 deletions src/xr_input/prototype_locomotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,89 +74,62 @@ pub fn proto_locomotion(
let mut position = tracking_root_query
.get_single_mut()
.expect("too many tracking roots");
//get the stick input and do some maths
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 => {
let Some(view) = views.first() else {
info!("locomotion found no head to use for relative movement");
return;
};
view.pose
}
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,
}
}
// 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 => {
//tick the timer
// 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() {
//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();
return;
} else {
config.snap_angle * rot_input.signum()
}
}
}
};

// 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);
}

0 comments on commit eb92e9c

Please sign in to comment.