From 093903e6a6557c07a44bdcd6dc0b8562f7c27cd0 Mon Sep 17 00:00:00 2001 From: Lubos Date: Sat, 28 Jan 2023 13:08:27 +0100 Subject: [PATCH] OpenXR - Support head rotations in 2D --- Common/VR/PPSSPPVR.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index 5d8d26e734..f8f7eda35f 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -104,6 +104,8 @@ static std::vector controllerMapping[2] = { }; static bool controllerMotion[2][5] = {}; static bool hmdMotion[4] = {}; +static float hmdMotionLast[2] = {}; +static float hmdMotionDiff[2] = {}; static int mouseController = 1; static bool mousePressed = false; @@ -351,6 +353,61 @@ void UpdateVRInput(bool haptics, float dp_xscale, float dp_yscale) { hmdMotion[3] = activate; } + // Head control in VR mode + if (g_Config.iHeadRotation && !IsFlatVRScene()) { + + // calculate delta angles of the rotation + float pitch = -VR_GetHMDAngles().x; + float yaw = -VR_GetHMDAngles().y; + float deltaPitch = pitch - hmdMotionLast[0]; + float deltaYaw = yaw - hmdMotionLast[1]; + while (deltaYaw >= 180) deltaYaw -= 360; + while (deltaYaw < -180) deltaYaw += 360; + hmdMotionLast[0] = pitch; + hmdMotionLast[1] = yaw; + hmdMotionDiff[0] += deltaPitch * g_Config.fHeadRotationScale; + hmdMotionDiff[1] += deltaYaw * g_Config.fHeadRotationScale; + + bool activate; + float limit = 1; + keyInput.deviceId = DEVICE_ID_XR_HMD; + + // vertical rotations + if (g_Config.iHeadRotation == 2) { + //up + activate = hmdMotionDiff[0] > limit; + keyInput.flags = activate ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_ROTATION_UP; + if (hmdMotion[0] != activate) NativeKey(keyInput); + if (activate) hmdMotionDiff[0] -= limit; + hmdMotion[0] = activate; + + //down + activate = hmdMotionDiff[0] < -limit; + keyInput.flags = activate ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_ROTATION_DOWN; + if (hmdMotion[1] != activate) NativeKey(keyInput); + if (activate) hmdMotionDiff[0] += limit; + hmdMotion[1] = activate; + } + + //left + activate = hmdMotionDiff[1] < -limit; + keyInput.flags = activate ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_ROTATION_LEFT; + if (hmdMotion[2] != activate) NativeKey(keyInput); + if (activate) hmdMotionDiff[1] += limit; + hmdMotion[2] = activate; + + //right + activate = hmdMotionDiff[1] > limit; + keyInput.flags = activate ? KEY_DOWN : KEY_UP; + keyInput.keyCode = NKCODE_EXT_ROTATION_RIGHT; + if (hmdMotion[3] != activate) NativeKey(keyInput); + if (activate) hmdMotionDiff[1] -= limit; + hmdMotion[3] = activate; + } + // Camera adjust if (pspKeys[VIRTKEY_VR_CAMERA_ADJUST]) { for (auto& device : pspAxis) {