Implement Auto-turn and Redirected Walking

Initial implementation of Auto-Turn

Auto-turn leverages Space Turn automatically whenever you reach a configured distance from your chaperone boundary. Turn direction is in order of precedence:

1. Away from a corner where you're already touching one wall. This is to avoid being turned to face a wall, which we pretty much never want. If 'use corner angle' is enabled, it'll turn the angle of the corner, otherwise it'll turn the angle of your headset to the wall you encountered.
2. In whichever direction will start you unwinding your cord. If your rotations is greater than 'cord detangling rotations' and your angle to the wall is less than 'min detangling angle' it'll turn you such that the thing you're walking towards is directly along a wall, in the direction which will begin unwinding your cord.
3. In the closest angle to the wall. This too will put whatever you're walking towards directly along a wall, so you can walk the maximum distance to it. 
- If Smooth turn is checked, it'll turn at a constant (configurable) rate until the world is aligned with the new angle, otherwise it'll snap to the new direction instantly. 
- Activation distance is the distance from your chaperone at which auto-turn starts, and deactivation distance is the minimum distance (+ the activation distance) at which it'll be able to be activated again, to avoid turning twice at once.


Redirected walking tries to gently guide you away from your playspace boundaries by tweaking the world's angle at smaller increments than your brain is capable of noticing. While most playspaces won't be big enough to allow this to give you a true 'infinite walk' experience, it can give the perception of your playspace being ever so slightly bigger by making you walk in a slight arc in real life while walking forward in VR.
- Redirected walking is independent from Auto-turn and can be activated on its own. At both the 'slight' and 'imperceptable' radii, the turning speed is incredibly minimal. When walking along one wall of your chaperone to another, you should notice the far corner of your playspace (relative to the world) drifting ever so slightly away from you. In normal circumstances at the lowest settings this shouldn't be noticable. 


Co-authored-by: feilen <feilen1000@gmail.com>

* Initial implementation of Infinite Walk

* Code cleanup and abstraction, use nearest corner instead of nearest wall to determine turn direction

* Run clang-format

* Turn relative to the last wall if we run into adjacent walls, otherwise turn at easiest angle

* Limit maximum turns (just refuse to turn for now), only turn on shared corners if going the same direction

* Improve same-corner

* get rid of LastDirectionLeft

* WIP: cord untangling hint

* Try to automatically untangle cord, don't rotate if we encounter a wall behind us

* Incorrect offset for walls

* optimize if hmd inactive

* Cleanup/clarify angles

* (probably?) solve rare segfault

* Adjust the defaults and fix a bug with blocking reverse turns

* Implement smooth turn, fix a bug with shared-corners that would cause it to turn the wrong way

* actually really fix shared corner bug

vect[i + 1 % vect.size()] was somehow evaluating to vect[i + (1 %
vect.size())]

* Move configurables for autoturn to .h to make clearer what's configurable

* WIP: true redirected walking

* First pass optimization/cleanup

* Second pass cleanup/optimize

* Missing clang-format

* Somewhat ugly: Optionally turn the exact angle of the corner when using shared corner turns

* added rotation tab controller in progress

* cleaned up chaperone controller/ finished settings/rotationtab

* did first 4 settings qml, and main qml setup

* initial working build, not all settings UI done

