melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

Support Gamepads

Open adammw opened this issue 14 years ago • 33 comments

Gamepad API http://dvcs.w3.org/hg/webevents/raw-file/default/gamepad.html So far has been implemented in Chrome Canary on all platforms (Windows is XBox 360 controller only). Would be really good for platform games.

adammw avatar Jan 13 '12 08:01 adammw

Found these resources here: http://www.html5rocks.com/en/tutorials/doodles/gamepad/gamepad-tester/gamepad.js

http://www.html5rocks.com/en/tutorials/doodles/gamepad

360 works in chrome on the tester. Works sorta on firefox, but a lot of bindings are wrong :/. Might be able to toy with it, see if it can be fixed.

agmcleod avatar Jul 21 '15 03:07 agmcleod

Two ideas I like better on my side :

  • Joypad App on the appStore : http://getjoypad.com/
  • PubNub Controller : http://www.pubnub.com/blog/turn-your-phone-into-a-motion-controller-with-polymer/

With PubNub being naturally a better fit with melonJS, as the former seems more to be tight with native application and SDK.

Because everybody has a smartphone they could use to play on their laptop :P

obiot avatar Jul 21 '15 03:07 obiot

I think those are nice options, but shouldn't be mandatory. It's hard to beat the performance of a wired device compared to a bluetooth or wireless service talking to the browser.

On Mon, Jul 20, 2015 at 11:29 PM Olivier Biot [email protected] wrote:

Two ideas I like better on my side :

*Joypad App on the appStore * : http://getjoypad.com/

*PubNub Controller * : http://www.pubnub.com/blog/turn-your-phone-into-a-motion-controller-with-polymer/

With PubNub being naturally a better fit with melonJS, as the former seems more to be tight with native application and SDK.

— Reply to this email directly or view it on GitHub https://github.com/melonjs/melonJS/issues/34#issuecomment-123140632.

agmcleod avatar Jul 21 '15 03:07 agmcleod

If UA support for the GamePad API has improved since I last looked at this, I'm all for it!

parasyte avatar Jul 21 '15 05:07 parasyte

FYI, I wanted to polyfill this with a virtual gamepad, using kenney's graphics pack for the visuals. Then I stormed away in frustration with the bad native implementations and melonJS lacking enter/leave events at the time. :wink:

Here's the source for my original virtual gamepad, BTW https://github.com/blipjoy/sprung_fever/blob/gh-pages/public/js/entities/ui.js

parasyte avatar Jul 21 '15 05:07 parasyte

i think the support status is still more or less the same : http://caniuse.com/#feat=gamepad only difference since last time is that Chrome lose the webkit prefix, which makes it a bit more standard in terms of implementation between Firefox and Chrome.

a polyfill though is a great idea, or somehting similar to would allow to attach different joypad :

  • a real gamepad via the gamepad API
  • a virtual one like the one Jason implemented
  • a remote virtual one, for example like the one using pubnub

obiot avatar Jul 21 '15 06:07 obiot

UGH! The Wii U browser has its own vendor-specific gamepad API (does not follow the spec): http://www.nintendo.com/wiiu/built-in-software/browser-specs/extended-functionality/

parasyte avatar Aug 08 '15 03:08 parasyte

Basic gamepad support is done (ticket-34 branch) along with some simple button remapping. This takes care of support for the digital buttons. Still missing is the analog button support and analog axes.

Below is a stream of consciousness writeup, as I try to work out how analog inputs will work in melonJS...

First, let's talk about that simple button remapping! I had to fight against Chrome's shady gamepad API bugs to get this code tested. It required many browser restarts... anyway, Firefox doesn't seem to support any gamepad mappings at all! So I had to write maps for every gamepad I have access to. This was an exercise in pushing every button and flipping every axis while watching the HTML5 gamepad tester.

The button you physically press on the gamepad is the mapping index (according to the standard gamepad mapping spec), and the button you see highlighted on the tester is the mapping value ... I've also taken the liberty to make axes[4] map to the left analog trigger, and axes[5] map to the right analog trigger. Since there are a lot of gamepads that melonJS will never support directly, it will be useful to provide a method for user-code to supply its own mapping (including overriding the default mappings).

The analog values are interesting, and currently missing entirely from melonJS. The input interface has classically been normalized to a keyboard; pointer events are bound to keys, etc. However, melonJS has a notion of generic "actions" which keyboard keys are bound to. This seems like the right place to put analog support; give the "actions" an analog state (in addition to its digital state). Then bind pointer events and gamepad buttons/axes to melonJS actions.

We'll keep the current bindPointer behavior of binding to a keyCode for backward compatibility. Going forward, we need a new pointer API for binding directly to an action. The gamepad support could even launch with bind-to-action, and never support bind-to-keycode.

A new API method will be needed to retrieve action state (in place of key state). This will replace the me.input.isKeyPressed family as the primary source of input values. There should be two different methods for getting input action state; one for digital and one for analog. Every action will support both; e.g. a keyboard key will provide "analog" values of 0.0 and 1.0, corresponding to digital values false and true. The digital state is capable of edge detection (as in the lock parameter for the bindKey method).

The second thing about supporting analog buttons/axes is normalization. Some gamepads (notably OUYA) do not deliver analog values in normalized form [-1.0..1.0]. For these gamepads, we need a method of normalizing the analog value.

And finally, integrating a virtual gamepad and remote gamepad ... Having a method to write action state (analog and digital) it really all it needs. Virtual gamepads and remote gamepads can then be added as plugins. This extends to the Wii U gamepad, IMHO; a plugin is a good place for it.

Here's the breakdown:

  • Add a new API for reading and writing action states
    • me.input.getActionEdge(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, axes, buttons, normalize_fn)
    • axes and buttons are each an array of mappings. See https://github.com/melonjs/melonJS/blob/096c14960a0a7978ed3fcce930529a0dec74e0af/src/input/gamepad.js#L22-L23
    • normalize_fn will be called on an unmapped axis or button with the following parameters:
      • {String} type "axes" or "buttons"
      • {Number} index axis or button index (this is the raw index from the gamepad driver, not the standard gamepad mapping)
      • {Number} value the value read from navigator.getGamepads()[...][type][index].value
    • normalize_fn must return a normalized value in range [-1.0..1.0], or 0.0 if the button or axis is unknown.

parasyte avatar Aug 08 '15 21:08 parasyte

Good news! I spent the day investigating the gamepad bugs in Chrome, and eventually created a patch for it. I'll try to get it upstream for Chrome 46.

parasyte avatar Aug 10 '15 06:08 parasyte

(just for me to remember it tomorrow)

To create a mapping for FF :

  • start with a default mapping :
    {
        "axes" : [ 0, 1, 2, 3, -1, -1 ],
        "buttons" : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    }
  • at each index listed here
  • replace the value with the one given here

obiot avatar Aug 17 '15 16:08 obiot

i guess we can close this one now ? as we have basic support done, and follow up points in #724

obiot avatar Aug 20 '15 14:08 obiot

Agreed. Great work!

parasyte avatar Aug 20 '15 15:08 parasyte

Well you did most of it ! :)

CHANGELOG? :p

obiot avatar Aug 20 '15 15:08 obiot