USB game controller

From Pin Eight
Revision as of 17:55, 29 January 2013 by Tepples (Talk | contribs)

Jump to: navigation, search

Contents

Button layouts

Unlike controllers for a single game console, USB Human Interface Device game controllers for a PC have their buttons in different layouts. Even if the buttons are in the same layout, they may be presented to the application in a different order. So unlike a console game, a PC game cannot assume that, say, "button 1 = jump". This section describes how various controllers are seen by SDL on Linux. All tests were performed with a Pygame program under Xubuntu 12.04 "Precise Pangolin".

Controllers

The original iMac was the first popular personal computer to have USB as its only connection for external input, storage, and output devices. USB ports became common on other brands of PC starting in 1999. Once USB became standard and sound cards stopped carrying the 15-pin game ports to which joysticks had previously connected, companies started producing USB game controllers specifically for the PC.

Gravis GamePad Pro USB

Controller Gravis GamePad Pro.jpg

One of the first USB game controllers, with the same physical layout as the original PlayStation controller.

  • Axes: 2
    1. Control Pad X
    2. Control Pad Y
  • Buttons: 10
    1. Red (left)
    2. Yellow (down)
    3. Green (right)
    4. Blue (up)
    5. L1
    6. R1
    7. L2
    8. R2
    9. Select
    10. Start

Logitech Logitech Dual Action

Controller Dual Action.jpg

A controller with the same layout as Sony's Dual Shock, except the sticks' restrictors are square, not circular. When the "mode" switch is turned on, the left stick is swapped with the digital Control Pad. Surprisingly, buttons 1-10 are in the same order as on the Gravis.

  • Axes: 4
    1. Left stick X
    2. Left stick Y
    3. Right stick X
    4. Right stick Y
  • Buttons: 12
    1. Square (Left)
    2. X (Down)
    3. Circle (Right)
    4. Triangle (Up)
    5. L1
    6. R1
    7. L2
    8. R2
    9. Select
    10. Start
    11. L3
    12. R3
  • Hats: 1
    1. Control Pad

