Game Development Community

A qustion that has been asked many timed - XBox Controllers in 1.5.2

by Gordon Platt · in Torque Game Engine · 10/07/2009 (8:55 pm) · 23 replies

Hi all,

I've just started using torque and am a fairly novice coder. I've been digging around for some info on getting a 360 controller to work in torque, most articles I've come accross seem out of date, or a little out of my depth.

After some trawling I found this:

// This code as been tested on WindowsXP for TGE 1.5 and TGB 1.1.3
function xbox360bind ()
{
//SETUP
$enableDirectInput = "1";
activateDirectInput();
enableJoystick();

//BUTTONS
moveMap.bindCmd(joystick, "button0", "echo("Pressed A");", "");
moveMap.bindCmd(joystick, "button1", "echo("Pressed B");", "");
moveMap.bindCmd(joystick, "button2", "echo("Pressed X");", "");
moveMap.bindCmd(joystick, "button3", "echo("Pressed Y");", "");
moveMap.bindCmd(joystick, "button4", "echo("Pressed LB");", "");
moveMap.bindCmd(joystick, "button5", "echo("Pressed RB");", "");
moveMap.bindCmd(joystick, "button6", "echo("Pressed BACK");", "");
moveMap.bindCmd(joystick, "button7", "echo("Pressed START");", "");
moveMap.bindCmd(joystick, "button8", "echo("Pressed L-ANALOG");", "");
moveMap.bindCmd(joystick, "button9", "echo("Pressed R-ANALOG");", "");

// ANALOG
moveMap.bind(joystick, xaxis, "LAnaglogX");
moveMap.bind(joystick, yaxis, "LAnaglogY");
moveMap.bind(joystick, rxaxis, "RAnaglogX");
moveMap.bind(joystick, ryaxis, "RAnaglogY");
moveMap.bind(joystick, zaxis, "LTRTAnaglog");

// D-PAD
// Press are release
moveMap.bindCmd( joystick, upov, "echo("Pressed UPOV");", "" );
moveMap.bindCmd( joystick, dpov, "echo("Pressed DPOV");", "" );
moveMap.bindCmd( joystick, lpov, "echo("Pressed LPOV");", "" );
moveMap.bindCmd( joystick, rpov, "echo("Pressed RPOV");", "" );
// Press before releasing of previous pov
moveMap.bindCmd( joystick, upov2, "echo("Pressed UPOV2");", "" );
moveMap.bindCmd( joystick, dpov2, "echo("Pressed DPOV2");", "" );
moveMap.bindCmd( joystick, lpov2, "echo("Pressed LPOV2");", "" );
moveMap.bindCmd( joystick, rpov2, "echo("Pressed RPOV2");", "" );
}
function LAnaglogX( %value )
{
if ( %value!= 0 ) echo( "Left Analog Stick X value = " @ %value );
}
function LAnaglogY( %value )
{
if ( %value!= 0 ) echo ( "Left Analog Stick Y value = " @ %value );
}
function RAnaglogX( %value )
{
if ( %value!= 0 ) echo ( "RightAnalog Stick X value = " @ %value );
}
function RAnaglogY( %value )
{
if ( %value!= 0 ) echo ( "RightAnalog Stick Y value = " @ %value );
}
function LTRTAnaglog( %value )
{
if ( %value!= 0 ) echo ( "LT / RT Analog value = " @ %value );
}


I guess this is all I need? And a silly question, where does this go?

What would the next step be to getting this to work in "GameOne" to show controller functionality (movement + jump would do for now)

I appreciate all feedback/pointers/help you have to offer.

Thank you for your time,


Gordon.

About the author

Recent Threads

Page «Previous 1 2
#1
10/11/2009 (9:20 pm)
Try putting the code in the default.bind.cs file located in

starter.fps/client/scripts directory
#2
10/14/2009 (3:15 pm)
Tried that, but to no avail.

