Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multiple joysticks - Joystick IDs  (Read 5197 times)

0 Members and 1 Guest are viewing this topic.

Vandenmills

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple joysticks - Joystick IDs
« on: May 05, 2013, 12:36:01 am »
Hi,

I have a small question regarding joysticks. As part of a school project we're making a game where we are using 4 xbox 360 joysticks (as of writing I only have access to 3 controllers though). Windows identifies the joystick with the right ID. SFML however doesn't.

I've made a for loop that looks like this:
for (unsigned int i = 0; i < 4; i++)
{
        if (sf::Joystick::isConnected(i))
        {
                std::cout << "Joystick " << i << " connected!" << std::endl;
        }
}

My current setup is that I have 3 controllers, 2 wireless (1 receiver) and 1 wired. If i unplug the wired controller and have only 1 wireless connected it says that Joystick 2 is connected.
If I connect the second wireless controller, it says that Joystick 1 and joystick 2 is connected.
If I plug in the wired controller it says that I have joystick 0, 1 and 2 connected. The wired controller also gets the first ID. Regardless how I do it, this is how it ends up.

When using Xinput to control rumble it rumbles on the wrong controller, because Xinput seems to follow the lights on the controller and not the ID sfml gives them.

My question is this, is there a way to get SFML to use the xbox controller lights the same way Xinput does?

I'm using SFML 2.0 & Visual studio 2010 (32bit).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Multiple joysticks - Joystick IDs
« Reply #1 on: May 05, 2013, 08:44:37 am »
I'm sorry, but I don't understand your problem. What's the relation between SFML and Xinput? What "lights" are you talking about?
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Multiple joysticks - Joystick IDs
« Reply #2 on: May 05, 2013, 08:58:12 am »
I'm sorry, but I don't understand your problem. What's the relation between SFML and Xinput? What "lights" are you talking about?
The XBox 360 controller has a ring of 4 lights around its center button. One of these four lights will light up on each controller that's plugged in, telling the player which player number they are. The ID SFML gives to each controller for sf::Joystick::isConnected( id ) and similar functions does not match up with the light on the controller. Additionally, when he uses XInput to tell a controller to rumble, he tries to use the ID given to him by SFML. This results in the wrong controller rumbling.

You can see the lights on the 360 controller here.
I use the latest build of SFML2

Vandenmills

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple joysticks - Joystick IDs
« Reply #3 on: May 05, 2013, 09:42:57 am »
Thanks OniLinkPlus for clarifying, that is exactly right :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Multiple joysticks - Joystick IDs
« Reply #4 on: May 05, 2013, 12:15:58 pm »
Ok! Thanks for making this clear.

The ID used by SFML is the one returned by the Win32 API, there's nothing I can do if it's the wrong number.
Laurent Gomila - SFML developer

Vandenmills

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple joysticks - Joystick IDs
« Reply #5 on: May 05, 2013, 12:56:11 pm »
Alright, thanks for taking the time to look into it :)

Found a solution by just switching the ID sent to the sf::Joystick::isPressed(). If the ID was 2 an if sentence made it into 1 and vice versa. It wasn't pretty but it worked.
(If someone has a better suggestion I'm all ears, I'm a newbie programmer and have programmed for less than a year).

Just thought I'd write the "solution" in case others run into the same type of problem.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Multiple joysticks - Joystick IDs
« Reply #6 on: May 16, 2013, 02:14:05 am »
Just let the users press a key in the joystick to define who is player 1, player 2 etc..

Vandenmills

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple joysticks - Joystick IDs
« Reply #7 on: May 21, 2013, 01:05:49 am »
If only it was that simple. We're going to use xbox 360 controllers, but we're modifying it, creating a different controller which will only have 3 buttons.

That said, the solution wasn't as easy as I wrote here. I ended up having to use Microsofts input library for the xbox controller. It's fine though, SFML works like a charm for the rest of the setup.