Skip to content

Followup: TODO for gamepad support #724

Closed

Description

@parasyte

Follows #34

  • Add a gamepad tester to examples; for validating mappings
  • Remap axes[4] and axes[5] extension (L2 and R2 analog triggers) to buttons[me.GAMEPAD.L2].value and buttons[me.GAMEPAD.R2].value
  • Add a new API for reading and writing action states
    • me.input.isActionSet(action) (was me.input.isKeyPressed)
    • me.input.getActionState(action) (was me.input.keyState)
    • me.input.getActionValue(action) (NEW; returns action analog value)
    • me.input.getAction(action) (NEW; returns action analog value, digital state, and digital edge)
    • me.input.setAction(action, state, value) (NEW; sets action analog value and digital state)
  • Add a new method for custom gamepad mapping, with a normalization function
    • me.input.setGamepadMapping(id, mapping)
    • The function signature has been changed to better fit the needs of remapping axes to button values; see below.
    • mapping is a hash table containing the following keys:
      • {Number[]} mapping.axes Standard analog control stick axis locations
      • {Number[]} mapping.buttons Standard digital button locations
      • {Number[]} [mapping.analog] Analog axis locations for buttons. See: https://developer.mozilla.org/en-US/docs/Web/API/GamepadButton
      • {Function} [mapping.normalize_fn] will be called on an unmapped axis or button with the following parameters:
        • {Number} value The raw value read from the gamepad driver
        • {Number} axis The axis index from the standard mapping, or -1 if not an axis
        • {Number} button The button index from the standard mapping, or -1 if not a button
      • normalize_fn must return a normalized value in range [-1.0..1.0], or 0.0 if the axis is unknown.

mapping.analog is specially for mapping analog buttons or pressure-sensitive buttons from an axis location to a button location. This is for things like the L2 and R2 analog triggers, and pressure sensitive D-pad and face buttons on a PS3 controller. Here's how it might look for remapping L2 and R2 triggers on a wired XBox 360 controller:

{
    "axes" : [ 0, 1, 3, 4 ],
    "buttons" : [ 11, 12, 13, 14, 8, 9, -1, -1, 5, 4, 6, 7, 0, 1, 2, 3, 10 ],
    "analog" : [ -1, -1, -1, -1, -1, -1, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1 ]
}

This will fill in that gap in the buttons array during remapping. A little extra code is necessary to combine the axis and the button to produce a meaningful result.


Some mapping test reports:

Chrome (45+) :

  • PS3 Gamepad
  • PS4 Gamepad
  • XBox One Gamepad
  • XBox 360 USB Gamepad
  • XBox 360 Wireless Gamepad
  • Wii Remote (not recognised by the browser)
  • Wii "Classic" Gamepad (not recognised by the browser)
  • Wii U Gamepad
  • Ouya Gamepad

Firefox (39+) :

  • PS3 Gamepad
  • PS4 Gamepad
  • XBox One Gamepad
  • XBox 360 USB Gamepad
  • XBox 360 Wireless Gamepad
  • Wii Remote (not recognised by the browser)
  • Wii "Classic" Gamepad (not recognised by the browser)
  • Wii U Gamepad
  • Ouya Gamepad

Wiimote on Mac requires a driver: https://github.com/alxn1/wjoy

I haven't mapped it out, yet. But initial testing shows a huge caveat; this driver reports the gamepad id as a string containing a MAC address (which is unique for each device). For this reason, I want to defer Wiimote support to a melonJS plugin; which is better suited to doing things like RegExp over gamepad id.


Ouya gamepad on Mac requires a driver: http://ouyaforum.com/showthread.php?3185-Ouya-controller-input-on-computer-macosx&p=78432&viewfull=1#post78432 This driver is unsigned, so requires disabling KEXT signature checking on OSX 10.10+:

$ sudo nvram boot-args=kext-dev-mode=1

After restarting, load the driver:

$ sudo kextload /System/Library/Extensions/OUYAGameController.kext