I put it in the fps folder and in the tutorial.base folder. In the fps you have the option to change keybinds but it's not accepting any input from the control pad, and nothing in tutorial.base.

Should I be over-writing code, activating any code, setting any of the buttons up to directions?
#3
10/14/2009 (4:37 pm)
If the code works, try doing this instead.

go into your starter.fps/client/prefs.cs file and make sure your joystick is enabled like this:
$pref::Input::JoystickEnabled = "1";
then take the only this code:
//BUTTONS   
moveMap.bindCmd(joystick, "button0", "echo(Pressed A);", "");   
moveMap.bindCmd(joystick, "button1", "echo(Pressed B);", "");   
moveMap.bindCmd(joystick, "button2", "echo(Pressed X);", "");   
moveMap.bindCmd(joystick, "button3", "echo(Pressed Y);", "");   
moveMap.bindCmd(joystick, "button4", "echo(Pressed LB);", "");   
moveMap.bindCmd(joystick, "button5", "echo(Pressed RB);", "");   
moveMap.bindCmd(joystick, "button6", "echo(Pressed BACK);", "");   
moveMap.bindCmd(joystick, "button7", "echo(Pressed START);", "");   
moveMap.bindCmd(joystick, "button8", "echo(Pressed L-ANALOG);", "");   
moveMap.bindCmd(joystick, "button9", "echo(Pressed R-ANALOG);", "");   
  
// ANALOG   
moveMap.bind(joystick, xaxis, "LAnaglogX");   
moveMap.bind(joystick, yaxis, "LAnaglogY");   
moveMap.bind(joystick, rxaxis, "RAnaglogX");   
moveMap.bind(joystick, ryaxis, "RAnaglogY");   
moveMap.bind(joystick, zaxis, "LTRTAnaglog");   
  
// D-PAD   
// Press are release   
moveMap.bindCmd( joystick, upov, "echo(Pressed UPOV);", "" );   
moveMap.bindCmd( joystick, dpov, "echo(Pressed DPOV);", "" );   
moveMap.bindCmd( joystick, lpov, "echo(Pressed LPOV);", "" );   
moveMap.bindCmd( joystick, rpov, "echo(Pressed RPOV);", "" );   
// Press before releasing of previous pov   
moveMap.bindCmd( joystick, upov2, "echo(Pressed UPOV2);", "" );   
moveMap.bindCmd( joystick, dpov2, "echo(Pressed DPOV2);", "" );   
moveMap.bindCmd( joystick, lpov2, "echo(Pressed LPOV2);", "" );   
moveMap.bindCmd( joystick, rpov2, "echo(Pressed RPOV2);", "" );   
   
function LAnaglogX( %value )   
{   
if ( %value!= 0 ) echo( "Left Analog Stick X value = " @ %value );   
}   
function LAnaglogY( %value )   
{   
if ( %value!= 0 ) echo ( "Left Analog Stick Y value = " @ %value );   
}   
function RAnaglogX( %value )   
{   
if ( %value!= 0 ) echo ( "RightAnalog Stick X value = " @ %value );   
}   
function RAnaglogY( %value )   
{   
if ( %value!= 0 ) echo ( "RightAnalog Stick Y value = " @ %value );   
}   
function LTRTAnaglog( %value )   
{   
if ( %value!= 0 ) echo ( "LT / RT Analog value = " @ %value );   
}
And put it into your starter.fps/client/scripts/default.bind.cs file at the bottom (or anywhere, but the bottom is fine)

Then try pressing a button on the controller in-game and check your console for the echo's that the controller is sending.

You may or may not be able to reconfigure the keybinds through the options menu, it should however work in-game as echo's sent to your console.

I'm actually trying this code for my Logitech RumblePad 2, seeing that the code "looks" universal(other then some pressure sensitive buttons) it should work, but atm I can't get it to work, I'll update you if I can get it work work.
#4
10/14/2009 (5:32 pm)
Thank you for your response, I feel progress is being made! :)