* merging master into redirected walking. (#387)

* Update OVR to 1.10.30 (#379)

* updated to ovr 1.10.30

* fixed updated showoverlaykeyboard call

* fixed keyboard location

* Updated Manifest (#380)

* updated to ovr 1.10.30

* fixed updated showoverlaykeyboard call

* fixed keyboard location

* removed un-necessary matrix, updated app key and manifest

* fixed formatting

* Fix push to mute icon not being used when PTM enabled

* added touch default bindings (#385)

travis CI down/broken?

Co-authored-by: Vasily <vautkin@teknik.io>

* added action, and additional settings

* fixed various bugs added turn speed and mode

* fixed conversion error for turn speed

* added redirected walking options

* formatting

* formatting fixing

* removed old comments

* changed logs to debug and some wording changes

* changed UI layout

* added space turn to rotation page

* added icon, fixed autoturn to autoTurn, fixed conversion to be in qml vs set

* Update src/res/qml/RootPage.qml

yep XD

Co-Authored-By: Kung-vr <38249782+Kung-vr@users.noreply.github.com>

* reverted build changes

* removed un-needed app image build commands

* Cleanup some functions, avoid an extra chaperone check.

* Make redirected walking and autoturn independently enable-able

Co-authored-by: Feilen <feilen1000@gmail.com>
Co-authored-by: Vasily <vautkin@teknik.io>
Co-authored-by: Kung-vr <38249782+Kung-vr@users.noreply.github.com>
This commit is contained in:
ykeara
2020-04-08 18:57:53 -07:00
committed by GitHub
co-authored by Vasily Kung-vr Feilen
parent c0d44812d6
commit 1ae740463c
26 changed files with 1483 additions and 41 deletions
@@ -0,0 +1,553 @@
#include "RotationTabController.h"
#include <QQuickWindow>
#include "../overlaycontroller.h"
#include "../settings/settings.h"
#include "../utils/Matrix.h"
#include "../quaternion/quaternion.h"
#include <cmath>
// application namespace
namespace advsettings
{
void RotationTabController::initStage1()
{ // TODO adjust update rate
// m_chaperoneSettingsUpdateCounter
// = utils::adjustUpdateRate( k_chaperoneSettingsUpdateCounter );
}
void RotationTabController::initStage2( OverlayController* var_parent )
{
this->parent = var_parent;
}
void RotationTabController::eventLoopTick(
vr::TrackedDevicePose_t* devicePoses )
{
if ( devicePoses )
{
m_isHMDActive = false;
std::lock_guard<std::recursive_mutex> lock(
parent->chaperoneUtils().mutex() );
auto& poseHmd = devicePoses[vr::k_unTrackedDeviceIndex_Hmd];
// m_isHMDActive is true when prox sensor OR HMD is moving (~10 seconds
// to update from OVR)
// THIS IS A WORK-AROUND Until proper binding support/calls are made
// availble for prox sensor
if ( vr::VRSystem()->GetTrackedDeviceActivityLevel(
vr::k_unTrackedDeviceIndex_Hmd )
== vr::k_EDeviceActivityLevel_UserInteraction )
{
m_isHMDActive = true;
}
// TODO: this doesn't need to run if both are off
if ( poseHmd.bPoseIsValid && poseHmd.bDeviceIsConnected
&& poseHmd.eTrackingResult == vr::TrackingResult_Running_OK )
{
auto chaperoneDistances
= parent->chaperoneUtils().getDistancesToChaperone(
{ poseHmd.mDeviceToAbsoluteTracking.m[0][3],
poseHmd.mDeviceToAbsoluteTracking.m[1][3],
poseHmd.mDeviceToAbsoluteTracking.m[2][3] } );
// Autoturn mode
if ( RotationTabController::autoTurnEnabled() )
{
doAutoTurn( poseHmd, chaperoneDistances );
}
// Vestibular motion. Dependent on autoTurn so the playspace
// doesn't move when you use the keybind
if ( RotationTabController::vestibularMotionEnabled() )
{
doVestibularMotion( poseHmd, chaperoneDistances );
}
m_autoTurnChaperoneDistancesLast = std::move( chaperoneDistances );
}
}
}
void RotationTabController::doVestibularMotion(
const vr::TrackedDevicePose_t& poseHmd,
const std::vector<utils::ChaperoneQuadData>& chaperoneDistances )
{
if ( m_isHMDActive && poseHmd.bPoseIsValid && poseHmd.bDeviceIsConnected
&& poseHmd.eTrackingResult == vr::TrackingResult_Running_OK
&& !chaperoneDistances.empty() )
{
if ( chaperoneDistances.size()
!= m_autoTurnChaperoneDistancesLast.size() )
{
m_autoTurnChaperoneDistancesLast = chaperoneDistances;
m_autoTurnLastHmdUpdate = poseHmd.mDeviceToAbsoluteTracking;
}
// Find the nearest wall we're moving TOWARDS and rotate
// away from it Rotate dist/(2*pi*r) where r is
// m_autoTurnVestibularMotionRadius, as if we had walked
// however many inches along a circle and the world was
// turning to compensate
do
{
// find nearest wall we're moving towards
auto nearestWall = chaperoneDistances.end();
auto itrLast = m_autoTurnChaperoneDistancesLast.begin();
for ( auto itr = chaperoneDistances.begin();
itr != chaperoneDistances.end();
itr++ )
{
itrLast++;
if ( itr->distance > itrLast->distance )
{
continue;
}
if ( nearestWall == chaperoneDistances.end()
|| itr->distance < nearestWall->distance )
{
nearestWall = itr;
}
}
if ( nearestWall == chaperoneDistances.end() )
{
break;
}
// Convert pose matrix to quaternion
auto hmdQuaternion = quaternion::fromHmdMatrix34(
poseHmd.mDeviceToAbsoluteTracking );
// Get HMD raw yaw
double hmdYaw = quaternion::getYaw( hmdQuaternion );
// Get angle between HMD position and nearest point on
// wall
double hmdPositionToWallYaw = static_cast<double>(
std::atan2( poseHmd.mDeviceToAbsoluteTracking.m[0][3]
- nearestWall->nearestPoint.v[0],
poseHmd.mDeviceToAbsoluteTracking.m[2][3]
- nearestWall->nearestPoint.v[2] ) );
// Get angle between HMD and wall
double hmdToWallYaw
= reduceAngle<>( hmdYaw - hmdPositionToWallYaw, -M_PI, M_PI );
bool turnLeft = hmdToWallYaw > 0.0;
// Get the distance between previous and current
// position
double distanceChange = std::sqrt(
std::pow( poseHmd.mDeviceToAbsoluteTracking.m[0][3]
- m_autoTurnLastHmdUpdate.m[0][3],
2.0 )
+ std::pow( poseHmd.mDeviceToAbsoluteTracking.m[0][3]
- m_autoTurnLastHmdUpdate.m[0][3],
2.0 ) );
double rotationAmount
= ( distanceChange
/ ( 2.0 * M_PI
* RotationTabController::vestibularMotionRadius() ) )
* ( turnLeft ? 1 : -1 );
double newRotationAngleDeg
= std::fmod( parent->m_moveCenterTabController.rotation()
+ ( rotationAmount * k_radiansToCentidegrees )
+ m_autoTurnRoundingError,
360000.0 );
int newRotationAngleInt = static_cast<int>( newRotationAngleDeg );
m_autoTurnRoundingError = newRotationAngleDeg - newRotationAngleInt;
parent->m_moveCenterTabController.setRotation(
newRotationAngleInt );
} while ( false );
m_autoTurnLastHmdUpdate = poseHmd.mDeviceToAbsoluteTracking;
}
}
void RotationTabController::doAutoTurn(
const vr::TrackedDevicePose_t& poseHmd,
const std::vector<utils::ChaperoneQuadData>& chaperoneDistances )
{
if ( m_isHMDActive && poseHmd.bPoseIsValid && poseHmd.bDeviceIsConnected
&& poseHmd.eTrackingResult == vr::TrackingResult_Running_OK
&& !chaperoneDistances.empty() )
{
auto currentTime = std::chrono::steady_clock::now();
if ( m_autoTurnWallActive.size() != chaperoneDistances.size() )
{
// Chaperone changed
m_autoTurnWallActive.clear();
// Initialize all to 'true' so we don't rotate on startup if
// the user is outside of the play area.
m_autoTurnWallActive.resize( chaperoneDistances.size(), true );
m_autoTurnLastUpdate = currentTime;
}
// Apply smooth rotation per-frame: get the time since last frame,
// multiply by speed
if ( RotationTabController::autoTurnModeType()
== AutoTurnModes::LINEAR_SMOOTH_TURN
&& m_autoTurnLinearSmoothTurnRemaining != 0 )
{
// TODO: implement angular acceleration max?
auto deltaMillis
= std::chrono::duration_cast<std::chrono::milliseconds>(
currentTime - m_autoTurnLastUpdate )
.count();
auto miniDeltaAngle = static_cast<int>(
std::abs(
( deltaMillis * RotationTabController::autoTurnSpeed() )
/ 1000 )
* ( m_autoTurnLinearSmoothTurnRemaining < 0 ? -1 : 1 ) );
if ( std::abs( m_autoTurnLinearSmoothTurnRemaining )
< std::abs( miniDeltaAngle ) )
{
miniDeltaAngle = m_autoTurnLinearSmoothTurnRemaining;
}
int newRotationAngleDeg = reduceAngle<>(
static_cast<int>( parent->m_moveCenterTabController.rotation()
+ miniDeltaAngle ),
0,
36000 );
parent->m_moveCenterTabController.setRotation(
newRotationAngleDeg );
m_autoTurnLinearSmoothTurnRemaining -= miniDeltaAngle;
}
for ( size_t i = 0; i < chaperoneDistances.size(); i++ )
{
const auto& chaperoneQuad = chaperoneDistances[i];
if ( chaperoneQuad.distance
<= RotationTabController::autoTurnActivationDistance()
&& !m_autoTurnWallActive[i] )
{
// Convert pose matrix to quaternion
auto hmdQuaternion = quaternion::fromHmdMatrix34(
poseHmd.mDeviceToAbsoluteTracking );
// Get HMD raw yaw
double hmdYaw = quaternion::getYaw( hmdQuaternion );
// Get angle between HMD position and nearest point on
// wall
double hmdPositionToWallYaw = static_cast<double>(
std::atan2( poseHmd.mDeviceToAbsoluteTracking.m[0][3]
- chaperoneQuad.nearestPoint.v[0],
poseHmd.mDeviceToAbsoluteTracking.m[2][3]
- chaperoneQuad.nearestPoint.v[2] ) );
// Get angle between HMD and wall
double hmdToWallYaw = reduceAngle<>(
hmdYaw - hmdPositionToWallYaw, -M_PI, M_PI );
do
{
// Ignore if the wall we encountered is behind us
if ( std::abs( hmdToWallYaw ) >= M_PI / 2 )
{
LOG( DEBUG ) << "Ignoring turn in opposite "
"direction (angle "
<< std::abs( hmdToWallYaw ) << ")";
break;
}
// If the closest corner shares a wall with the last
// wall we turned at, turn relative to that corner
bool cornerShared
= m_autoTurnWallActive[circularIndex(
i, true, chaperoneDistances.size() )]
|| m_autoTurnWallActive[circularIndex(
i, false, chaperoneDistances.size() )];
bool turnLeft = true;
// Turn away from corner
if ( cornerShared )
{
// Turn left or right depending on which corner
// it is. If we go based on yaw, we could end up
// turning the wrong way if it's large obtuse
// angle and we're facing more towards the
// previous wall than the left.
turnLeft = m_autoTurnWallActive[circularIndex(
i, false, chaperoneDistances.size() )];
LOG( DEBUG ) << "turning away from shared corner";
}
// If within m_cordDetanglingAngle degrees of
// 'straight at a wall', start in whatever direction
// will start untangling your cord
else if ( ( std::abs( hmdToWallYaw )
<= RotationTabController::cordDetangleAngle() )
&& ( std::abs( parent->m_moveCenterTabController
.getHmdYawTotal() )
> RotationTabController::minCordTangle() ) )
{
turnLeft = ( parent->m_moveCenterTabController
.getHmdYawTotal()
< 0.0 );
LOG( DEBUG ) << "turning to detangle cord";
}
// Turn the closest angle to the wall
else
{
turnLeft = hmdToWallYaw > 0.0;
LOG( DEBUG ) << "turning closest angle to wall";
}
LOG( DEBUG ) << "hmd yaw " << hmdYaw
<< ", hmd position to wall angle "
<< hmdPositionToWallYaw;
LOG( DEBUG ) << "hmd to wall angle " << hmdToWallYaw;
// Positive hmd-to-wall is facing left, negative is
// facing right (relative to the wall)
double delta_degrees
= ( ( turnLeft ? M_PI / 2 : -M_PI / 2 ) - hmdToWallYaw )
* k_radiansToCentidegrees;
if ( RotationTabController::autoTurnUseCornerAngle()
&& cornerShared )
{
// Turn the angle of the corner
// Three relevant points - the far corner wall
// we're currently touching, the far corner on
// the wall we've just touched, and the corner
// between them
const size_t cornerCnt
= parent->chaperoneUtils().quadsCount();
const size_t middleCornerIdx
= turnLeft ? i
: circularIndex( i, true, cornerCnt );
const auto& middleCorner
= parent->chaperoneUtils().getCorner(
middleCornerIdx );
const auto& newWallCorner
= parent->chaperoneUtils().getCorner( circularIndex(
middleCornerIdx, turnLeft, cornerCnt ) );
const auto& touchingWallCorner
= parent->chaperoneUtils().getCorner( circularIndex(
middleCornerIdx, !turnLeft, cornerCnt ) );
double newWallAngle = static_cast<double>( std::atan2(
middleCorner.v[0] - newWallCorner.v[0],
middleCorner.v[2] - newWallCorner.v[2] ) );
double touchingWallAngle
= static_cast<double>( std::atan2(
middleCorner.v[0] - touchingWallCorner.v[0],
middleCorner.v[2] - touchingWallCorner.v[2] ) );
LOG( DEBUG ) << "twa: " << touchingWallAngle
<< ", nwa: " << newWallAngle << ", diff: "
<< ( newWallAngle - touchingWallAngle );
delta_degrees = ( newWallAngle - touchingWallAngle
+ ( turnLeft ? M_PI : -M_PI ) )
* k_radiansToCentidegrees;
delta_degrees
= reduceAngle<>( delta_degrees, -18000.0, 18000.0 );
}
LOG( DEBUG ) << "rotating space " << ( delta_degrees / 100 )
<< " degrees";
switch ( RotationTabController::autoTurnModeType() )
{
case AutoTurnModes::SNAP:
parent->m_moveCenterTabController.setRotation(
static_cast<int>(
parent->m_moveCenterTabController.rotation()
+ delta_degrees ) );
break;
case AutoTurnModes::LINEAR_SMOOTH_TURN:
m_autoTurnLinearSmoothTurnRemaining
+= static_cast<int>( delta_degrees );
break;
}
} while ( false );
m_autoTurnWallActive[i] = true;
}
else if ( ( chaperoneQuad.distance
> ( RotationTabController::autoTurnActivationDistance()
+ RotationTabController::
autoTurnDeactivationDistance() ) )
&& m_autoTurnWallActive[i] )
{
m_autoTurnWallActive[i] = false;
}
}
m_autoTurnLastUpdate = currentTime;
}
else
{
m_autoTurnWallActive.clear();
}
}
// getters
bool RotationTabController::autoTurnEnabled() const
{
return settings::getSetting(
settings::BoolSetting::ROTATION_autoturnEnabled );
}
float RotationTabController::autoTurnActivationDistance() const
{
return static_cast<float>( settings::getSetting(
settings::DoubleSetting::ROTATION_activationDistance ) );
}
float RotationTabController::autoTurnDeactivationDistance() const
{
return static_cast<float>( settings::getSetting(
settings::DoubleSetting::ROTATION_deactivateDistance ) );
}
bool RotationTabController::autoTurnUseCornerAngle() const
{
return settings::getSetting(
settings::BoolSetting::ROTATION_autoturnUseCornerAngle );
}
double RotationTabController::cordDetangleAngle() const
{
return settings::getSetting(
settings::DoubleSetting::ROTATION_cordDetanglingAngle );
}
double RotationTabController::minCordTangle() const
{
return settings::getSetting(
settings::DoubleSetting::ROTATION_autoturnMinCordTangle );
}
int RotationTabController::autoTurnSpeed() const
{
return settings::getSetting(
settings::IntSetting::ROTATION_autoturnLinearTurnSpeed );
}
AutoTurnModes RotationTabController::autoTurnModeType() const
{
return static_cast<AutoTurnModes>(
settings::getSetting( settings::IntSetting::ROTATION_autoturnMode ) );
}
int RotationTabController::autoTurnMode() const
{
return settings::getSetting( settings::IntSetting::ROTATION_autoturnMode );
}
bool RotationTabController::vestibularMotionEnabled() const
{
return settings::getSetting(
settings::BoolSetting::ROTATION_autoturnVestibularMotionEnabled );
}
double RotationTabController::vestibularMotionRadius() const
{
return settings::getSetting(
settings::DoubleSetting::ROTATION_autoturnVestibularMotionRadius );
}
// Setters
void RotationTabController::setAutoTurnEnabled( bool value, bool notify )
{
settings::setSetting( settings::BoolSetting::ROTATION_autoturnEnabled,
value );
if ( notify )
{
emit autoTurnEnabledChanged( value );
}
}
void RotationTabController::setAutoTurnActivationDistance( float value,
bool notify )
{
settings::setSetting( settings::DoubleSetting::ROTATION_activationDistance,
static_cast<double>( value ) );
if ( notify )
{
emit autoTurnActivationDistanceChanged( value );
}
}
void RotationTabController::setAutoTurnDeactivationDistance( float value,
bool notify )
{
settings::setSetting( settings::DoubleSetting::ROTATION_deactivateDistance,
static_cast<double>( value ) );
if ( notify )
{
emit autoTurnDeactivationDistanceChanged( value );
}
}
void RotationTabController::setAutoTurnUseCornerAngle( bool value, bool notify )
{
settings::setSetting(
settings::BoolSetting::ROTATION_autoturnUseCornerAngle, value );
if ( notify )
{
emit autoTurnUseCornerAngleChanged( value );
}
}
void RotationTabController::setCordDetangleAngle( double value, bool notify )
{
settings::setSetting( settings::DoubleSetting::ROTATION_cordDetanglingAngle,
value );
if ( notify )
{
emit cordDetangleAngleChanged( value );
}
}
void RotationTabController::setMinCordTangle( double value, bool notify )
{
settings::setSetting(
settings::DoubleSetting::ROTATION_autoturnMinCordTangle, value );
if ( notify )
{
emit minCordTangleChanged( value );
}
}
void RotationTabController::setAutoTurnSpeed( int value, bool notify )
{
settings::setSetting(
settings::IntSetting::ROTATION_autoturnLinearTurnSpeed, value );
if ( notify )
{
emit autoTurnSpeedChanged( value );
}
}
void RotationTabController::setAutoTurnMode( int value, bool notify )
{
settings::setSetting( settings::IntSetting::ROTATION_autoturnMode,
static_cast<int>( value ) );
if ( notify )
{
emit autoTurnModeChanged( value );
}
}
void RotationTabController::setVestibularMotionEnabled( bool value,
bool notify )
{
settings::setSetting(
settings::BoolSetting::ROTATION_autoturnVestibularMotionEnabled,
value );
if ( notify )
{
emit vestibularMotionEnabledChanged( value );
}
}
void RotationTabController::setVestibularMotionRadius( double value,
bool notify )
{
settings::setSetting(
settings::DoubleSetting::ROTATION_autoturnVestibularMotionRadius,
value );
if ( notify )
{
emit vestibularMotionRadiusChanged( value );
}
}
} // namespace advsettings