Support Gamepads
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.
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.
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
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.
If UA support for the GamePad API has improved since I last looked at this, I'm all for it!
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
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
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/
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)(wasme.input.isKeyPressed)me.input.getActionState(action)(wasme.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)axesandbuttonsare each an array of mappings. See https://github.com/melonjs/melonJS/blob/096c14960a0a7978ed3fcce930529a0dec74e0af/src/input/gamepad.js#L22-L23normalize_fnwill be called on an unmapped axis or button with the following parameters:- {String}
type"axes" or "buttons" - {Number}
indexaxis or button index (this is the raw index from the gamepad driver, not the standard gamepad mapping) - {Number}
valuethe value read fromnavigator.getGamepads()[...][type][index].value
- {String}
normalize_fnmust return a normalized value in range[-1.0..1.0], or0.0if the button or axis is unknown.
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.
(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]
}
i guess we can close this one now ? as we have basic support done, and follow up points in #724
Agreed. Great work!
Well you did most of it ! :)
CHANGELOG? :p