Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gamepad support #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

Pyroglyph
Copy link

The controller doesn't currently support gamepads. This PR aims to change that!

Currently, keyboard input is broken as the gamepad is overriding the values in FpsControllerInput.
There are two ways of solving this:

  1. Both input methods need to reach consensus. For example, pressing W on your keyboard and moving the left stick to the left will cause the player to move forward and left.
  2. The game should automatically switch between the two input methods (depending on which one received the most recent input)

I'm not sure which solution is best, so any input is welcome! (pun not intended)

This currently breaks keyboard input as the gamepad is overriding the values in FpsControllerInput. Both methods either need to reach consensus or we need to be able to switch from one to the other.
@Pyroglyph
Copy link
Author

A quick and dirty change to add input consesus is here:

- input.movement = Vec3::new(move_vec.x, vertical_axis, move_vec.y);
+ input.movement += Vec3::new(move_vec.x, vertical_axis, move_vec.y);
                 ^
- input.sprint = button_input.pressed(button(controller.pad_sprint));
+ input.sprint = input.sprint || button_input.pressed(button(controller.pad_sprint));
                 ^-------------^
- input.jump = button_input.pressed(button(controller.pad_jump));
+ input.jump = input.jump || button_input.pressed(button(controller.pad_jump));
               ^-----------^
- input.fly = button_input.just_pressed(button(controller.pad_fly));
+ input.fly = input.fly || button_input.just_pressed(button(controller.pad_fly));
              ^----------^
- input.crouch = button_input.pressed(button(controller.pad_crouch));
+ input.crouch = input.crouch || button_input.pressed(button(controller.pad_crouch));
                 ^-------------^

This naive implementation is kinda brittle as relies on the gamepad input running after the keyboard input, but it does work.

@Pyroglyph
Copy link
Author

After asking around online, someone brought up a good point in favour of the input consensus solution.

The Steam Deck and Steam Controller’s touchpads can result in both mouse and joystick input being used simultaneously. Locking down which input is used prevents this from working as intended.

As such, I think the consensus solution would be the way to go.

Here's a link to the thread if anyone's interested.

It is now possible to use keyboard/mouse/gamepad in tandem. Also, FpsControllerInput is now stateless and truly represents one frame of input.
@Pyroglyph Pyroglyph marked this pull request as ready for review March 20, 2024 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant