quinton-ashley / p5play

JavaScript game engine that uses q5.js/p5.js for graphics and Box2D for physics.
https://p5play.org
GNU Affero General Public License v3.0
667 stars 184 forks source link

If two or more controllers are connected they are considered the same controller #327

Open Dukemz opened 4 months ago

Dukemz commented 4 months ago

image

If two controllers/gamepads of the same type/ID are connected, p5play treats them as the same (in the attached screenshot, they are both considered to be contros[0]).

image

I'm not completely sure why this happens? Possibly because gamepad index isn't taken into account?

quinton-ashley commented 4 months ago

What version of p5play are you using?

Dukemz commented 4 months ago

3.22.11, forgot to include in the post my bad

quinton-ashley commented 4 months ago

Thanks for reporting the issue but I don't know why it occurs.

Take a look at the _Contros._onConnect function:

https://github.com/quinton-ashley/p5play/blob/e587c7f9e78b50ed14046c5c15667f6bd3c1f68a/p5play.js#L10563

The indexes of controllers in p5play's contros array differ from the HTML5 navigator.gamepads array because I wanted reconnected controllers to be at the same index in the contros array and new controllers should be added to the next available slot in the controllers array (if previous controllers were removed).

If contros[0] already exists it shouldn't be possible to overwrite it. Can you try to set breakpoints in p5play.js to see why index is still 0 when the second controller is added?

Dukemz commented 3 months ago

The second time the _onConnect function is called it seems that this is an array with just 1 object image which as a result causes index to be 0. Looking at the MDZN documentation, it says that controllers that are disconnected will be replaced in the array returned by navigator.getGamepads() with null, but will remain in the same index. image I'm still not entirely sure what's going on but I'll reply again when I've figured out some more details.

Dukemz commented 3 months ago

image This seems to be the most reliable fix. The index should stay the same between disconnects/reconnects. Edit: see below comment

Dukemz commented 4 weeks ago

Bug still present in 3.23. Putting what Quinton said on the p5play discord server here for anyone not in the server. image

quinton-ashley commented 3 weeks ago

@Dukemz I will take another look at this soon.