Game Development Community

How do I read the joystick?

by Mark Green · in Torque Game Builder · 08/01/2005 (2:26 pm) · 5 replies

I've turned on joystick input in defaults.cs and bound a procedure into an actionMap on the joystick xaxis event, but when I move the joystick nothing happens. Is there something else I need to be doing?

#1
08/01/2005 (7:38 pm)
You might find this thread useful.

http://www.garagegames.com/mg/forums/result.thread.php?qt=32873
#2
08/02/2005 (2:55 pm)
I've tried following things in that thread. Here's my client.cs at the moment..

//-----------------------------------------------------------------------------
// Torque 2D. 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

// --------------------------------------------------------------------
// Initialise Client.
// --------------------------------------------------------------------
function initialiseClient()
{
	// Initialise Base Client.
	InitBaseClient();
	
	// Key-Bindings.
	GlobalActionMap.bind(keyboard, tilde, ToggleConsole);
	GlobalActionMap.bindCmd(keyboard, "alt enter", "", "toggleFullScreen();");
	
		
	// Initialise Canvas.
	InitCanvas("T2D");

	
	// ************************************************************************
	// Load-up Demo Datablocks.
	// NOTE:-	Remove this is you're not interested in running the demos
	//			or any or the default datablocks.
	// ************************************************************************
	exec("./demoDatablocks.cs");
	
	
	
	// Load-up Datablocks.	
	exec("./datablocks.cs");
	// Load-up GUIs.
	exec("./mainScreenGui.gui");
	
	// Set GUI.
	Canvas.setContent(mainScreenGui);
	// Set Cursor.
	Canvas.setCursor(DefaultCursor);
	
   // Setup Scene.
	setupT2DScene();
}


// --------------------------------------------------------------------
// Destroy Client.
//
// Here we destroy the SceneGraph.
// --------------------------------------------------------------------
function destroyClient()
{
	// Destroy fxSceneGraph2D.
	if ( isObject(t2dSceneGraph) )
		t2dSceneGraph.delete();	
}

function gonuts(%val) {

        datablock fxImageMapDatablock2D(playershipImageMap)
        {
          mode = full;
          textureName = "~/client/images/enemyMissile";
        };

        for ($x=1; $x<360; $x+=5) {
          $player[x] = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
          $player[x].setPosition("-25 0");
          $player[x].setSize("3 1.5");
          $player[x].setImageMap(playershipImageMap);
          $player[x].setRotation(90+$x);
          $player[x].setLinearVelocityPolar($x, 20); 
          $player[x].setAngularVelocity(5);
          echo($x);
        }

        for ($x=1; $x<360; $x+=5) {
          $playera[x] = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
          $playera[x].setPosition("25 0");
          $playera[x].setSize("3 1.5");
          $playera[x].setImageMap(playershipImageMap);
          $playera[x].setRotation(90+$x);
          $playera[x].setLinearVelocityPolar($x, 20); 
          echo($x);
        }
}


// --------------------------------------------------------------------
// Setup T2D Scene.
// --------------------------------------------------------------------
function setupT2DScene()
{
	// Create fxSceneGraph2D.
	new fxSceneGraph2D(t2dSceneGraph);
        $pref::Input::JoystickEnabled = "1";
        $pref::Input::GamepadEnabled = "1";
	// Associate Scenegraph with Window.
	sceneWindow2D.setSceneGraph( t2dSceneGraph );	
	// Set Camera Position to be centered on (0,0) with
	// view width/height of (100/80).
	sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );
        new actionMap(playerMap);
        playerMap.bind(joystick, "xaxis", gonuts);
        playerMap.push();

}

The $pref lines I also put in prefs.cs and default.cs, but I stuck a copy of them in there because it was mentioned in the original thread that they get reset sometime during the initialisation. So I figured I should put them in again at the last moment..

The problem is, still nothing happens when I move the joystick X axis.
#3
08/03/2005 (6:36 am)
Mark

Do you have these 2 lines anywhere?

$enableDirectInput = "1";
   activateDirectInput();

That actually turns the joystick on.


BTW, the function gonuts() would live up to its name there ;) you get a lot of calls to a function from a joystick move. I think you also need to take out the datablock from the function.

How about writing it so it only goes nuts say every 2 seconds?

datablock fxImageMapDatablock2D(playershipImageMap)
    {
      mode = full;
      textureName = "~/client/images/enemyMissile";
    };


$GoneNuts = false;

function gonuts(%val) {

if ($GoneNuts == false )
   {
        $GoneNuts = true;

        for ($x=1; $x<360; $x+=5) {
          $player[x] = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
          $player[x].setPosition("-25 0");
          $player[x].setSize("3 1.5");
          $player[x].setImageMap(playershipImageMap);
          $player[x].setRotation(90+$x);
          $player[x].setLinearVelocityPolar($x, 20); 
          $player[x].setAngularVelocity(5);
          echo($x);
        }

        for ($x=1; $x<360; $x+=5) {
          $playera[x] = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
          $playera[x].setPosition("25 0");
          $playera[x].setSize("3 1.5");
          $playera[x].setImageMap(playershipImageMap);
          $playera[x].setRotation(90+$x);
          $playera[x].setLinearVelocityPolar($x, 20); 
          echo($x);
        }

schedule( 2000, 0, "GoNutsAgain" );

}
}


function GoNutsAgain()
{
   $GoneNuts = false;
}


Quite a pretty effect :)
#4
08/03/2005 (3:16 pm)
I'm not too worried about the multiple calls - that code was just me noodling around with the SDK and was used there to be something which was known to work and display stuff so I could tell a joystick event had been detected.

However, I've just found out that my physical joystick isn't working, which could explain it..

However, we're not out of the woods yet, as I tried doing the same thing with the mouse and that didn't work either. Current state of the setupT2Dscene():

// --------------------------------------------------------------------
// Setup T2D Scene.
// --------------------------------------------------------------------
function setupT2DScene()
{
	// Create fxSceneGraph2D.
	new fxSceneGraph2D(t2dSceneGraph);

        $pref::Input::JoystickEnabled = "1";
        $pref::Input::GamepadEnabled = "1";
        $pref::Input::MouseEnabled = "1";
        $pref::Input::enableDirectInput = "1";
        $enableDirectInput = "1";
        activateDirectInput();

	// Associate Scenegraph with Window.
	sceneWindow2D.setSceneGraph( t2dSceneGraph );
	
	// Set Camera Position to be centered on (0,0) with
	// view width/height of (100/80).
	sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );


        new actionMap(playerMap);
        playerMap.bind(mouse, "xaxis", gonuts);
        playerMap.push();

	// ************************************************************************
	//
	// Add your custom code here...
	//
	// ************************************************************************
	

	
}

I checked in the source code to actionMap and the comments state that the mouse should emit xaxis events..
#5
08/04/2005 (7:34 am)
Ok - for the mouse to work as a control you need 2 more commands :

...
...
[b]      lockMouse(true);
   Canvas.cursorOff();[/b]
   $enableDirectInput = "1";
   activateDirectInput();
...