1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-13 12:40:04 +00:00

Refactor sensor manager axis correction

This commit is contained in:
uramer 2022-01-19 17:01:43 +01:00
parent a496f16cdb
commit 4021d23cff
6 changed files with 31 additions and 59 deletions

View File

@ -33,8 +33,8 @@ namespace MWInput
: mBindingsManager(bindingsManager)
, mActionManager(actionManager)
, mMouseManager(mouseManager)
, mGyroAvailable(false)
, mJoystickEnabled (Settings::Manager::getBool("enable controller", "Input"))
, mGyroAvailable(false)
, mGamepadCursorSpeed(Settings::Manager::getFloat("gamepad cursor speed", "Input"))
, mSneakToggleShortcutTimer(0.f)
, mGamepadGuiCursorEnabled(true)

View File

@ -12,8 +12,6 @@ namespace MWInput
, mGuiCursorEnabled(true)
, mSensitivityH(Settings::Manager::getFloat("gyro horizontal sensitivity", "Input"))
, mSensitivityV(Settings::Manager::getFloat("gyro vertical sensitivity", "Input"))
, mInvertH(Settings::Manager::getBool("invert x axis", "Input"))
, mInvertV(Settings::Manager::getBool("invert y axis", "Input"))
, mInputThreshold(Settings::Manager::getFloat("gyro input threshold", "Input"))
, mAxisH(gyroscopeAxisFromString(Settings::Manager::getString("gyro horizontal axis", "Input")))
, mAxisV(gyroscopeAxisFromString(Settings::Manager::getString("gyro vertical axis", "Input")))
@ -26,13 +24,13 @@ namespace MWInput
float gyroH = getAxisValue(mAxisH, values);
float gyroV = getAxisValue(mAxisV, values);
if (gyroH == 0 && gyroV == 0)
if (gyroH == 0.f && gyroV == 0.f)
return;
float rot[3];
rot[0] = -gyroV * dt * mSensitivityV * 4 * (mInvertV ? -1 : 1);
rot[0] = -gyroV * dt * mSensitivityV;
rot[1] = 0.0f;
rot[2] = -gyroH * dt * mSensitivityH * 4 * (mInvertH ? -1 : 1);
rot[2] = -gyroH * dt * mSensitivityH;
// Only actually turn player when we're not in vanity mode
bool playerLooking = MWBase::Environment::get().getInputManager()->getControlSwitch("playerlooking");
@ -62,10 +60,6 @@ namespace MWInput
mSensitivityH = Settings::Manager::getFloat("gyro horizontal sensitivity", "Input");
else if (setting.second == "gyro vertical sensitivity")
mSensitivityV = Settings::Manager::getFloat("gyro vertical sensitivity", "Input");
else if (setting.second == "invert x axis")
mInvertH = Settings::Manager::getBool("invert x axis", "Input");
else if (setting.second == "invert y axis")
mInvertV = Settings::Manager::getBool("invert y axis", "Input");
else if (setting.second == "gyro input threshold")
mInputThreshold = Settings::Manager::getFloat("gyro input threshold", "Input");
else if (setting.second == "gyro horizontal axis")
@ -75,21 +69,15 @@ namespace MWInput
}
}
namespace
{
int signum(int x)
{
return 0 < x - x < 0;
}
}
float GyroManager::getAxisValue(GyroscopeAxis axis, std::array<float, 3> values) const
{
if (axis == GyroscopeAxis::Unknown)
return 0;
float value = values[std::abs(axis) - 1] * signum(axis);
//if (std::abs(value) <= mInputThreshold)
// value = 0;
float value = values[std::abs(axis) - 1];
if (axis < 0)
value *= -1;
if (std::abs(value) <= mInputThreshold)
value = 0;
return value;
}
}

View File

@ -25,8 +25,6 @@ namespace MWInput
bool mGuiCursorEnabled;
float mSensitivityH;
float mSensitivityV;
bool mInvertH;
bool mInvertV;
float mInputThreshold;
GyroscopeAxis mAxisH;
GyroscopeAxis mAxisV;

View File

