Add friction feature to motion tab

#344
This commit is contained in:
Kung-vr
2020-01-03 01:25:49 +09:00
parent 5a7ad06b1c
commit d8f1f4e6b9
5 changed files with 120 additions and 24 deletions
+66 -16
View File
@@ -464,6 +464,12 @@ int MoveCenterTabController::smoothTurnRate() const
settings::IntSetting::PLAYSPACE_smoothTurnRate );
}
int MoveCenterTabController::frictionPercent() const
{
return settings::getSetting(
settings::IntSetting::PLAYSPACE_frictionPercent );
}
void MoveCenterTabController::setSmoothTurnRate( int value, bool notify )
{
settings::setSetting( settings::IntSetting::PLAYSPACE_smoothTurnRate,
@@ -475,6 +481,17 @@ void MoveCenterTabController::setSmoothTurnRate( int value, bool notify )
}
}
void MoveCenterTabController::setFrictionPercent( int value, bool notify )
{
settings::setSetting( settings::IntSetting::PLAYSPACE_frictionPercent,
value );
if ( notify )
{
emit frictionPercentChanged( value );
}
}
bool MoveCenterTabController::adjustChaperone() const
{
return settings::getSetting(
@@ -1929,12 +1946,6 @@ void MoveCenterTabController::gravityToggleAction(
void MoveCenterTabController::gravityReverseAction( bool gravityReverseHeld )
{
// detect new press or new release
if ( ( gravityReverseHeld && !m_gravityReversed )
|| ( !gravityReverseHeld && m_gravityReversed ) )
{
setGravityStrength( gravityStrength() * -1.0f );
}
m_gravityReversed = gravityReverseHeld;
}
@@ -2374,6 +2385,50 @@ void MoveCenterTabController::updateHandTurn(
void MoveCenterTabController::updateGravity()
{
float currentGravity = gravityStrength();
if ( m_gravityReversed )
{
currentGravity *= -1.0f;
}
double secondsSinceLastGravityUpdate
= std::chrono::duration<double>( std::chrono::steady_clock::now()
- m_lastGravityUpdateTimePoint )
.count();
// apply friction
if ( frictionPercent() > 0 )
{
// if we have friction and all velocity components are less than
// halt speed, we stop.
if ( abs( m_velocity[0] ) < k_frictionHalt_mps
&& abs( m_velocity[1] ) < k_frictionHalt_mps
&& abs( m_velocity[2] ) < k_frictionHalt_mps )
{
m_velocity[0] = 0.0;
m_velocity[1] = 0.0;
m_velocity[2] = 0.0;
}
else
{
// frictionPercent() * 10 is scaler such that at 90fps we
// reduce from terminal velocity (50m/s) to about 1mm/s in 1
// second using 100% friction. This is an arbitrary scaler,
// but doing a more accurate job would require a performance
// hit of the math pow() function to an arbitrary exponent.
double frictionVelocityMultiplier
= ( 100.0
- ( static_cast<double>( frictionPercent() * 10 )
* secondsSinceLastGravityUpdate ) )
/ 100.0;
frictionVelocityMultiplier
= std::clamp( frictionVelocityMultiplier, 0.0, 1.0 );
m_velocity[0] *= frictionVelocityMultiplier;
m_velocity[1] *= frictionVelocityMultiplier;
m_velocity[2] *= frictionVelocityMultiplier;
}
}
// prevent velocity underflow
if ( std::isnan( m_velocity[0] ) )
{
@@ -2405,23 +2460,18 @@ void MoveCenterTabController::updateGravity()
m_velocity[2] = 0.0;
}
double secondsSinceLastGravityUpdate
= std::chrono::duration<double>( std::chrono::steady_clock::now()
- m_lastGravityUpdateTimePoint )
.count();
// are we falling?
// note: up is negative y
// note: set to always fall if gravity reversed (strength < 0)
if ( ( m_offsetY < m_gravityFloor ) || ( gravityStrength() < 0 ) )
// note: set to always fall if gravity reversed (currentGravity < 0)
if ( ( m_offsetY < m_gravityFloor ) || ( currentGravity < 0 ) )
{
// check if we're about to land
// note: we don't land if gravity is reversed (strength < 0)
// note: we don't land if gravity is reversed (currentGravity < 0)
if ( ( m_offsetY
+ static_cast<float>( m_velocity[1]
* secondsSinceLastGravityUpdate )
>= m_gravityFloor )
&& gravityStrength() >= 0 )
&& currentGravity >= 0 )
{
// get ratio of how much from y velocity applied to overcome y
// offset and get down to ground.
@@ -2462,7 +2512,7 @@ void MoveCenterTabController::updateGravity()
// accelerate downward velocity for the next update
// note: downward is positive y
m_velocity[1] = m_velocity[1]
+ ( static_cast<double>( gravityStrength() )
+ ( static_cast<double>( currentGravity )
* secondsSinceLastGravityUpdate );
return;
}