Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4856

Gaming • Gyro Aiming in any Raspberry Pi with SDL >= 2.0.22

$
0
0
Intro

Playstation and (specially) Nintendo gamers can testify on how practical it is to aim in shooters by just tilting the gamepad. While not as accurate as a high quality mouse, it is ridiculously more usable, not to mention immersive, than the classic right stick. And currently there's at least one port of a game that allows for it, in any Raspberry Pi.

Hardware requirements

  • A Raspberry Pi, any model (duh)
  • A gamepad with a gyroscopic sensor
For the second point, this is a complete list. Most known ones are the PS4 and 5 controllers, the Nintendo Switch Pro, and the old Steam Controller.

Software requirements

* OS: either Raspberry Pi OS Bookworm or Arch Linux ARM.
This is because they offer packages with newer SDL2 versions. As said in the title, SDL must be 2.0.22 or newer. If you know how to compile a newer version for Bullseye, please share how it's done.
Bullseye offers SDL 2.0.14; this version allows for gyro, but with a very limited compatibility (e.g. PS4 controller may work, but PS5 and Switch definitely won't).

* Your OS has to detect your gamepad connected, through either USB or Bluetooth.
Doesn't matter if it doesn't detect gyro; we will solve that now.

How to make SDL2 detect your gamepad's gyro sensor

Your OS must provide Hidraw read/write access to the gamepad. The following set of udev rules will allow for it:
https://aur.archlinux.org/packages/game-devices-udev
Just follow the installation steps in "How to set it up?" -> "Others".
Basically, copy all *.rules to /etc/udev/rules.d, create /etc/modules-load.d/uinput.conf with just "uinput" as content, and reboot.

So, is this used somewhere? Yeah...

Practical example: Yamagi Quake II

To try this, you'll need either the full version of the original 1997 release of Quake II, or the demo version 3.14:
https://deponie.yamagi.org/quake2/idstu ... mo-x86.exe (this is a self-extracting zip, can be "unzip"-ed without issues without needing Windows)

The following is a executive summary of its installation. It assumes you are installing the game in ~/Quake2

Prerequisites
For Bookworm:

Code:

apt install build-essential libgl1-mesa-dev libsdl2-dev libopenal-dev libcurl4-openssl-dev
For Arch:

Code:

pacman -S base-devel mesa openal curl sdl2
Compiling

Code:

git clone https://github.com/yquake2/yquake2.gitcd yquake2makecp -r release/* ~/Quake2
Then, copy the entire baseq2 directory of your game to ~/Quake2; you can omit the gamex86.dll file.

Code:

cd (location of your unzipped game installation)cp -r baseq2 ~/Quake2
Before starting the game for the first time, let's set some helpful default values to the game configuration. Save the following as ~/.yq2/baseq2/yq2.cfg

Code:

// Default gamepad controlsbind BTN_A "cycleweap weapon_supershotgun weapon_shotgun weapon_chainfist"bind BTN_B "cycleweap weapon_bfg weapon_disintegrator weapon_railgun weapon_hyperblaster ammo_tesla ammo_trap"bind BTN_X "cycleweap weapon_phalanx weapon_rocketlauncher weapon_proxlauncher weapon_grenadelauncher ammo_grenades"bind BTN_Y "cycleweap weapon_plasmabeam weapon_boomer weapon_chaingun weapon_etf_rifle weapon_machinegun weapon_blaster"bind BTN_BACK "cmd help"bind STICK_LEFT "+gyroaction"bind STICK_RIGHT "centerview"bind SHOULDR_LEFT "+movedown"bind SHOULDR_RIGHT "+joyaltselector"bind DP_UP "prefweap weapon_railgun weapon_plasmabeam weapon_boomer weapon_hyperblaster weapon_chaingun weapon_supershotgun weapon_etf_rifle weapon_machinegun weapon_shotgun weapon_blaster"bind DP_DOWN "prefweap weapon_bfg weapon_disintegrator weapon_phalanx weapon_railgun weapon_rocketlauncher weapon_plasmabeam weapon_boomer weapon_hyperblaster weapon_grenadelauncher weapon_chaingun weapon_proxlauncher ammo_grenades weapon_supershotgun"bind DP_LEFT "weapprev"bind DP_RIGHT "weapnext"bind TRIG_LEFT "+moveup"bind TRIG_RIGHT "+attack"bind BTN_BACK_ALT "inven"bind DP_UP_ALT "invuse"bind DP_DOWN_ALT "invdrop"bind DP_LEFT_ALT "invprev"bind DP_RIGHT_ALT "invnext"// Gameplay optionsset aimfix "1"set cl_run "1"set g_machinegun_norecoil "1"set g_quick_weap "1"set g_swap_speed "2"// Audioset s_openal "0"set ogg_ignoretrack0 "1"// Videoset vid_renderer "gl1"set gl1_lightmapcopies "1"set gl1_discardfb "1"set gl1_pointparameters "0"set vid_fullscreen "0"set r_mode "-2"
It's recommended to run the game outside of X11 or any other windowing system; the KMSDRM video driver (used in the command line) is the best for this hardware.
Run the game with ~/Quake2/quake2
To increase performance, set a proper resolution in game. E.g. for a RPi 3, 1366x768 guarantees 60fps at all times. You may want to set it to 1280x720 or lower in smaller hardware.

For more details, check the YQ2 documentation or this thread.

Using the controller in the game

It should be automatically detected; the menu itself can be handled with the DPad and the A and B (or X and O, or equivalent) buttons. If not, copy:
https://raw.githubusercontent.com/gabom ... llerdb.txt
...to either the ~/Quake2 or ~/.yq2 directory.
If you start a game, first thing you'll notice is your view slowly drifting. This is good! It means you'll need to calibrate the gyro.
In Options -> customize gamepad -> gyro options at the bottom, there is "calibrate". Put your gamepad in a stable surface and select that with Enter or "A". Leave it alone, and after 5 or 6 seconds, it will announce "Calibration complete!". If your view still drifts, you should increase the deadzone on your right stick instead; that is found in the "stick config" section of the "customize gamepad" menu.
You should really take a look at the entire gamepad menu here. Not only you can change button binds, but also different sensitivities for the sticks and gyro.
Note that the gyro uses a "natural sensitivity" scale, meaning that at "1.0" (default), a 90º turn to the right of your gamepad will mean a 90º turn to the right of your in-game view, while at "2.0" sensitivity the same turn will mean a 180º turn of your game view. If you're just beginning I don't recommend something higher than 3, but after a while you'll increase it ;)

If you are using USB, you can increase the rumble intensity. Don't do it with Bluetooth, the gamepad will disconnect unfortunately.

For more options, please check the entire gamepad menu and the Yamagi documentation. You can even set Flick Stick to your liking!

Conclusion

Gyro makes aiming with a gamepad fun, specially in old games which don't have stick aiming in mind, because they don't have the usual assists the console shooters have.
The entire Yamagi SDL2/3 gyro implementation is short and very practical. Currently Ironwail uses it, so there's a record of at least two successful applications.
Hopefully another port that runs on a RPi can apply this, or any other application; SDL provides that flexibility.
If you have any questions, just shoot!

Statistics: Posted by Protocultor — Sun Jan 05, 2025 7:20 pm



Viewing all articles
Browse latest Browse all 4856

Trending Articles