ursina
ursina copied to clipboard
Using other input devices
Out of curiosity dig a bit on gamepad and add this patch:
diff --git c/ursina/gamepad.py i/ursina/gamepad.py
index 641865c..22e2465 100644
--- c/ursina/gamepad.py
+++ i/ursina/gamepad.py
@@ -6,7 +6,17 @@ if __name__ == '__main__':
text_entity = Text()
player = Entity(model='cube', color=color.azure)
+ input_handler.gamepads = base.devices.getDevices()
+ print(gamepad.buttons)
+ print(gamepad.axes)
+
def update():
+ for button in gamepad.buttons:
+ print(button.handle.name,":", button.pressed)
+
+ for axis in gamepad.axes:
+ print(axis.axis, ":", axis.value)
+
player.x += held_keys['gamepad left stick x'] * time.dt * 5
player.y += held_keys['gamepad left stick y'] * time.dt * 5
text_entity.text = '\n'.join([f'{key}: {held_keys[key]}' for key in ('gamepad left trigger', 'gamepad right trigger', 'gamepad right stick x', 'gamepad right stick y')])
Which outputs:
trigger : False
joystick2 : False
joystick3 : False
joystick4 : False
joystick5 : False
joystick6 : False
joystick7 : False
joystick8 : False
joystick9 : False
joystick10 : False
joystick11 : False
joystick12 : False
joystick13 : False
hat_left : False
hat_right : False
hat_up : False
hat_down : False
Axis.roll : 0.04673838406958114
Axis.pitch : 0.9941348973607038
Axis.yaw : -0.9960899315738025
Axis.throttle : 0.9990224828934506
Axis.none : 0.0
Axis.none : 0.0
I'm sure ursina tries to stay simple, but wonder if support other devices would be an accepted PR.
Same as buttons remap, it can accept a map for button names and axis names and send that data as gamepad.py does to input function.
Basically adding this into my own main.py for a project was able to read my wheel axis and buttons:
diff --git i/main.py w/main.py
index fdef27c..1e0e7cc
--- i/main.py
+++ w/main.py
@@ -17,6 +19,8 @@ from tracks.snow_track import SnowTrack
from tracks.forest_track import ForestTrack
from tracks.savannah_track import SavannahTrack
from tracks.lake_track import LakeTrack
+from panda3d.core import InputDevice
+
Text.default_font = "./assets/Roboto.ttf"
Text.default_resolution = 1080 * Text.size
@@ -151,14 +155,71 @@ main_menu.sun = sun
# Sky
Sky(texture = "sky")
+base = app
+
+input_handler.wheel = None
+input_handler.wheels = base.devices.getDevices()
+if input_handler.wheels:
+ input_handler.wheel = input_handler.wheels[0]
+
+
+for i, wheel in enumerate(input_handler.wheels):
+ wheel_name = 'wheel'
+ wheel_name = 'wheel'
+ if i > 0:
+ wheel_name += f'_{i}'
+
+ base.attachInputDevice(wheel, prefix=wheel_name)
+ buttons = {
+ 'trigger' : 'btn1',
+ 'joystick2' : 'btn2',
+ 'joystick3' : 'btn3',
+ 'joystick4' : 'btn4',
+ 'joystick5' : 'btn5',
+ 'joystick6' : 'btn6',
+ 'joystick7' : 'btn7',
+ 'joystick8' : 'btn8',
+ 'joystick9' : 'btn9',
+ 'joystick10' : 'btn10',
+ 'joystick11' : 'btn11',
+ 'joystick12' : 'btn12',
+ 'joystick13' : 'btn13',
+ 'hat_left' : 'hleft',
+ 'hat_right' : 'hright',
+ 'hat_down' : 'hdown',
+ 'hat_up' : 'hup',
+ }
+
+ for original_name, new_name in buttons.items():
+ base.accept(f'{wheel_name}-{original_name}', base.input, extraArgs=[f'{wheel_name} {new_name}'])
+ base.accept(f'{wheel_name}-{original_name}-up', base.input, extraArgs=[f'{wheel_name} {new_name} up'])
+
+def wheel_update():
+ axes = {
+ 'roll' : 'axis1',
+ 'pitch' : 'axis2',
+ 'yaw' : 'axis3',
+ 'throttle' : 'axis4',
+ }
+ for i, wheels in enumerate(input_handler.wheels):
+ wheel_name = 'wheel'
+ if i > 0:
+ wheel_name += f'_{i}'
+
+ for key, value in axes.items():
+ held_keys[f'{wheel_name} {value}'] = wheel.findAxis(InputDevice.Axis[key]).value
+
+Entity(name='wheel_handler', update=wheel_update, eternal=True) # connect update() to an entity so it runs
+
def update():
+ print(held_keys["wheel axis1"])
+ print(held_keys["wheel btn1"])
I suppose a generic joystick would be a better name for any device with axis and buttons
I found a problem with current input handler, it tries to filter the " up" movement for a button, but ends filtering any key ending in "up"
I open a PR #524
By now I I call my hat up
hap
instead of hup
Showing a use case for this change: Allow to use a wheel for a drive game (still only hat, not axis, but input is there to code some logic)
https://github.com/mandaw2014/Rally/pull/8
@albfan : I wonder weather this patch might be required to set-up the FirstPersonController
so that it is moved by scrolling mouse wheel 🤔
This help yo recognize other input, so with modification you can use whatever input, and then write some logic around that
You should make a PR if you've got something working and it's stable.
Ok, will try to code some PR to cover devices with axis and buttons