Logitech Logitech(R) Precision(T

Controller Logitech Precision.jpg

This controller is essentially the same thing as the Dual Action, except with no analog sticks and the "mode" switch always on. It's similar to the original PlayStation controller with larger L2 and R2 buttons like those of the Dual Shock.

  • Axes: 2
    1. Control Pad X
    2. Control Pad Y
  • Buttons: 10; same placement and order as the first ten buttons of the Dual Action

Microsoft X-Box 360 pad

Controller Xbox 360.jpg

This is a wired Xbox 360 Controller. Its Linux driver misspells "Xbox". The layout below is for SDL on the Linux driver; the Windows driver appears to produce the difference RT - LT as one axis, with no second axis for LT + RT, in order to encourage developers to switch from DirectInput to the XInput API, which is exclusive to Xbox 360 controllers. (Another thing to encourage use of XInput is the fact that Windows Store applications cannot use DirectInput and thus cannot use any controller other than the Xbox 360 Controller.)

  • Axes: 6
    1. Left stick X
    2. Left stick Y
    3. LT
    4. Right stick X
    5. Right stick Y
    6. RT
  • Buttons: 11
    1. A (Down)
    2. B (Right)
    3. X (Left)
    4. Y (Up)
    5. LB
    6. RB
    7. Back
    8. Start
    9. Guide (the "hot cross bun" button in the middle)
    10. L3
    11. R3
  • Hats: 1
    1. Control Pad

Microsoft makes a receiver for Xbox 360 wireless controllers that plugs into a PC's USB port, but I haven't had a chance to test it.

HID 1267:2afb

Controller SNES USB.jpg

This is a Chinese no-name clone of the Super NES controller, except with lighter plastic and a USB HID protocol instead of the SPI-like protocol that the NES and Super NES controllers use.

  • Axes: 2
    1. Control Pad X
    2. Control Pad Y
  • Buttons: 8
    1. Y
    2. X
    3. B
    4. A
    5. L
    6. R
    7. Select
    8. Start

Adapters

Evidently a lot of people are dissatisfied with the Control Pad on no-name and Gravis controllers, preferring that of a Nintendo or Sony console. Several companies have produced adapters to use controllers for classic game consoles with any PC game that supports USB HID controllers.

Wish Technologies Adaptoid

Controller Adaptoid.jpg

Adapter to use Nintendo 64 controllers on a USB HID host. As with the Dual Analog, there is a way to swap the Control Pad and Control Stick. While holding L, R, and Z, press Up on the Control Pad three times to enable swapping or move the Control Stick up three times to disable it.

  • Axes: 2
    1. Control Stick X
    2. Control Stick Y
  • Buttons: 14
    1. A
    2. C Down
    3. C Right
    4. B
    5. C Left
    6. C Up
    7. L
    8. R
    9. Start
    10. Z
    11. Control Pad Up
    12. Control Pad Down
    13. Control Pad Left
    14. Control Pad Right

HID 0b43:0003

Controller EMS USB2.jpg

The EMS USB2 adapter can be switched between two modes. One mode allows the use of an original Namco Guncon with PS2 software designed for the Guncon 2. The other mode, described here, allows using one or two PlayStation, Dual Shock, or Dual Shock 2 controllers on a USB HID host. Some people report it works better when plugged into a hub than when plugged directly into the PC. It has become popular among StepMania users because Control Pad directions are always returned as buttons, even in digital mode, so that Left+Right and Up+Down presses on a dance pad work correctly. An original PlayStation controller or a Dual Shock or Dual Shock 2 with the Analog button turned off will replace all joystick axes with the corresponding direction on the Control Pad. The buttons on a Dual Shock 2 are not pressure-sensitive; it is treated as an original Dual Shock.

  • Axes: 4
    1. Left stick X
    2. Left stick Y
    3. Right stick Y
    4. Right stick X
  • Buttons: 14
    1. Triangle
    2. Circle
    3. X
    4. Square
    5. L2
    6. R2
    7. L1
    8. R1
    9. Select
    10. Start
    11. L3
    12. R3
    13. Control Pad Up
    14. Control Pad Right
    15. Control Pad Down
    16. Control Pad Left

Conclusions

The primary axes are either the Control Pad or the left stick. Buttons come in a rough order: face buttons, then shoulder buttons, then Select and Start, then buttons under sticks, and finally Control Pad directions if not assigned to a hat. But the order and number of buttons within a category are unpredictable, as is which button the user expects to use for each action.

MUST
SHOULD
MAY
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119. (what's this?)

An application that doesn't recognize a particular controller brand SHOULD do the following:

  • In the menu, map the primary axes to move, buttons 1 and 3 to OK, and 2 and 4 to Back.
  • In the game, map face and shoulder buttons for an Xbox 360 controller, and map 7 and 8 to Pause unless the controller has fewer than eight buttons, in which case the last should be Pause.
  • From both the game's menu and the pause menu, include a configuration form that can be operated with the menu bindings.
  • Save the result of this form after the user completes it.

To make things easier for the user, the form SHOULD be preloaded with layouts for the most widespread controllers. All XInput controllers use the layout for Xbox 360, and the layout for Gravis GamePad Pro USB, Logitech Precision, and Logitech Dual Action appears to be fairly widespread as well. If the user selects "Custom", ask the user to press the button for each game action in sequence. Assigning multiple buttons to a given action can be done later, after the basic configuration is in place.

Main Menu
Continue
New Game
⌥ Options
Exit
Options
🎮 Control
🎨 Graphics
🎧 Sound
← Back
Control
Show a drawing of the controller with button functions overlaid.
[Xbox 360 Standard] (or whatever controller was detected)
Custom Layout
🔧 Fine-Tune
← Back
Custom Layout
Press the following
buttons in order:
[Up], Down, Left, Right,
Jump, Attack, Change Tool,
Pause
Game Paused
► Continue
Abort Mission
⌥ Options

The player may end up starting a game with the wrong controls and not knowing how to get back to the menu to configure the controls. Such a player would have to press all the buttons on the controller but would eventually end up pressing the button whose number is the same as that of the Back or Start button on the Xbox 360 controller, or in the case of a controller with few buttons, the last button. (Based in part on recommendation by Belial6.)

Reading the controller

An analog joystick returns displacements along two axes: X (horizontal) and Y (vertical). These readings include a small amount of noise. So the way axes are mapped SHOULD have a dead zone, or an area at the center of the range where the stick is considered centered.

There are two ways to convert the X and Y axes to a direction: separable and polar. A "separable" interpretation treats the X and Y axes independently, giving each axis its own dead zone. The size of the dead zone controls how easy it is to hit the straight and diagonal areas, and it's next to impossible to go directly from centered to diagonal. Separable interpretation is useful for side-view games, where the X axis moves left and right and the Y axis looks up and down or jumps and crouches.

A "polar" interpretation first determines how far and at what angle the stick is moved. The straight sectors can be the same size as the diagonals (45 degrees), or it can be biased toward straight movement (60 degrees). The angle measurement need not use time-consuming trigonometry or square roots. It takes only two multiplies and three adds to calculate whether the squared displacement is greater than the square of the dead zone radius (x2 + y2 > r2), and then calculate whether min(|x|, |y|) > m * max(|x|, |y|), where the slope threshold m (0 < m < 1) is the tangent of half the width of the straight sector.

Here are plots of the nine possible zones for the six combinations of separable, polar with 45 degree straight sectors, and polar with 60 degree sectors, at both 25% and 50% dead zone radius.

Some games allow no diagonal movement at all, such as top-down games with 4-way movement (Pac-Man; Pokemon) and block puzzle games that don't let the user move a piece sideways and drop it at the same time. The best way I've found to handle this is to use a bit of hysteresis: if the diagonal is pressed, use the most recent straight sector (if it's adjacent). This works with separable interpretation or with 60-degree straight sectors.

Even on controllers with a digital Control Pad, some cheaper controllers may make it too easy for the player to accidentally press diagonally. This might make the character in a platformer suddenly switch from a run (Right) into a crawl (Down+Right). This can be prevented with a similar technique to the diagonal: only allow Down+Right to be entered from Down. Holding Right and pressing Down would continue moving to the right; to roll or crawl, the player would first have to roll the Control Pad to Down then back to Down+Right, as if performing a Dragon Punch. But games SHOULD still offer a way to turn this compensation off in case the user is using a higher-quality controller, such as a keyboard, an N64 controller through an Adaptoid, a Dual Shock through an EMS USB2, or a Wii Classic Controller through a Mayflash PC045.

And don't depend on the player being able to reach the four corners with the joystick. The thumb sticks on the Xbox 360 and PlayStation controllers, for example, have a circular restrictor. So scale each joystick's dead zone size relative to the maximum displacement that the program has seen on that joystick in this play session.

Personal tools