The Joystick was indeed set to '0'.

In the console I get no feedback about the controller, however, I can set the digital buttons through the aforementioned config screen (in-game) including the left/right analog stick "presses" but not getting anything from the analog sticks x/y axis or the analogue triggers.

Any ideas?
#5
10/14/2009 (5:47 pm)
Strange, I'm actually getting no response.

Also, the original way the Echo statements were being passed was causing errors... however the way I have rewritten the Echo statements would probably be the reason your not seeing the echo's in the console, because the Echo needs the quotes for the string to be passed but if you use that in the bind then it corrupts the function.(hence why I rewrote the function... incorrectly, but no errors at least)
#6
10/14/2009 (6:18 pm)
Give this a try and see if it gives you the echo's:
//BUTTONS   
moveMap.bindCmd(joystick, "button0", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button1", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button2", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button3", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button4", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button5", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button6", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button7", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button8", GamePadEcho, "");   
moveMap.bindCmd(joystick, "button9", GamePadEcho, "");   
  
// ANALOG   
moveMap.bind(joystick, xaxis, "LAnaglogX");   
moveMap.bind(joystick, yaxis, "LAnaglogY");   
moveMap.bind(joystick, rxaxis, "RAnaglogX");   
moveMap.bind(joystick, ryaxis, "RAnaglogY");   
moveMap.bind(joystick, zaxis, "LTRTAnaglog");   
  
// D-PAD   
// Press are release   
moveMap.bindCmd( joystick, upov, GamePadEcho, "" );   
moveMap.bindCmd( joystick, dpov, GamePadEcho, "" );   
moveMap.bindCmd( joystick, lpov, GamePadEcho, "" );   
moveMap.bindCmd( joystick, rpov, GamePadEcho, "" );   
// Press before releasing of previous pov   
moveMap.bindCmd( joystick, upov2, GamePadEcho, "" );   
moveMap.bindCmd( joystick, dpov2, GamePadEcho, "" );   
moveMap.bindCmd( joystick, lpov2, GamePadEcho, "" );   
moveMap.bindCmd( joystick, rpov2, GamePadEcho, "" );   
  
function LAnaglogX( %value )   
{   
if ( %value!= 0 ) echo( "Left Analog Stick X value = " @ %value );   
}   
function LAnaglogY( %value )   
{   
if ( %value!= 0 ) echo ( "Left Analog Stick Y value = " @ %value );   
}   
function RAnaglogX( %value )   
{   
if ( %value!= 0 ) echo ( "RightAnalog Stick X value = " @ %value );   
}   
function RAnaglogY( %value )   
{   
if ( %value!= 0 ) echo ( "RightAnalog Stick Y value = " @ %value );   
}   
function LTRTAnaglog( %value )   
{   
if ( %value!= 0 ) echo ( "LT / RT Analog value = " @ %value );   
}  

function GamePadEcho()
{
   echo("Pressed a button on the GamePad");
}

If it does, this will help you better test what works, though it doesnt tell you as nicely as it was originally supposed to...

I still can't get this code to work for my gamepad, so it may be xbox only.
#7
10/14/2009 (6:52 pm)
I get nothing with an xbox controller. Here's where he got the original code from: tdn.garagegames.com/wiki/Script/Input_Keys_and_Maps
#8
10/14/2009 (7:01 pm)
Thanks for the link, it seems the slashes where cut from the orginal code hence why the echo's were not working correctly.

Edit: with the PS2 code I was to get the console to echo all the correct buttons and axis.(with the original code function)
#9
10/14/2009 (8:10 pm)
I would suggest using the XBox version as it was used on the original site, then when you start then game open the console with ~ and type
xbox360bind();