@ -11,10 +11,9 @@
namespace MWInput
{
SensorManager::SensorManager()
: mGyroValues()
: mRotation()
, mGyroValues()
, mGyroUpdateTimer(0.f)
, mGyroHAxis(GyroscopeAxis::Minus_X)
, mGyroVAxis(GyroscopeAxis::Y)
, mGyroscope(nullptr)
, mGuiCursorEnabled(true)
{
@ -44,40 +43,36 @@ namespace MWInput
// Treat setting from config as axes for landscape mode.
// If the device does not support orientation change, do nothing.
// Note: in is unclear how to correct axes for devices with non-standart Z axis direction.
mGyroHAxis = gyroscopeAxisFromString(Settings::Manager::getString("gyro horizontal axis", "Input"));
mGyroVAxis = gyroscopeAxisFromString(Settings::Manager::getString("gyro vertical axis", "Input"));
mRotation = osg::Matrixf::identity();
float angle = 0;
SDL_DisplayOrientation currentOrientation = SDL_GetDisplayOrientation(Settings::Manager::getInt("screen", "Video"));
switch (currentOrientation)
{
case SDL_ORIENTATION_UNKNOWN:
return;
break;
case SDL_ORIENTATION_LANDSCAPE:
break;
case SDL_ORIENTATION_LANDSCAPE_FLIPPED:
{
mGyroHAxis = GyroscopeAxis(-mGyroHAxis);
mGyroVAxis = GyroscopeAxis(-mGyroVAxis);
angle = osg::PIf;
break;
}
case SDL_ORIENTATION_PORTRAIT:
{
GyroscopeAxis oldVAxis = mGyroVAxis;
mGyroVAxis = mGyroHAxis;
mGyroHAxis = GyroscopeAxis(-oldVAxis);
angle = -0.5 * osg::PIf;
break;
}
case SDL_ORIENTATION_PORTRAIT_FLIPPED:
{
GyroscopeAxis oldVAxis = mGyroVAxis;
mGyroVAxis = GyroscopeAxis(-mGyroHAxis);
mGyroHAxis = oldVAxis;
angle = 0.5 * osg::PIf;
break;
}
}
mRotation.makeRotate(angle, osg::Vec3f(0, 0, 1));
}
void SensorManager::updateSensors()
@ -127,12 +122,6 @@ namespace MWInput
{
if (setting.first == "Input" && setting.second == "enable gyroscope")
init();
if (setting.first == "Input" && setting.second == "gyro horizontal axis")
correctGyroscopeAxes();
if (setting.first == "Input" && setting.second == "gyro vertical axis")
correctGyroscopeAxes();
}
}
@ -159,11 +148,9 @@ namespace MWInput
break;
case SDL_SENSOR_GYRO:
{
mGyroValues[0] = arg.data[0];
mGyroValues[1] = arg.data[1];
mGyroValues[2] = arg.data[2];
osg::Vec3f gyro(arg.data[0], arg.data[1], arg.data[2]);
mGyroValues = mRotation * gyro;
mGyroUpdateTimer = 0.f;
break;
}
default:
@ -179,7 +166,7 @@ namespace MWInput
// More than half of second passed since the last gyroscope update.
// A device more likely was disconnected or switched to the sleep mode.
// Reset current rotation speed and wait for update.
mGyroValues = { 0, 0, 0 };
mGyroValues = osg::Vec3f();
mGyroUpdateTimer = 0.f;
}
}
@ -191,6 +178,6 @@ namespace MWInput
std::array<float, 3> SensorManager::getGyroValues() const
{
return mGyroValues;
return { mGyroValues.x(), mGyroValues.y(), mGyroValues.z() };
}
}

View File

@ -3,11 +3,12 @@
#include <SDL_sensor.h>
#include <osg/Matrixf>
#include <osg/Vec3f>
#include <components/settings/settings.hpp>
#include <components/sdlutil/events.hpp>
#include "gyroaxis.hpp"
namespace SDLUtil
{
class InputWrapper;
@ -45,12 +46,10 @@ namespace MWInput
void updateSensors();
void correctGyroscopeAxes();
std::array<float, 3> mGyroValues;
osg::Matrixf mRotation;
osg::Vec3f mGyroValues;
float mGyroUpdateTimer;
GyroscopeAxis mGyroHAxis;
GyroscopeAxis mGyroVAxis;
SDL_Sensor* mGyroscope;
bool mGuiCursorEnabled;

View File

@ -540,7 +540,7 @@ gyro horizontal axis = -x
gyro vertical axis = y
# The minimum gyroscope movement that is able to rotate the camera.
gyro input threshold = 0.01
gyro input threshold = 0
# Horizontal camera axis sensitivity to gyroscope movement.
gyro horizontal sensitivity = 1.0