sharpdx - How to use xbox one controller in C# application -
there exists large amount of information using xbox 360 controller in c#, didn't find info xbox 1 controller.
i need basic operation out of it, joystick , trigger values.
the majority of information online c++ applications, rather attempting write custom library, i'd use sharpdx. how use in application?
note: i'm posting share info found, documenting findings myself. i'd love hear other methods of getting controller input .net application though.
the easiest way found started use sharpdx. sharpdx website:
sharpdx open-source managed .net wrapper of directx api.
sharpdx available on nuget, it's simple started inside visual studios.
to started, go tools -> nuget package manager -> package manager console
inside visual studios (i'm using 2015 community edition).
then type: install-package sharpdx
package manager console appears @ bottom of visual studios.
visual studios download , add solution. onto code.
since wanted input xbox controller, need add:
using sharpdx.xinput
top of our program.
and code values simple:
class xinputcontroller { controller controller; gamepad gamepad; public bool connected = false; public int deadband = 2500; public point leftthumb, rightthumb = new point(0,0); public float lefttrigger, righttrigger; public xinputcontroller() { controller = new controller(userindex.one); connected = controller.isconnected; } // call method update class values public void update() { if(!connected) return; gamepad = controller.getstate().gamepad; leftthumb.x = (math.abs((float)gamepad.leftthumbx ) < deadband) ? 0 : (float)gamepad.leftthumbx / short.minvalue * -100; leftthumb.y = (math.abs((float)gamepad.leftthumby ) < deadband) ? 0 : (float)gamepad.leftthumby / short.maxvalue * 100; rightthumb.y = (math.abs((float)gamepad.rightthumbx) < deadband) ? 0 : (float)gamepad.rightthumbx / short.maxvalue * 100; rightthumb.x = (math.abs((float)gamepad.rightthumby) < deadband) ? 0 : (float)gamepad.rightthumby / short.maxvalue * 100; lefttrigger = gamepad.lefttrigger; righttrigger = gamepad.righttrigger; } }
this creates deadband because joysticks on controller never return zero. inverts left x axis both stick inputs match. y positive up, x positive right:
leftthumb.x = (math.abs((float)gamepad.leftthumbx ) < deadband) ? 0 : (float)gamepad.leftthumbx / short.minvalue * -100; leftthumb.y = (math.abs((float)gamepad.leftthumby ) < deadband) ? 0 : (float)gamepad.leftthumby / short.maxvalue * 100; rightthumb.y = (math.abs((float)gamepad.rightthumbx) < deadband) ? 0 : (float)gamepad.rightthumbx / short.maxvalue * 100; rightthumb.x = (math.abs((float)gamepad.rightthumby) < deadband) ? 0 : (float)gamepad.rightthumby / short.maxvalue * 100;
hopefully helpful in future looking add xbox 1 controller input .net application!
Comments
Post a Comment