melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.84k stars 643 forks source link

Support Gamepads #34

Closed adammw closed 9 years ago

adammw commented 12 years ago

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.

melonjs commented 12 years ago

I saw that too, I need a gamepad now :)

parasyte commented 11 years ago

Gamepad API is now a working draft spec: http://www.w3.org/TR/gamepad/

melonjs commented 11 years ago

http://www.gamepadjs.com source : https://github.com/sgraham/gamepad.js/

will do a nice plugin once the plugin system will be ready :)

melonjs commented 11 years ago

fyi, there is this game made in the context of the GGJ 2013, i did not really look at it yet, but it uses the gamepad api http://globalgamejam.org/2013/befestigen

melonjs commented 11 years ago

it's rather old, but I just found the following article : http://constyspace.blogspot.nl/2012/07/gamepadhtml5-websocket.html

obiot commented 11 years ago

https://github.com/kallaspriit/HTML5-JavaScript-Gamepad-Controller-Library

obiot commented 10 years ago

http://html5gamepad.com/

aaschmitz commented 10 years ago

https://wiki.mozilla.org/GamepadAPI#!

obiot commented 10 years ago

https://hacks.mozilla.org/2013/12/the-gamepad-api/

lunarcloud commented 10 years ago

I'd love to fork and adapt this to your engine instead, as I wasn't happy with ImpactJS. https://github.com/lunarcloud/gamepad.js

I'm just still learning about your engine, and you don't abstract key presses behind "actions" like they do, so it doesn't look as easy to just make it a plugin that flawlessly adds to any game already written.

aaschmitz commented 10 years ago

Will be a great addition to melonJS!

parasyte commented 10 years ago

@lunarcloud There is some key press abstraction through key bindings. For exmaple, it is possible to bind multiple keys to a defined action, and even binding click/touch to the same action.

obiot commented 10 years ago

that would be awesome !

I actually quickly through the code and I think that it could be easily ported like in a day. I would however probably move some of the detection stuff into our me.device class and probably add there as well a proper polyfil to avoid calling all the webit/Moz prefixed ones in your code.

uxdxdev commented 10 years ago

So does melonjs support Xbox360 controllers?

obiot commented 10 years ago

while looking for audio fix when using cocoonjs through google, I stumbled on the following : http://videlais.com/2014/02/19/the-trials-and-slow-successes-of-html5-development-using-cocoonjs-on-the-ouya/ https://gist.github.com/videlais/8110000

agmcleod commented 10 years ago

Hmm. This is kind of where I hope things like the built in web view for iOS and the tools for android get better and support more features that we get in browsers.

parasyte commented 10 years ago

I actually started working on this a while ago, but I stopped almost immediately when I learned Firefox does not support gamepad button press events; it requires polling, which as the common denominator means we have to implement gamepad support with polling if we want to support Firefox.

I think I would go back into this ticket thinking we won't care about the deprecated gamepad polling API, and just do it right.

aaschmitz commented 9 years ago

http://gamedevelopment.tutsplus.com/tutorials/using-the-html5-gamepad-api-to-add-controller-support-to-browser-games--cms-21345

aaschmitz commented 9 years ago

http://blogs.msdn.com/b/uk_faculty_connection/archive/2014/11/18/internet-explorer-and-xbox-controller-support.aspx

parasyte commented 9 years ago
agmcleod commented 9 years ago

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.

obiot commented 9 years ago

Two ideas I like better on my side :

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

agmcleod commented 9 years ago

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 notifications@github.com 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.

parasyte commented 9 years ago

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

parasyte commented 9 years ago

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

obiot commented 9 years ago

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 :

parasyte commented 9 years ago

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 commented 9 years ago

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:

parasyte commented 9 years ago

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.

obiot commented 9 years ago

(just for me to remember it tomorrow)

To create a mapping for FF :

    {
        "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]
    }
obiot commented 9 years ago

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

parasyte commented 9 years ago

Agreed. Great work!

obiot commented 9 years ago

Well you did most of it ! :)

CHANGELOG? :p