then you should recieve all the correct echo's from the controller, setting up the axis and buttons beyond that is going to be differant in most applications, and I am finding it a bit difficult to get the accuracy and functionality of most video game gamepad setups.
#10
10/15/2009 (12:50 am)
Once I type in xbox360bind(); all the echos work. But, the character doesn't move. Any thoughts?
#11
10/15/2009 (12:55 am)
you have to replace the echo's with the functions that you want to be used.

I'm still working on the axis though, I'll post them when I fix the look function.
#12
10/15/2009 (1:59 am)
OK, heres what I have so far:
function gamepadbind()
 {
     //SETUP
     $enableDirectInput = "1";
     activateDirectInput();
     enableJoystick();
     
     //BUTTONS
     moveMap.bind(joystick, "button3", autoMountVehicle);                       //Triangle
     moveMap.bind(joystick, "button2", nextWeapon);                             //Circle
     moveMap.bind(joystick, "button1", jump);                                   //X
     moveMap.bind(joystick, "button0", changePlayerPosition);                   //Square
     moveMap.bind(joystick, "button6", toggleOptics);                           //L2
     moveMap.bind(joystick, "button7", altTrigger);                             //R2
     moveMap.bind(joystick, "button4", toggleWeaponZoom);                       //L1
     moveMap.bind(joystick, "button5", mouseFire);                              //R1
     moveMap.bind(joystick, "button8", bringUpOptions);                         //Select
     moveMap.bindCmd(joystick, "button9", "escapeFromGame();", "");             //Start
     moveMap.bind(joystick, "button10", toggleFirstPerson);                     //L Toggle
moveMap.bindCmd(joystick, "button11", "commandToServer('WeaponReload');", "");//R Toggle
     
     // ANALOG
     moveMap.bind(joystick, xaxis, "LAnalogX");
     moveMap.bind(joystick, yaxis, "LAnalogY");
     moveMap.bind(joystick, rzaxis, "RAnalogX");
     moveMap.bind(joystick, zaxis, "RAnalogY");
     
     // D-PAD
     // Press are release
     moveMap.bindCmd( joystick, upov, "echo("Pressed UPOV");", "" );
     moveMap.bindCmd( joystick, dpov, "echo("Pressed DPOV");", "" );
     moveMap.bindCmd( joystick, lpov, "echo("Pressed LPOV");", "" );
     moveMap.bindCmd( joystick, rpov, "echo("Pressed RPOV");", "" );
     // Press before releasing of previous pov
     moveMap.bindCmd( joystick, upov2, "echo("Pressed UPOV2");", "" );
     moveMap.bindCmd( joystick, dpov2, "echo("Pressed DPOV2");", "" );
     moveMap.bindCmd( joystick, lpov2, "echo("Pressed LPOV2");", "" );
     moveMap.bindCmd( joystick, rpov2, "echo("Pressed RPOV2");", "" );
 }
 
 function LAnalogX( %value )
 {
     if ( %value!= 0 )
     {
     echo( "Left Analog Stick X value = " @ %value );
        
     $mvYaw =  (%value) / 20;
     
     // I cannot seem to do this part correctly...
     schedule(10, 0, MoveLJoystickX, %value ); //debugedit
     }
 }
 function LAnalogY( %value )
 {
     if ( %value!= 0 )
     {
      echo( "Left Analog Stick Y value = " @ %value );
        
     $mvPitch =  (%value) / 20;
     
     // I cannot seem to do this part correctly...
     schedule(10, 0, MoveLJoystickY, %value );  //debugedit
     }
 }
 
 function RAnalogX( %value )
 {
     //this is pt.1 of deadzone, my gamepad centered was returning a value of 1.5259e-005
     // and it was frequently off center so I incorporated this for a "deadzone"
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 ) 
     {
     echo ( "RightAnalog Stick X value = " @ %value );
     $mvBackwardAction = %value;
     
     }
     else
     ResetRJoystickX(); //pt.2 of calibration deadzone
 }
 function RAnalogY( %value )
 {
     //this is pt.1 of deadzone, my gamepad centered was returning a value of 1.5259e-005
     // and it was frequently off center so I incorporated this for a "deadzone"
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 )
     {
     echo ( "RightAnalog Stick Y value = " @ %value );
     $mvRightAction = %value;
     
     }
     else
     ResetRJoystickY(); //pt.2 of calibration deadzone
 }

 function ResetRJoystickY( %value )
 {
     $mvRightAction = 0; //pt.3 of calibration deadzone
 }
  function ResetRJoystickX( %value )
 {
     $mvBackwardAction = 0; //pt.3 of calibration deadzone
 }
 
 // these are "supposed" to keep the yaw & pitch value increasing
  function MoveLJoystickY( %value ) //debugedit
 {
     echo( "Left Analog Stick Y value = " @ %value );
        
     $mvPitch =  (%value) / 20;
 }
  function MoveLJoystickX( %value ) //debugedit
 {
     echo( "Left Analog Stick X value = " @ %value );
        
     $mvYaw =  (%value) / 20;
 }
