1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-01 07:06:43 +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) : mBindingsManager(bindingsManager)
, mActionManager(actionManager) , mActionManager(actionManager)
, mMouseManager(mouseManager) , mMouseManager(mouseManager)
, mGyroAvailable(false)
, mJoystickEnabled (Settings::Manager::getBool("enable controller", "Input")) , mJoystickEnabled (Settings::Manager::getBool("enable controller", "Input"))
, mGyroAvailable(false)
, mGamepadCursorSpeed(Settings::Manager::getFloat("gamepad cursor speed", "Input")) , mGamepadCursorSpeed(Settings::Manager::getFloat("gamepad cursor speed", "Input"))
, mSneakToggleShortcutTimer(0.f) , mSneakToggleShortcutTimer(0.f)
, mGamepadGuiCursorEnabled(true) , mGamepadGuiCursorEnabled(true)

View file

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

View file

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

View file

@ -11,10 +11,9 @@
namespace MWInput namespace MWInput
{ {
SensorManager::SensorManager() SensorManager::SensorManager()
: mGyroValues() : mRotation()
, mGyroValues()
, mGyroUpdateTimer(0.f) , mGyroUpdateTimer(0.f)
, mGyroHAxis(GyroscopeAxis::Minus_X)
, mGyroVAxis(GyroscopeAxis::Y)
, mGyroscope(nullptr) , mGyroscope(nullptr)
, mGuiCursorEnabled(true) , mGuiCursorEnabled(true)
{ {
@ -44,40 +43,36 @@ namespace MWInput
// Treat setting from config as axes for landscape mode. // Treat setting from config as axes for landscape mode.
// If the device does not support orientation change, do nothing. // 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. // 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")); SDL_DisplayOrientation currentOrientation = SDL_GetDisplayOrientation(Settings::Manager::getInt("screen", "Video"));
switch (currentOrientation) switch (currentOrientation)
{ {
case SDL_ORIENTATION_UNKNOWN: case SDL_ORIENTATION_UNKNOWN:
return; break;
case SDL_ORIENTATION_LANDSCAPE: case SDL_ORIENTATION_LANDSCAPE:
break; break;
case SDL_ORIENTATION_LANDSCAPE_FLIPPED: case SDL_ORIENTATION_LANDSCAPE_FLIPPED:
{ {
mGyroHAxis = GyroscopeAxis(-mGyroHAxis); angle = osg::PIf;
mGyroVAxis = GyroscopeAxis(-mGyroVAxis);
break; break;
} }
case SDL_ORIENTATION_PORTRAIT: case SDL_ORIENTATION_PORTRAIT:
{ {
GyroscopeAxis oldVAxis = mGyroVAxis; angle = -0.5 * osg::PIf;
mGyroVAxis = mGyroHAxis;
mGyroHAxis = GyroscopeAxis(-oldVAxis);
break; break;
} }
case SDL_ORIENTATION_PORTRAIT_FLIPPED: case SDL_ORIENTATION_PORTRAIT_FLIPPED:
{ {
GyroscopeAxis oldVAxis = mGyroVAxis; angle = 0.5 * osg::PIf;
mGyroVAxis = GyroscopeAxis(-mGyroHAxis);
mGyroHAxis = oldVAxis;
break; break;
} }
} }
mRotation.makeRotate(angle, osg::Vec3f(0, 0, 1));
} }
void SensorManager::updateSensors() void SensorManager::updateSensors()
@ -127,12 +122,6 @@ namespace MWInput
{ {
if (setting.first == "Input" && setting.second == "enable gyroscope") if (setting.first == "Input" && setting.second == "enable gyroscope")
init(); 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; break;
case SDL_SENSOR_GYRO: case SDL_SENSOR_GYRO:
{ {
mGyroValues[0] = arg.data[0]; osg::Vec3f gyro(arg.data[0], arg.data[1], arg.data[2]);
mGyroValues[1] = arg.data[1]; mGyroValues = mRotation * gyro;
mGyroValues[2] = arg.data[2];
mGyroUpdateTimer = 0.f; mGyroUpdateTimer = 0.f;
break; break;
} }
default: default:
@ -179,7 +166,7 @@ namespace MWInput
// More than half of second passed since the last gyroscope update. // More than half of second passed since the last gyroscope update.
// A device more likely was disconnected or switched to the sleep mode. // A device more likely was disconnected or switched to the sleep mode.
// Reset current rotation speed and wait for update. // Reset current rotation speed and wait for update.
mGyroValues = { 0, 0, 0 }; mGyroValues = osg::Vec3f();
mGyroUpdateTimer = 0.f; mGyroUpdateTimer = 0.f;
} }
} }
@ -191,6 +178,6 @@ namespace MWInput
std::array<float, 3> SensorManager::getGyroValues() const 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 <SDL_sensor.h>
#include <osg/Matrixf>
#include <osg/Vec3f>
#include <components/settings/settings.hpp> #include <components/settings/settings.hpp>
#include <components/sdlutil/events.hpp> #include <components/sdlutil/events.hpp>
#include "gyroaxis.hpp"
namespace SDLUtil namespace SDLUtil
{ {
class InputWrapper; class InputWrapper;
@ -45,12 +46,10 @@ namespace MWInput
void updateSensors(); void updateSensors();
void correctGyroscopeAxes(); void correctGyroscopeAxes();
std::array<float, 3> mGyroValues; osg::Matrixf mRotation;
osg::Vec3f mGyroValues;
float mGyroUpdateTimer; float mGyroUpdateTimer;
GyroscopeAxis mGyroHAxis;
GyroscopeAxis mGyroVAxis;
SDL_Sensor* mGyroscope; SDL_Sensor* mGyroscope;
bool mGuiCursorEnabled; bool mGuiCursorEnabled;

View file

@ -540,7 +540,7 @@ gyro horizontal axis = -x
gyro vertical axis = y gyro vertical axis = y
# The minimum gyroscope movement that is able to rotate the camera. # 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. # Horizontal camera axis sensitivity to gyroscope movement.
gyro horizontal sensitivity = 1.0 gyro horizontal sensitivity = 1.0