Brandon.ToString()

Brandon blogs, just for you.

XBox Controller in .NET 3.5 with 3 lines of code

with 3 comments

XBox controllers are pretty cool little hardware devices. So why not use them in .NET to control something cool?

XBoxController

The only problem is, there’s not a whole lot of code out there for any recent version of .NET, and nobody really likes learning Managed DirectX for fun… In addition, the bits of code that I did actually find, while useful for getting the hardware connected, felt like hacks to actually use it. So…

I’ve pieced together most of the XBox code into what I hope is a clean, ready-to-use and complete .NET DLL to get connected up to a controller and using it in 3 lines of code. Credit goes to some of the code at MDXInfo for giving me a good head start.

First, download the DLL:

http://static.x9tech.com/XNT/XBoxController/X9Tech.XBox.Input.zip

Add it as a reference to your .NET project. Then, use it like so:

 X9Tech.XBox.Input.XBoxControllerManager xcm = new XBoxControllerManager();
 var controllers = xcm.GetConnectedControllers(); // get the collection of connected controllers
 var controller1 = controllers[0]; // the controller at index 0 will be controller #1
 if (controller1.ButtonAPressed) {
 // do something if Button A is pressed...
 }

 

The XBoxControllerManager class simply has a method to GetConnectedControllers() which gives you a List<XBoxController> collection of connected controllers.

This little library addresses the following common issues when dealing with XBox Controllers…

Percentages instead of raw values – In reality, the thumb pads have values between –32,767 to +32,768. The triggers have values of 0 to 65,000. For all practical purposes, these are pretty meaningless, so these types of ranges are converted to percentages (as a double with crazy precisions). When each thumb pad is at its center position, this is 50% X / 50% Y. So from here on out, we’re going to be talking percentages, mmkay?

Trigger On/Off – Left and right triggers (the little, well, triggers that resemble the trigger of a gun) on the front of the controller) are, in reality, never actually “on” or “off”, they’re in between 0-100%. However, you may notice that the XBoxController class has two boolean properties called TriggerLeftPressed and TriggerRightPressed. These are inferred based on the current trigger percentage value, and two more properties (TriggerLeftPressThreshold / TriggerRightPressThreshold), which define the minimum percentage values the trigger needs to meet in order to be considered pressed. Defaults to 10%, as I found this was a pretty comfortable place to consider it “pressed”.

Dead Zone – The thumb pads do have springs to snap them back to “center”. However, with 65,535 positions on each axis, rarely will “center” ever mean the same thing twice. If you’re counting on 50/50 being “no thumb on the thumbpad”, this will drive you insane. So, I needed some kind of a ‘dead zone’ to go ahead and assume 50/50 when it’s in the “close enough to be considered the center” area. Through sheer trial and error I determined that a reliable center is anywhere from a 40% to 60% position. If you want this to be more or less sensitive, just set the SnapDeadZoneTolerance property. The tolerance is the percentage value that we consider to be in the “dead zone” on either side of 50%. So, for a Dead Zone Tolerance of 10, this means “10% on either side of 50%”, so 40% – 60%. A tolerance of 5 would mean the dead zone is between 55% – 65%, and of course a tolerance of 0 would disable the assumed dead zone.

Controller Polling / Refresh – To get new controller state information, we have to poll the XBox Controller. This little library will handle it automatically, so you can just grab properties and go. By default, this will poll the device a maximum of every 30 milliseconds. This felt comfortable to me, but if you need faster or slower polling, just adjust the RefreshIntervalMilliseconds property.

Controller Connect/Disconnect – At some point, your user will disconnect the controller while your application is using it; nothing should crash, and the controller’s IsConnected property should return false once it’s disconnected with no blow ups. Reconnection happens automagically.

Vibration Motors – The XBox Controller has 2 vibration motors, a left and right. The left motor (let’s call it “the big vibrations”), when set to 100%, will send your XBox controller walking across whatever surface it’s on. The right motor resembles what’s in your cell phone. You can set the motors (again, with percentages here) by calling SetLeftMotorVibrationSpeed() and SetRightMotorVibrationSpeed().

That’s about it. If you use this library and there’s anything I have forgotten, I’d love to hear. Happy coding!

About these ads

Written by Brandon

December 28, 2009 at 1:18 pm

3 Responses

Subscribe to comments with RSS.

  1. Fun! You should put the code on CodePlex or something.

    Rex M

    December 28, 2009 at 1:31 pm

  2. Great, now I have to go buy an XBox to play with this code you’ve posted. :-)

    Chris Ballance

    December 28, 2009 at 5:20 pm

  3. I’m a few years late to this party, but this does what it says on the tin, and I am pleased.

    Harold

    September 8, 2012 at 8:09 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: