Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Gyro aiming: left / right is the turn about the room's vertical (gravity), not the pad's own yaw: held tilted, part of the turn was on its roll axis and aimed less

Rafael Abud Ferreirabbb5a69d85bf
diff --git a/src/input/controller.cpp b/src/input/controller.cpp
index 94e1f3c..fadd26c 100644
--- a/src/input/controller.cpp
+++ b/src/input/controller.cpp
@@ -174,7 +174,7 @@ std::atomic<bool> g_triggerPulses{false}, g_triggerLogging{false}, g_resistanceA
bool g_rumbleLogged = false;
std::atomic<int> g_gyroMode{kGyroOff}, g_gyroLevel{5};
std::atomic<bool> g_gyroActive{false};
-int g_gyroLogged = 0; // the first turns, to check the axes' signs
+int g_gyroLogged = 0, g_gyroLoggedX = 0; // the first turns up / down and sideways, to check the axes
inline float normStick(uint8_t val) {
// 0..255 -> -1.0 .. 1.0
@@ -458,14 +458,18 @@ bool pollPad(int index, ControllerState& out) {
bool gyroOn = g_gyroActive && (gyroMode == kGyroAlways || (gyroMode == kGyroAiming && out.triggerL2 > 0.3f));
if (gyroOn) {
float gx, gy;
- gyroToStick(pad.angularVelocity, g_gyroLevel.load(std::memory_order_relaxed), slot.gyro, gx, gy);
+ gyroToStick(pad.angularVelocity, pad.acceleration, g_gyroLevel.load(std::memory_order_relaxed), slot.gyro, gx, gy);
float x = out.rightStickX + gx, y = out.rightStickY + gy, m = std::hypot(x, y);
if (m > 1.0f) x /= m, y /= m;
out.rightStickX = x;
out.rightStickY = y;
- if (g_gyroLogged < 6 && std::abs(pad.angularVelocity[1]) + std::abs(pad.angularVelocity[0]) > 1.0f)
- XC_LOGI("gyro: pitch %.2f yaw %.2f roll %.2f rad/s -> stick %.2f,%.2f (%d)", pad.angularVelocity[0],
- pad.angularVelocity[1], pad.angularVelocity[2], gx, gy, ++g_gyroLogged);
+ // The first few turns each way, to check the axes.
+ bool sideways = std::abs(gx) > 0.5f && std::abs(gx) > std::abs(gy);
+ int& logged = sideways ? g_gyroLoggedX : g_gyroLogged;
+ if (logged < 4 && std::max(std::abs(gx), std::abs(gy)) > 0.5f)
+ XC_LOGI("gyro: pitch %.2f yaw %.2f roll %.2f rad/s, gravity %.2f,%.2f,%.2f -> stick %.2f,%.2f",
+ pad.angularVelocity[0], pad.angularVelocity[1], pad.angularVelocity[2], pad.acceleration[0],
+ pad.acceleration[1], pad.acceleration[2], gx, gy), ++logged;
} else {
slot.gyro = {};
}
diff --git a/src/input/gyro.cpp b/src/input/gyro.cpp
index 2dfd09c..364facc 100644
--- a/src/input/gyro.cpp
+++ b/src/input/gyro.cpp
@@ -7,14 +7,28 @@
namespace xc::input {
-void gyroToStick(const float w[3], int level, GyroState& s, float& x, float& y) {
+void gyroToStick(const float w[3], const float* accel, int level, GyroState& s, float& x, float& y) {
+ // Up: gravity, eased so the hand's own jolts don't swing it, and kept
+ // pointing out of the pad's top side (not upside down).
+ if (accel) {
+ float m = std::sqrt(accel[0] * accel[0] + accel[1] * accel[1] + accel[2] * accel[2]);
+ if (m > 0.5f && m < 1.5f) { // about 1 g: the pad isn't being shaken
+ float sign = accel[1] < 0 ? -1.0f : 1.0f;
+ for (int i = 0; i < 3; ++i) s.up[i] += (sign * accel[i] / m - s.up[i]) * 0.1f;
+ float n = std::sqrt(s.up[0] * s.up[0] + s.up[1] * s.up[1] + s.up[2] * s.up[2]);
+ if (n > 1e-3f)
+ for (float& u : s.up) u /= n;
+ }
+ }
+ // The turn about the room's vertical: flat, that is the pad's yaw alone.
+ const float yaw = w[0] * s.up[0] + w[1] * s.up[1] + w[2] * s.up[2];
// Below ~1 degree a second it is the hand shaking or the sensor's noise.
constexpr float kNoise = 0.015f;
auto still = [](float v) { return std::abs(v) < kNoise ? 0.0f : v - std::copysign(kNoise, v); };
// Intensity 5: half a turn a second (~0.5 rad/s, a quick aim) is ~40 %.
const float scale = 0.16f * std::clamp(level, 1, kGyroLevels);
// Turning left (yaw +) aims left; tilting the top up (pitch +) aims up.
- float tx = -still(w[1]) * scale, ty = -still(w[0]) * scale;
+ float tx = -still(yaw) * scale, ty = -still(w[0]) * scale;
s.x += (tx - s.x) * 0.5f;
s.y += (ty - s.y) * 0.5f;
float m = std::hypot(s.x, s.y);
diff --git a/src/input/gyro.h b/src/input/gyro.h
index 2cd9996..aaae9ad 100644
--- a/src/input/gyro.h
+++ b/src/input/gyro.h
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 RafaelNGP
// Gyro aiming: turning the DualSense moves the right stick, for the cloud
-// games that only know sticks. The turn speed (the pad's yaw and pitch) is
-// eased a little and lifted past the games' own stick dead zone, so small
-// turns still move the aim.
+// games that only know sticks. Left / right is the turn about the room's
+// vertical (from gravity: a pad held tilted turns partly about its own roll
+// axis), up / down the pad's pitch; eased a little and lifted past the games'
+// own stick dead zone, so small turns still move the aim.
#pragma once
namespace xc::input {
@@ -12,11 +13,13 @@ constexpr int kGyroLevels = 10; // intensity 1..kGyroLevels
enum GyroMode { kGyroOff, kGyroAlways, kGyroAiming }; // kGyroAiming: while L2 is held
struct GyroState {
- float x = 0, y = 0; // the push given, eased
+ float x = 0, y = 0; // the push given, eased
+ float up[3] = {0, 1, 0}; // the room's up in the pad's axes, eased
};
-// `w`: the pad's angular velocity (rad/s; x pitch, y yaw, z roll) -> the
-// push to add to the right stick (pad convention: +x right, +y down).
-void gyroToStick(const float w[3], int level, GyroState& s, float& x, float& y);
+// `w`: the pad's angular velocity (rad/s; x pitch, y yaw, z roll), `accel`
+// what its accelerometer reads (g; nullptr: held flat) -> the push to add
+// to the right stick (pad convention: +x right, +y down).
+void gyroToStick(const float w[3], const float* accel, int level, GyroState& s, float& x, float& y);
} // namespace xc::input
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 916afa7..03f1653 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -1060,25 +1060,37 @@ static void testGyro() {
GyroState s;
float x = 1, y = 1;
const float still[3] = {0.01f, -0.01f, 0.0f};
- gyroToStick(still, 5, s, x, y);
+ gyroToStick(still, nullptr, 5, s, x, y);
CHECK(x == 0 && y == 0);
const float left[3] = {0.0f, 0.5f, 0.0f}; // yaw +: turning left
- for (int i = 0; i < 8; ++i) gyroToStick(left, 5, s, x, y);
+ for (int i = 0; i < 8; ++i) gyroToStick(left, nullptr, 5, s, x, y);
CHECK(x < -0.3f && x > -0.6f && std::abs(y) < 1e-6f);
GyroState s2;
float x2 = 0, y2 = 0;
- for (int i = 0; i < 8; ++i) gyroToStick(left, 10, s2, x2, y2);
+ for (int i = 0; i < 8; ++i) gyroToStick(left, nullptr, 10, s2, x2, y2);
CHECK(x2 < x);
const float up[3] = {0.3f, 0.0f, 0.0f}; // pitch +: the top tilted up
GyroState s3;
- for (int i = 0; i < 8; ++i) gyroToStick(up, 5, s3, x, y);
+ for (int i = 0; i < 8; ++i) gyroToStick(up, nullptr, 5, s3, x, y);
CHECK(y < -0.12f && x == 0);
const float tiny[3] = {0.0f, 0.03f, 0.0f}; // a slow turn: still past the dead zone
GyroState s4;
- for (int i = 0; i < 8; ++i) gyroToStick(tiny, 5, s4, x, y);
+ for (int i = 0; i < 8; ++i) gyroToStick(tiny, nullptr, 5, s4, x, y);
CHECK(x <= -0.12f);
- for (int i = 0; i < 20; ++i) gyroToStick(still, 5, s4, x, y);
+ for (int i = 0; i < 20; ++i) gyroToStick(still, nullptr, 5, s4, x, y);
CHECK(x == 0);
+ // Held tilted 45 degrees towards the player: a turn about the room's
+ // vertical shows on the pad's yaw and roll; it aims as far as held flat.
+ const float c = 0.7071f, tilted[3] = {0.0f, 0.5f * c, 0.5f * c}, gravity[3] = {0.0f, c, c};
+ GyroState s5;
+ float x5 = 0, y5 = 0;
+ for (int i = 0; i < 60; ++i) gyroToStick(tilted, gravity, 5, s5, x5, y5);
+ CHECK(x5 < -0.3f);
+ GyroState flat;
+ float xf = 0, yf = 0;
+ const float level[3] = {0.0f, 1.0f, 0.0f};
+ for (int i = 0; i < 60; ++i) gyroToStick(left, level, 5, flat, xf, yf);
+ CHECK(std::abs(x5 - xf) < 0.01f);
}
int main() {