#13
10/15/2009 (2:02 am)
I can't get the Left Joystick to keep moving the screen while being held at the same spot, atm it only moves the pitch/yaw whenever I move the joystick.(whenever the function is called)

However I need a way to keep calling the pitch/yaw function until %value = 0.(MoveLJoystickX & MoveLJoystickY)

Btw: from the looks of the XBox360 layout it looks like LT/RT are -/+ of the value returned (or +/- I dont have a 360 controller to verify).

Also you should be able call your "xbox360bind();" from inside the game.cs startGame() function.


Hope that helps guys!
#14
10/15/2009 (2:55 am)
Sweet thanks! ok, so far with the xbox controller:

firing(but, I can't stop firing)
Forward movement(the character is always moving forward without pressing anything)
Backwards movemnt
Camera movement with the Right Analog stick(but very sluggish)
Back button is set to quit because it won't let me use the "escape" key

I'll try and tackle it more later

#15
10/15/2009 (3:05 am)
So far this is the closest I've gotten the joystick to work with the axis sensitivity, with the Logitech Profiler software the gamepad emulation was a joke.

I'd be done(except for Rumble... which is something else entirely) with my Gamepad setup once I can figure out continuous yaw/pitch movement.

I would try the original XBox function first and check your dead center value, in my case it was (1.5259e-005 & -1.5259e-005)

I was also getting constant movement because my gamepad calibration was off slightly so I gave it a 0.1+/- deadzone which you probably need to verify on your controller as well, as it may be differant values.
#16
10/15/2009 (4:20 am)
Improved PS2-Style Mapping:
function gamepadbind()
 {
     //SETUP
     $enableDirectInput = "1";
     activateDirectInput();
     enableJoystick();
     
     //BUTTONS
     moveMap.bind(joystick, "button3", autoMountVehicle);                       //Triangle
     moveMap.bind(joystick, "button2", nextWeapon);                             //Circle
     moveMap.bind(joystick, "button1", jump);                                   //X
     moveMap.bind(joystick, "button0", changePlayerPosition);                   //Square
     moveMap.bind(joystick, "button6", toggleOptics);                           //L2
     moveMap.bind(joystick, "button7", altTrigger);                             //R2
     moveMap.bind(joystick, "button4", toggleWeaponZoom);                       //L1
     moveMap.bind(joystick, "button5", mouseFire);                              //R1
     moveMap.bind(joystick, "button8", bringUpOptions);                         //Select
     moveMap.bindCmd(joystick, "button9", "escapeFromGame();", "");             //Start
     moveMap.bind(joystick, "button10", toggleFirstPerson);                     //L Toggle
moveMap.bindCmd(joystick, "button11", "commandToServer('WeaponReload');", "");//R Toggle
     
     // ANALOG
     moveMap.bind(joystick, xaxis, "LAnalogX");
     moveMap.bind(joystick, yaxis, "LAnalogY");
     moveMap.bind(joystick, rzaxis, "RAnalogX");
     moveMap.bind(joystick, zaxis, "RAnalogY");
     
     // D-PAD: Not set ATM
     // Press are release
     moveMap.bindCmd( joystick, upov, "echo("Pressed UPOV");", "" );
     moveMap.bindCmd( joystick, dpov, "echo("Pressed DPOV");", "" );
     moveMap.bindCmd( joystick, lpov, "echo("Pressed LPOV");", "" );
     moveMap.bindCmd( joystick, rpov, "echo("Pressed RPOV");", "" );
     // Press before releasing of previous pov
     moveMap.bindCmd( joystick, upov2, "echo("Pressed UPOV2");", "" );
     moveMap.bindCmd( joystick, dpov2, "echo("Pressed DPOV2");", "" );
     moveMap.bindCmd( joystick, lpov2, "echo("Pressed LPOV2");", "" );
     moveMap.bindCmd( joystick, rpov2, "echo("Pressed RPOV2");", "" );
 }

 function LAnalogX( %value )//yaw
 {
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 ) 
     {
     echo( "Left Analog Stick X value = " @ %value );
        
     $mvYawLeftSpeed = %value / 20;
     }
     else
     ResetLJoystickX();

 }
 function LAnalogY( %value )//pitch
 {
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 ) 
     {
      echo( "Left Analog Stick Y value = " @ %value );
        
     $mvPitchUpSpeed = %value / 20;
     }
     else
     ResetLJoystickY();

 }
  function ResetLJoystickY( %value )
 {
     $mvPitchUpSpeed = 0;
 }
  function ResetLJoystickX( %value )
 {
     $mvYawLeftSpeed = 0;
 }

 function RAnalogX( %value )
 {
     //this is pt.1 of deadzone, my gamepad centered was returning a value of 1.5259e-005
     // and it was frequently off center so I incorporated this for a "deadzone"
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 ) 
     {
     echo ( "RightAnalog Stick X value = " @ %value );
     $mvBackwardAction = %value;
     
     }
     else
     ResetRJoystickX();
 }
 function RAnalogY( %value )
 {
     //this is pt.1 of deadzone, my gamepad centered was returning a value of 1.5259e-005
     // and it was frequently off center so I incorporated this for a "deadzone"
     if ( %value>= 0.1 || %value<= -0.1 && %value!= -1.5259e-005 && %value!= 1.5259e-005 )
     {
     echo ( "RightAnalog Stick Y value = " @ %value );
     $mvRightAction = %value;
     
     }
     else
     ResetRJoystickY();
 }
 function ResetRJoystickY( %value )
 {
     $mvRightAction = 0;
 }
  function ResetRJoystickX( %value )
 {
     $mvBackwardAction = 0;
 }
#17
10/15/2009 (4:25 am)
Sorry had to add another post because the last was at the 5000-char limit.

This code still needs some tweaking such as $pref deadzone, sensitivity and inverting, but it should be a good start for getting an xbox360 controller working.

Note: be careful of the bind and bindCmd when setting up your own functions.

I'll add the $pref variables later, as I'm tired as hell and happy there were functions already available to make things easier... (too bad I couldnt find them sooner, lmao)

Hope it helps. :)
#18
10/15/2009 (12:17 pm)
thanks the bind and bindCmd were throwing me off. That's why I couldn't jump.
#19
10/16/2009 (1:17 am)
Looks like TGEA has rumble, but I'm not sure about TGE:
www.garagegames.com/community/forums/viewthread/91717
#20
10/16/2009 (2:14 am)
Problem is I'm using DirectInput and I would have to reset all my binds if it has to use Xinput.(given TGE has Xinput, either way if it is in TGEA it can be ported)

I'm actually going to start a new thread for the rumble function.
Page «Previous 1 2