summaryrefslogtreecommitdiff
path: root/src/core/hid/motion_input.cpp
diff options
context:
space:
mode:
authorGravatar Narr the Reg2023-05-05 12:29:26 -0600
committerGravatar Narr the Reg2023-05-05 13:53:38 -0600
commit46e835f2d6531baea061a2723d171a2f5a1abf6a (patch)
treedab41bff4babe44fd5850c6f5042090d7179bf82 /src/core/hid/motion_input.cpp
parentMerge pull request #10153 from FernandoS27/a-quickie-fixie (diff)
downloadyuzu-46e835f2d6531baea061a2723d171a2f5a1abf6a.tar.gz
yuzu-46e835f2d6531baea061a2723d171a2f5a1abf6a.tar.xz
yuzu-46e835f2d6531baea061a2723d171a2f5a1abf6a.zip
yuzu: Add motion preview to controller input
Diffstat (limited to 'src/core/hid/motion_input.cpp')
-rw-r--r--src/core/hid/motion_input.cpp36
1 files changed, 36 insertions, 0 deletions
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 @@
1// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include <cmath>
5
4#include "common/math_util.h" 6#include "common/math_util.h"
5#include "core/hid/motion_input.h" 7#include "core/hid/motion_input.h"
6 8
@@ -51,6 +53,20 @@ void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
51 quat = quaternion; 53 quat = quaternion;
52} 54}
53 55
56void MotionInput::SetEulerAngles(const Common::Vec3f& euler_angles) {
57 const float cr = std::cos(euler_angles.x * 0.5f);
58 const float sr = std::sin(euler_angles.x * 0.5f);
59 const float cp = std::cos(euler_angles.y * 0.5f);
60 const float sp = std::sin(euler_angles.y * 0.5f);
61 const float cy = std::cos(euler_angles.z * 0.5f);
62 const float sy = std::sin(euler_angles.z * 0.5f);
63
64 quat.w = cr * cp * cy + sr * sp * sy;
65 quat.xyz.x = sr * cp * cy - cr * sp * sy;
66 quat.xyz.y = cr * sp * cy + sr * cp * sy;
67 quat.xyz.z = cr * cp * sy - sr * sp * cy;
68}
69
54void MotionInput::SetGyroBias(const Common::Vec3f& bias) { 70void MotionInput::SetGyroBias(const Common::Vec3f& bias) {
55 gyro_bias = bias; 71 gyro_bias = bias;
56} 72}
@@ -222,6 +238,26 @@ Common::Vec3f MotionInput::GetRotations() const {
222 return rotations; 238 return rotations;
223} 239}
224 240
241Common::Vec3f MotionInput::GetEulerAngles() const {
242 // roll (x-axis rotation)
243 const float sinr_cosp = 2 * (quat.w * quat.xyz.x + quat.xyz.y * quat.xyz.z);
244 const float cosr_cosp = 1 - 2 * (quat.xyz.x * quat.xyz.x + quat.xyz.y * quat.xyz.y);
245
246 // pitch (y-axis rotation)
247 const float sinp = std::sqrt(1 + 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z));
248 const float cosp = std::sqrt(1 - 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z));
249
250 // yaw (z-axis rotation)
251 const float siny_cosp = 2 * (quat.w * quat.xyz.z + quat.xyz.x * quat.xyz.y);
252 const float cosy_cosp = 1 - 2 * (quat.xyz.y * quat.xyz.y + quat.xyz.z * quat.xyz.z);
253
254 return {
255 std::atan2(sinr_cosp, cosr_cosp),
256 2 * std::atan2(sinp, cosp) - Common::PI / 2,
257 std::atan2(siny_cosp, cosy_cosp),
258 };
259}
260
225void MotionInput::ResetOrientation() { 261void MotionInput::ResetOrientation() {
226 if (!reset_enabled || only_accelerometer) { 262 if (!reset_enabled || only_accelerometer) {
227 return; 263 return;