Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Pads: stronger vibration at low motor values, steadier updates

Samir Nurie40e9c576a02
diff --git a/host/window.cpp b/host/window.cpp
index 4ba8d17..f5e8d58 100644
--- a/host/window.cpp
+++ b/host/window.cpp
@@ -9,6 +9,7 @@
#include <algorithm>
#include <atomic>
+#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <thread>
@@ -107,15 +108,19 @@ std::uint8_t axis(SDL_GameControllerAxis a) {
}
void rumble() {
- static std::uint16_t last = 0;
+ static double last_strong = 0.0;
+ static bool last_weak = false;
static Uint32 sent = 0;
if (!g_pad) return;
const std::uint16_t motors = ps2::pad_rumble();
+ const double strong = (motors >> 8) < 16 ? 0.0 : std::sqrt((motors >> 8) / 255.0);
+ const bool weak = motors & 1u;
const Uint32 now = SDL_GetTicks();
- if (motors == last && (!motors || now - sent < 100)) return;
- last = motors;
+ if (std::fabs(strong - last_strong) <= 0.08 && weak == last_weak && (!(strong > 0.0 || weak) || now - sent < 100)) return;
+ last_strong = strong;
+ last_weak = weak;
sent = now;
- SDL_GameControllerRumble(g_pad, static_cast<Uint16>((motors >> 8) * 257), (motors & 1u) ? 0xFFFF : 0, motors ? 200 : 0);
+ SDL_GameControllerRumble(g_pad, static_cast<Uint16>(strong * 65535.0), weak ? 0xFFFF : 0, strong > 0.0 || weak ? 200 : 0);
}
void poll() {
diff --git a/web/shell.html b/web/shell.html
index 8be78b8..7931df0 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -395,7 +395,8 @@ window.addEventListener('keyup', (e) => { keys.delete(e.code); });
const padButtons = [[0, 14], [1, 13], [2, 15], [3, 12], [4, 10], [5, 11], [6, 8], [7, 9], [8, 0], [9, 3], [10, 1], [11, 2],
[12, 4], [13, 6], [14, 7], [15, 5]];
const axis = (v) => Math.max(0, Math.min(255, Math.round((v + 1) * 127.5)));
-let lastPad = -1, lastPressure = '', lastRumble = 0, rumbleAt = 0;
+let lastPad = -1, lastPressure = '', lastStrong = 0, lastWeak = 0, rumbleAt = 0;
+const motorStrength = (v) => v < 16 ? 0 : Math.sqrt(v / 255);
function pollInput() {
let buttons = 0, lx = 128, ly = 128, rx = 128, ry = 128, gamepad = null;
const pressure = new Array(16).fill(255);
@@ -438,11 +439,13 @@ function pollInput() {
Module._ps2web_pressure(packed[0], packed[1], packed[2], packed[3]);
}
const rumble = Module._ps2web_rumble(), now = performance.now();
+ const strong = motorStrength(rumble >> 8), weak = rumble & 1;
const actuator = gamepad && gamepad.vibrationActuator;
- if (actuator && (rumble !== lastRumble || (rumble && now - rumbleAt > 100))) {
- lastRumble = rumble;
+ if (actuator && (Math.abs(strong - lastStrong) > 0.08 || weak !== lastWeak || ((strong || weak) && now - rumbleAt > 100))) {
+ lastStrong = strong;
+ lastWeak = weak;
rumbleAt = now;
- const done = rumble ? actuator.playEffect('dual-rumble', { duration: 200, weakMagnitude: rumble & 1, strongMagnitude: (rumble >> 8) / 255 }) : actuator.reset();
+ const done = strong || weak ? actuator.playEffect('dual-rumble', { duration: 200, weakMagnitude: weak, strongMagnitude: strong }) : actuator.reset();
if (done && done.catch) done.catch(() => {});
}
}