Activity

  1. added this to the 3.0.0 milestone on Aug 17, 2015
  2. obiot commented on Aug 18, 2015

    @obiot
    Member

    note that the device test example is maybe a right place where to add simple gamepad tester :P

  3. obiot commented on Aug 18, 2015

    @obiot
    Member

    @parasyte : I added some testing report, based on my test, and the one you described in the other ticket, have a look to see if I missed anything

  4. obiot commented on Aug 18, 2015

    @obiot
    Member

    other improvements I could see, by looking at the API and what you did today :

    • leverage the connected/disconnected event to lower the operation in the update function (but i'm mainly thinking about the remapping stuff)
    • use the timestamp provided in the gamepad object to exit the update function if nothing happen since the last frame (although this is not supported in FF, but only in Chrome)
  5. obiot commented on Aug 18, 2015

    @obiot
    Member

    also, just an idea, but for the mapping function, why not also use it to apply a mapping to keyboard event ?
    as an Id we could use the keyboard keyword for the id, and then we can just pass a default array to map the gamepad array to the keyboard one ?

  6. parasyte commented on Aug 18, 2015

    @parasyte
    CollaboratorAuthor

    Hmm, in regard to the stuff you added to the TODO list with 360 support... that isn't right. ;) melonJS only supports remapping USB 360 controllers on Firefox right now, because that's all I have. The Wireless 360 controller has a different id string at the very least. (So it won't automatically use the mapping for USB controllers.)

    As for the device test, I'm kind of OK with that, but I would prefer making it prettier than just dumping a bunch of text to the screen. I imagine something like the virtual gamepad on the HTML5 gamepad tester. We can use the Kenney gamepad UI graphics for it.

    The connect/disconnect events won't help us, because the UA already removes disconnected gamepads from the navigator.getGamepads() result. And it adds them automatically, too. I see these events as more of an application-level thing; that the game can automatically pause when the gamepad is disconnected, for example, and resume when reconnected. The engine doesn't need to do that kind of thing.

    The timestamp isn't portable, so I didn't bother with it. It also won't really save a whole lot of CPU or time anyway. It's a very, very minor optimization.

    I already explained my reason for not mapping to keyboard events in #34 (comment) (It's already mapping to keyboard events right now, and it doesn't work for the analog axes.)

  7. parasyte commented on Aug 18, 2015

    @parasyte
    CollaboratorAuthor

    @obiot I corrected some of the test report stuff in the first post.

  8. obiot commented on Aug 18, 2015

    @obiot
    Member

    about the timestamp, well i'm not sure, this is still a fair amount of code :
    https://github.com/melonjs/melonJS/blob/ticket-34/src/input/gamepad.js#L78-L104

  9. obiot commented on Aug 18, 2015

    @obiot
    Member

    For the Wiimote, interestingly enough, that's all I see under Chrome :
    image

    Firefox does not even recognize it !

  10. parasyte commented on Aug 18, 2015

    @parasyte
    CollaboratorAuthor

    The code you highlighted is just the inner loop that checks the state of buttons that are actually mapped. In the platformer demo, only up, down, left, right, and face_1 are mapped. So it only iterates five items. Hardly a worry for performance.

  11. obiot commented on Aug 18, 2015

    @obiot
    Member

    @agmcleod if i'm not wrong you do have a xbox one ? don't you ?

  12. added 4 commits that reference this issue on Aug 19, 2015
  13. 27 remaining items

  14. added 3 commits that reference this issue on Mar 5, 2016
  15. parasyte commented on Mar 5, 2016

    @parasyte
    CollaboratorAuthor

    We can add the new API without breaking compatibility; binding inputs to actions, and querying action values. I'd like to start moving toward that soon, too. I'm open to better function naming conventions, if you have ideas!

  16. added 2 commits that reference this issue on Mar 7, 2016
  17. modified the milestones: 4.0.0, 3.1.0 on Apr 4, 2016
  18. modified the milestones: 4.1.0, 4.0.0, on Oct 18, 2016
  19. adrianhartanto0 commented on Feb 5, 2018

    @adrianhartanto0

    Just wondering, what are action states? Include example if possible. Thanks

  20. locked and limited conversation to collaborators on Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions