From 46e835f2d6531baea061a2723d171a2f5a1abf6a Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Fri, 5 May 2023 12:29:26 -0600 Subject: yuzu: Add motion preview to controller input --- src/core/hid/emulated_controller.cpp | 6 ++---- src/core/hid/emulated_controller.h | 1 + src/core/hid/motion_input.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/core/hid/motion_input.h | 2 ++ 4 files changed, 41 insertions(+), 4 deletions(-) (limited to 'src/core/hid') diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index a70f8807c..a5c2e3db8 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -376,6 +376,7 @@ void EmulatedController::ReloadInput() { motion.accel = emulated_motion.GetAcceleration(); motion.gyro = emulated_motion.GetGyroscope(); motion.rotation = emulated_motion.GetRotations(); + motion.euler = emulated_motion.GetEulerAngles(); motion.orientation = emulated_motion.GetOrientation(); motion.is_at_rest = !emulated_motion.IsMoving(motion_sensitivity); } @@ -976,14 +977,11 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback emulated.UpdateOrientation(raw_status.delta_timestamp); force_update_motion = raw_status.force_update; - if (is_configuring) { - return; - } - auto& motion = controller.motion_state[index]; motion.accel = emulated.GetAcceleration(); motion.gyro = emulated.GetGyroscope(); motion.rotation = emulated.GetRotations(); + motion.euler = emulated.GetEulerAngles(); motion.orientation = emulated.GetOrientation(); motion.is_at_rest = !emulated.IsMoving(motion_sensitivity); } diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index 429655355..6e01f4e12 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -106,6 +106,7 @@ struct ControllerMotion { Common::Vec3f accel{}; Common::Vec3f gyro{}; Common::Vec3f rotation{}; + Common::Vec3f euler{}; std::array orientation{}; bool is_at_rest{}; }; diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp index 0dd66c1cc..b60478dbb 100644 --- a/src/core/hid/motion_input.cpp +++ b/src/core/hid/motion_input.cpp @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include + #include "common/math_util.h" #include "core/hid/motion_input.h" @@ -51,6 +53,20 @@ void MotionInput::SetQuaternion(const Common::Quaternion& quaternion) { quat = quaternion; } +void MotionInput::SetEulerAngles(const Common::Vec3f& euler_angles) { + const float cr = std::cos(euler_angles.x * 0.5f); + const float sr = std::sin(euler_angles.x * 0.5f); + const float cp = std::cos(euler_angles.y * 0.5f); + const float sp = std::sin(euler_angles.y * 0.5f); + const float cy = std::cos(euler_angles.z * 0.5f); + const float sy = std::sin(euler_angles.z * 0.5f); + + quat.w = cr * cp * cy + sr * sp * sy; + quat.xyz.x = sr * cp * cy - cr * sp * sy; + quat.xyz.y = cr * sp * cy + sr * cp * sy; + quat.xyz.z = cr * cp * sy - sr * sp * cy; +} + void MotionInput::SetGyroBias(const Common::Vec3f& bias) { gyro_bias = bias; } @@ -222,6 +238,26 @@ Common::Vec3f MotionInput::GetRotations() const { return rotations; } +Common::Vec3f MotionInput::GetEulerAngles() const { + // roll (x-axis rotation) + const float sinr_cosp = 2 * (quat.w * quat.xyz.x + quat.xyz.y * quat.xyz.z); + const float cosr_cosp = 1 - 2 * (quat.xyz.x * quat.xyz.x + quat.xyz.y * quat.xyz.y); + + // pitch (y-axis rotation) + const float sinp = std::sqrt(1 + 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z)); + const float cosp = std::sqrt(1 - 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z)); + + // yaw (z-axis rotation) + const float siny_cosp = 2 * (quat.w * quat.xyz.z + quat.xyz.x * quat.xyz.y); + const float cosy_cosp = 1 - 2 * (quat.xyz.y * quat.xyz.y + quat.xyz.z * quat.xyz.z); + + return { + std::atan2(sinr_cosp, cosr_cosp), + 2 * std::atan2(sinp, cosp) - Common::PI / 2, + std::atan2(siny_cosp, cosy_cosp), + }; +} + void MotionInput::ResetOrientation() { if (!reset_enabled || only_accelerometer) { return; diff --git a/src/core/hid/motion_input.h b/src/core/hid/motion_input.h index 9f3fc1cf7..482719359 100644 --- a/src/core/hid/motion_input.h +++ b/src/core/hid/motion_input.h @@ -35,6 +35,7 @@ public: void SetAcceleration(const Common::Vec3f& acceleration); void SetGyroscope(const Common::Vec3f& gyroscope); void SetQuaternion(const Common::Quaternion& quaternion); + void SetEulerAngles(const Common::Vec3f& euler_angles); void SetGyroBias(const Common::Vec3f& bias); void SetGyroThreshold(f32 threshold); @@ -54,6 +55,7 @@ public: [[nodiscard]] Common::Vec3f GetGyroBias() const; [[nodiscard]] Common::Vec3f GetRotations() const; [[nodiscard]] Common::Quaternion GetQuaternion() const; + [[nodiscard]] Common::Vec3f GetEulerAngles() const; [[nodiscard]] bool IsMoving(f32 sensitivity) const; [[nodiscard]] bool IsCalibrated(f32 sensitivity) const; -- cgit v1.2.3 From f017335fef95d2cecc0fcda185f0e59cc1945101 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Fri, 5 May 2023 17:11:53 -0600 Subject: input_common: Add property to invert an axis button --- src/core/hid/input_converter.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/hid') diff --git a/src/core/hid/input_converter.cpp b/src/core/hid/input_converter.cpp index 7cee39a53..a38e3bb3f 100644 --- a/src/core/hid/input_converter.cpp +++ b/src/core/hid/input_converter.cpp @@ -54,6 +54,7 @@ Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatu case Common::Input::InputType::Analog: status.value = TransformToTrigger(callback).pressed.value; status.toggle = callback.analog_status.properties.toggle; + status.inverted = callback.analog_status.properties.inverted_button; break; case Common::Input::InputType::Trigger: status.value = TransformToTrigger(callback).pressed.value; -- cgit v1.2.3