XBox360 Controller

BlitzMax Forums/BlitzMax Programming/XBox360 Controller

col(Posted 2011) [#1]
Hi all,

Download HERE
DOWNLOAD V2.0 HERE

For WIRED XBox360 controllers on Windows :-

I saw that there were some XBox360 controller code to allow using the controller in BMax. This suffered with a slight flaw in that the thumb buttons would cancel each out when they are both pressed at the same time resulting in incorrect thumb trigger values.

As I've decided to incorporate the controller as a standard controller in my game engine I decided to share the code.

I've written a module but for use only with Windows ( XP with SP1 or better ) in Bmax that overcomes the thumb trigger problem so you can use all the stick / triggers / buttons correctly. Unfortunately there's still no support for the X Button in the centre - For some reason Microsoft decided not to expose that button in DirectX.

How to use :-

BEFORE USING THIS SOFTWARE PLEASE MAKE SURE YOU HAVE THE LATEST DIRECTX UPDATE - even if you have DirectX 11 this statement still applies.

Installation-

Put/copy the folder srs.mod from the above download into your BlitzMax/mod folder, then rebuild your modules to build this module. The controller code is now ready to use.

Obviously you should plug in your XBox360 controller into a USB port :-)

To use in your code you need to import the module. Use

Import SRS.Xbox360

Then create an instance of the 'XBox360' Type like so

Global XB:XBox360 = new XBox360

before reading any data from any controllers use

XB.Update

You MUST call this before getting any data from the controllers, once per frame is ok, then use the following methods to get information about the controller. You need to pass in an ID value ranging from 0 to 3 inclusive for the corresponding controller as shown below

To see if a controller is connected use
This returns TRUE is a controller is connected or FALSE otherwise.
XB.IsConnected( ID )

These return TRUE of FALSE, TRUE if pressed, FALSE otherwise
XB.A( ID )
XB.B( ID )
XB.X( ID )
XB.Y( ID )
XB.Start( ID )
XB.Back( ID )
XB.LeftShoulder( ID )
XB.RightShoulder( ID )
XB.LeftThumb( ID )
XB.RightThumb( ID )


To get the condition of the D-Pad use a combination of the following
These also return TRUE for pressed, FALSE otherwise
XB.DPadUp( ID )
XB.DPadDown( ID )
XB.DPadLeft( ID )
XB.DPadRight( ID )


The trigger values will return a value between 0 and 255 inclusive. 0 for no action, 255 for fully pressed.
XB.LeftTriger( ID )
XB.RightTrigger( ID )


To retrieve the state of the analogue thumb stick positions, the values will return between 0 and 65535 inclusive. When the stick is centred the value is 32768. To access the thumb stick values use
XB.LeftStickHorizontal( ID )
XB.LeftStickVertical( ID )
XB.RightStickHorizontal( ID )
XB.RightStickVertical( ID )


To use the rumble features you use the following methods passing in the ID of the controller to set and also a value between 0 and 65535 inclusive. 0 is no vibration, 65535 is full vibration.
XB.SetSmallVibration( ID , value )
XB.SetBigVibration( ID , value )


You can also adjust the 'Dead Zone'. This is a zone of movement for the thumb sticks only. Its where you will get no controller response until you move a certain distance from the centre position. The value to set it to is between 0 and 1, 0 is no dead zone ( causing erratic sensitive movement data when the stick is at the centre position ) and 1 would be no stick movement detection. It defaults to 0.25. I advise you NOT to use this method but just in case you want to
XB.SetDeadZone( ID , value )

I hope some people will find it useful for their games. Please give proper credit to SRS Software if you decide to use it.

A complete example of how to use is here. In this example you can use the arrow keys to adjust the amount and type of rumble to inflict. Left and Right will adjust the small finer vibration, using Up and Down will adjust the bigger harsher vibration.


Many thanks

Last edited 2011


col(Posted 2011) [#2]
I imagine some newbie programmers may need some help with the values for the sticks.

Once you have received the value of a stick, in the range from 0 to 65535, it would be nice to scale this to the range of -1 to 1 with 0 being the centre. To do this you could use the following snippet to get the result for controller ID 0 -

For eg
To scale between -1 and 1 on the horizontal part of Left Analogue Stick use
Local ID:int = 0
Local mX:Float = Float( XB.LeftStickHorizontal( ID ) ) / 32768.0 - 1.0


mX will contain the value between -1 and 1

To do scale the thumb trigger values to a range of between 0 and 1, 0 being nothing pressed and 1 being fully pressed, you can use the following snippet
Local Trigger:float = Float( XB.LeftTrigger( Id ) ) / 255.0

'Trigger' will now be a varying result between 0 and 1 depending on how hard you press the Left Trigger.

See ya

Last edited 2011


Czar Flavius(Posted 2011) [#3]
Wow, that is very impressive!! Well done :D

Maybe you could add a second convenience function to return the stick value as a float in your module.


col(Posted 2011) [#4]
Thanks Czar Flavius.

V1.1:
Added some methods to return the analogue values as a float number. Its simply a case of the same method names but with an 'F' appended.

These will return a value between -1.0 and 1.0
XB.LeftStickHorizontalF( ID )
XB.LeftStickVerticalF( ID )
XB.RightStickHorizontalF( ID )
XB.RightStickVerticalF( ID )


And these will return a value between 0.0 and 1.0
XB.LeftTriggerF( ID )
XB.RightTriggerF( ID )


See ya

[EDIT]
V1.2 : Fixed a small bug that would cause the Float values of the Analogue Sticks to not quite reach the full range of -1.0 to 1.0 - Now they work as they should.

Last edited 2011


col(Posted 2011) [#5]
DOWNLOAD V2.0 HERE

While using this module I noticed that getting the update of the controllers takes a fair amount of CPU time. It was slowing my engine cpu code from an UN-Synced 2500FPS to 400FPS.

So here I present V2.0 which features multi-threading. This completely eliminates any slow down, bringing the UN-Synced framerate back up to 2500FPS.

To install it is identical as above in the very first post.

To use the Multi-Thread feature, Just select the 'Multi-thread' build option in your editor, and it will auto-magically use the multi-thread version. When using the multi-thread version there's no need for

XB.Update

as this is handled in a different thread.

Don't forget to 'Rebuild Modules' with both multi-thread SELECTED and UN-SELECTED before using it.

Have fun.

Last edited 2011