Game Development Community

dev|Pro Game Development Curriculum

Full Xbox360 controller support for T2D

by Simon Love · 12/30/2013 (7:33 pm) · 8 comments

X360 Controller Support for T2D


A few notes before we start

- This is obviously only available on Windows.
- This has been tested with all button mappings and rumble values on an Xbox 360 controller.
- While Xinput should work on all modern joysticks and devices, it hasn't been tested with anything else than an X360 controller.
- The branch is not submitted as a pull request mainly because I haven't cleaned it up to meet the required standards. It is however based on the latest development branch as of 30/12/2013.
- I work on Visual Studio 2012 Express, while I haven't added or deleted any files from the solution, it SHOULD work with other compilers although I cannot test this.

For those who are too excited to read the post, Grab it here!

-------------------------------------------

What doesn't work

While many might think that T2D supports the Xbox 360 controller, there are a few features which make it unusable for any serious project.

1 - The Analog Triggers

In stock T2D MIT, both triggers are mapped to the zaxis.
This means that fully pressing one trigger brings the axis value to one extreme(1.0), while fully pressing the other trigger brings the value to the other opposite (-1.0).

Not touching the triggers is interpreted as the central value of the axis (0.0). Problem is, fully pressing both triggers down is interpreted as 0.0 as well.

Once you start slightly pressing one trigger and the other, you get values that will range from both extremes, making it pretty much impossible to detect which trigger was pressed and how far.

2 - Force Feedback

Simply put, there was no rumble feature in T2D.

-------------------------------------------

What was changed (and does work)

Simply put, I have taken the code from T3D MIT's Xinput implementation and ported it to T2D.
It was pretty straightforward, the main issue being how T3D and T2D differ in posting InputEvents to the Game Interface.

1 - The Analog Triggers

Now, each trigger is detected independently, mapped to triggerl and triggerr. If the trigger is not pressed, the value will be 0.0; if fully pressed, the value will be 1.0. Slightly pressing a trigger will produce a value between 0.0 and 1.0.

2 - Force Feedback

Taken straight from T3D MIT, we now have a function which sends rumbling to one of the 4 possible Xinput devices. Look at the MSDN Xinput reference online to fully understand how rumble works in an Xbox 360 controller.

Here's how you call it from script
rumble(gamepad0, 0.5, 0.5);

3 - Connect / Disconnect

In Xinput, when you connect or disconnect a controller, this is seen as an input event which you can act upon. This is now also implemented and accessible via the actionmap as connect

Basic setup


In order for Xinput to work, you need to place these functions before you declare an XInput-centric ActionMap

enableXInput();
  $enableDirectInput = true;
  activateDirectInput();

Once that is done, you can call echoInputState(); to be sure that Xinput is enabled and active.

After that, declare an ActionMap as you would normally do, using the following values.

- To bind an action to your Xbox 360 controllers, use : gamepad0, gamepad1, gamepad2, gamepad3

- All buttons and triggers can then be mapped using the following values :

"connect"

// L & R Thumbsticks:
"thumblx"
"thumbly"
"thumbrx"
"thumbry"

// L & R Triggers:
"triggerl"
"triggerr"

// DPAD Buttons:
"dpadu"
"dpadd"
"dpadl"
"dpadr"

// START & BACK Buttons:
"btn_start"
"btn_back"

// L & R Thumbstick Buttons:
"btn_lt"
"btn_rt"

// L & R Shoulder Buttons:
"btn_l"
"btn_r"

// Primary buttons:
"btn_a"
"btn_b"
"btn_x"
"btn_y"

So an example would be
MyActionMap.bind("gamepad0","btn_a",somefunction);
Grab it here!

About the author

I am here to help. I've worked at every imaginable position in game development, having entered the field originally as an audio guy.


#1
12/31/2013 (2:40 am)
I like it!
#2
12/31/2013 (5:48 am)
I wonder if it would work on mac too if you installed this:

https://www.macupdate.com/app/mac/24762/xbox-360-controller-driver
#3
12/31/2013 (5:54 am)
I know you said you pull some of the code from T3D. I was wondering if someone could expose this for T3D as well..But fantastic that T2D now can support the xbox360 controller.
#4
12/31/2013 (9:17 am)
Kory, it's already in T3D. There's a resource *somewhere* which gives all of the features or you can just look in the engine for the keybinds with a find-in-files search to bring up the input file.
#5
12/31/2013 (12:03 pm)
@Tim : From what I've read, I don't think it'll work. The magic trick is to load the Xinput .dll which is part of DirectX. From that point on, everything concerning the controller is handled opaquely by Xinput.

I don't think the drivers on the page you've shared actually implement Xinput. I've also heard that T2D's joystick support in OSX is broken.

Community superstar Paul Jan has a branch labeled "macJoystick" which might be worth checking out

@Kory : This all works in T3D MIT as is. The main difference is that T3D uses Signals to send its input messages while T2D uses Gameinterface::postevent(...) instead.
#6
01/01/2014 (2:54 am)
Which version of Torque 3D MIT support this?

MIT version 2.0 or 3.0 or 3.5?

Yaaa I know I can do a find search thing on it myself, but if I had some quick history about which version it would be cool :o)

Happy new years guys and awesome stuff Simon Love :o)

Best,

Dwarf King reloaded 2014 :o)

#7
01/01/2014 (3:16 am)
@Dwarf King : My liege, I've simply taken the latest development branch and made the modifications based on that.

Note that this isn't part of T2D 3.0; it's just something I happened to work on for 20 hours or so and decided to share with the community.

As it is an incomplete feature and hasn't been cleaned up, I probably won't submit it as a pull request. I encourage anybody who's got the time to review and enhance it for official submission.

It should be easy to merge with the current dev branch but be aware that I haven't implemented everything (Quartenion rotation and other weird Xinput device stuff) which means that it works for Xbox360 controllers but probably won't work 100% for other XInput-based controllers.
#8
05/08/2014 (3:02 pm)
Just an update for those who find this via forums, Xinput is now officially support in T2D 3.0 MIT's master branch : Available here