OpenXR - Map controller motions on keys

This commit is contained in:
Lubos 2022-10-31 11:59:07 +01:00
parent cf27d14131
commit b740bfbcfb
3 changed files with 66 additions and 1 deletions

View File

@ -261,7 +261,14 @@ typedef enum _keycode_t {
NKCODE_EXT_MOUSEBUTTON_4 = 1006,
NKCODE_EXT_MOUSEBUTTON_5 = 1007,
NKCODE_EXT_MOUSEWHEEL_UP = 1008,
NKCODE_EXT_MOUSEWHEEL_DOWN = 1009
NKCODE_EXT_MOUSEWHEEL_DOWN = 1009,
// Virtual reality controller motion
NKCODE_EXT_MOTION_UP = 1101,
NKCODE_EXT_MOTION_DOWN = 1102,
NKCODE_EXT_MOTION_LEFT = 1103,
NKCODE_EXT_MOTION_RIGHT = 1104,
NKCODE_EXT_MOTION_FORWARD = 1105,
} keycode_t;
enum AndroidJoystickAxis {

View File

@ -78,6 +78,7 @@ static std::vector<ButtonMapping> controllerMapping[2] = {
leftControllerMapping,
rightControllerMapping
};
static bool controllerMotion[2][5];
static int mouseController = -1;
static bool mousePressed[] = {false, false};
@ -203,6 +204,56 @@ void UpdateVRInput(bool(*NativeKey)(const KeyInput &key), bool(*NativeTouch)(con
}
}
//motion control
bool activate;
for (int j = 0; j < 2; j++) {
float limit = 0.5f; //length of needed movement in meters
XrVector3f axis = {0, 1, 0};
float center = ToRadians((float)VR_GetConfig(VR_CONFIG_MENU_YAW));
XrQuaternionf orientation = XrQuaternionf_CreateFromVectorAngle(axis, center);
XrVector3f position = XrQuaternionf_Rotate(orientation, IN_VRGetPose(j).position);
//up
activate = position.y > limit;
keyInput.flags = activate ? KEY_DOWN : KEY_UP;
keyInput.keyCode = NKCODE_EXT_MOTION_UP;
keyInput.deviceId = controllerIds[j];
if (controllerMotion[j][0] != activate) NativeKey(keyInput);
controllerMotion[j][0] = activate;
//down
activate = position.y < -limit * 1.5f;
keyInput.flags = activate ? KEY_DOWN : KEY_UP;
keyInput.keyCode = NKCODE_EXT_MOTION_DOWN;
keyInput.deviceId = controllerIds[j];
if (controllerMotion[j][1] != activate) NativeKey(keyInput);
controllerMotion[j][1] = activate;
//left
activate = position.x < -limit * (j == 0 ? 1.0f : 0.25f);
keyInput.flags = activate ? KEY_DOWN : KEY_UP;
keyInput.keyCode = NKCODE_EXT_MOTION_LEFT;
keyInput.deviceId = controllerIds[j];
if (controllerMotion[j][2] != activate) NativeKey(keyInput);
controllerMotion[j][2] = activate;
//right
activate = position.x > limit * (j == 1 ? 1.0f : 0.25f);
keyInput.flags = activate ? KEY_DOWN : KEY_UP;
keyInput.keyCode = NKCODE_EXT_MOTION_RIGHT;
keyInput.deviceId = controllerIds[j];
if (controllerMotion[j][3] != activate) NativeKey(keyInput);
controllerMotion[j][3] = activate;
//forward
activate = position.z < -limit;
keyInput.flags = activate ? KEY_DOWN : KEY_UP;
keyInput.keyCode = NKCODE_EXT_MOTION_FORWARD;
keyInput.deviceId = controllerIds[j];
if (controllerMotion[j][4] != activate) NativeKey(keyInput);
controllerMotion[j][4] = activate;
}
//mouse cursor
if (mouseController >= 0) {
//get position on screen

View File

@ -304,6 +304,13 @@ static const KeyMap_IntStrPair key_names[] = {
{NKCODE_EXT_MOUSEWHEEL_UP, "MWheelU"},
{NKCODE_EXT_MOUSEWHEEL_DOWN, "MWheelD"},
{NKCODE_EXT_MOTION_UP, "MotionUp"},
{NKCODE_EXT_MOTION_DOWN, "MotionDown"},
{NKCODE_EXT_MOTION_LEFT, "MotionLeft"},
{NKCODE_EXT_MOTION_RIGHT, "MotionRight"},
{NKCODE_EXT_MOTION_FORWARD, "MotionFwd"},
{NKCODE_START_QUESTION, "¿"},
{NKCODE_LEFTBRACE, "{"},
{NKCODE_RIGHTBRACE, "}"},