From 4b4f1b4dbd16ac683d63f4a1140d882c0473fdf2 Mon Sep 17 00:00:00 2001 From: annika <annika.oesterdiekhoff@uni-bielefeld.de> Date: Tue, 13 Feb 2024 12:53:59 +0100 Subject: [PATCH] fix joystick movement --- .../gui_2d_vis/overcooked_gui.py | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index 1f5dc39c..4235ad63 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -182,36 +182,38 @@ class PyGameGUI: if np.linalg.norm(move_vec) != 0: move_vec = move_vec / np.linalg.norm(move_vec) - print("move_vec keys", move_vec) action = Action( key_set.name, ActionType.MOVEMENT, move_vec, duration=1 / self.FPS ) self.send_action(action) def handle_joy_stick_input(self, joysticks): - """MISSING + """Handles joystick inputs for movement every frame + Args: + joysticks: list of joysticks """ + # Axis 0: joy stick left: -1 = left, ~0 = center, 1 = right + # Axis 1: joy stick left: -1 = up, ~0 = center, 1 = down # see control stuff here (at the end of the page): https://www.pygame.org/docs/ref/joystick.html for joystick in joysticks.values(): - # Usually axis run in pairs, up/down for one, and left/right for - # the other. Triggers count as axes. - axes = joystick.get_numaxes() + # Usually axis run in pairs, up/down for one, and left/right for the other. Triggers count as axes. move_vec = np.zeros(2) - for i in range(axes): - axis = joystick.get_axis(i) - print("axis key", axis) - # joy stick left --> left & right - if axes == 0: - move_vec[0] += axis - # joy stick right --> up & down - elif axes == 1: - move_vec[1] += axis - - print("move_vec joystick before ", move_vec) + # You may want to take into account some tolerance to handle jitter, + # and joystick drift may keep the joystick from centering at 0 or using the full range of position values. + # FIXME: hardcoded threshold for tolerance + tolerance_threshold = 0.2 + # axis 0 = joy stick left --> left & right + axis_left_right = joystick.get_axis(0) + if abs(axis_left_right) > tolerance_threshold: + move_vec[0] += axis_left_right + # axis 1 = joy stick right --> up & down + axis_up_down = joystick.get_axis(1) + if abs(axis_up_down) > tolerance_threshold: + move_vec[1] += axis_up_down + if np.linalg.norm(move_vec) != 0: move_vec = move_vec / np.linalg.norm(move_vec) - print("move_vec joystick", move_vec) action = Action( "0", ActionType.MOVEMENT, move_vec, duration=1 / self.FPS ) -- GitLab