Page 1 of 1

Print joystick positions to console

PostPosted: Sun Jan 19, 2014 11:24 pm
by SM Sith Lord
Does anybody know how to print the Joystick analog stick positions to the console? I've been trying all day to get it working, but I've gotten nowhere.

I found in_joystick.cpp and I also found a function Joystick_GetForward in a few places (such as iinput.cpp), but I can't figure out how to call it.

It is not on the method list for input().

Re: Print joystick positions to console

PostPosted: Sun Jan 19, 2014 11:50 pm
by SM Sith Lord
I'm just going to hook into this function for now:
Code: Select all
void CInput::JoyStickMove( float frametime, CUserCmd *cmd );


But it seems like there is a better way to do this.

Re: Print joystick positions to console

PostPosted: Mon Jan 20, 2014 12:50 am
by SM Sith Lord
I finally got it all working, but it was a lot harder than just polling for a position value.

For future reference, inputsystem->GetAnalogValue() is how you get the Joystick input. However, dealing with the Joystick input is complicated.

There are lots of joysticks, and thresholds to worry about. Look at CInput::JoyStickMove() in in_joystick.cpp for more info.

Re: Print joystick positions to console

PostPosted: Mon Jan 20, 2014 1:49 am
by SM Sith Lord
First you need to initialize some stuff:
Code: Select all
   #include "inputsystem/iinputsystem.h"

   #define JOY_ABSOLUTE_AXIS 0x00000000
   #define JOY_RELATIVE_AXIS 0x00000010

   enum
   {
      GAME_AXIS_NONE = 0,
      GAME_AXIS_FORWARD,
      GAME_AXIS_PITCH,
      GAME_AXIS_SIDE,
      GAME_AXIS_YAW,
      MAX_GAME_AXES
   };

   unsigned int m_axesValues[MAX_JOYSTICK_AXES];

   // Grab some ConVars
   ConVar* joy_forwardthreshold = cvar->FindVar("joy_forwardthreshold");
   ConVar* joy_sidethreshold = cvar->FindVar("joy_sidethreshold");
   ConVar* joy_pitchthreshold = cvar->FindVar("joy_pitchthreshold");
   ConVar* joy_yawthreshold = cvar->FindVar("joy_yawthreshold");
   ConVar* joy_advanced = cvar->FindVar("joy_advanced");
   ConVar* joy_advaxisx = cvar->FindVar("joy_advaxisx");
   ConVar* joy_advaxisy = cvar->FindVar("joy_advaxisy");
   ConVar* joy_advaxisz = cvar->FindVar("joy_advaxisz");
   ConVar* joy_advaxisr = cvar->FindVar("joy_advaxisr");
   ConVar* joy_advaxisu = cvar->FindVar("joy_advaxisu");
   ConVar* joy_advaxisv = cvar->FindVar("joy_advaxisv");
   ConVar* joy_wwhack2 = cvar->FindVar("joy_wingmanwarrior_turnhack");

   // Initialize all the joystick maps
   for ( int i = 0; i < MAX_JOYSTICK_AXES; i++ )
   {
      m_axesValues[i] = GAME_AXIS_NONE;
   }

   if ( !joy_advanced->GetBool() )
   {
      // X and Y axes only
      m_axesValues[JOY_AXIS_X] = GAME_AXIS_YAW;
      m_axesValues[JOY_AXIS_Y] = GAME_AXIS_FORWARD;
   }
   else
   {
      // X, Y, Z, R, U, V axes
      m_axesValues[JOY_AXIS_X] = (DWORD)joy_advaxisx->GetInt() & 0x0000000f;
      m_axesValues[JOY_AXIS_Y] = (DWORD)joy_advaxisy->GetInt() & 0x0000000f;
      m_axesValues[JOY_AXIS_Z] = (DWORD)joy_advaxisz->GetInt() & 0x0000000f;
      m_axesValues[JOY_AXIS_R] = (DWORD)joy_advaxisr->GetInt() & 0x0000000f;
      m_axesValues[JOY_AXIS_U] = (DWORD)joy_advaxisu->GetInt() & 0x0000000f;
      m_axesValues[JOY_AXIS_V] = (DWORD)joy_advaxisv->GetInt() & 0x0000000f;
   }


Then you have to poll the joystick like this:
Code: Select all
   if( !inputsystem->GetJoystickCount() )
      return;
   
   float gameAxes[ MAX_GAME_AXES ];
   memset( &gameAxes, 0, sizeof(gameAxes) );

   for ( int i = 0; i < MAX_JOYSTICK_AXES; ++i )
   {
      if ( GAME_AXIS_NONE == m_axesValues[i] )
         continue;

      float fAxisValue = inputsystem->GetAnalogValue( (AnalogCode_t)JOYSTICK_AXIS( 0, i ) );

      if( joy_wwhack2->GetInt() )
      {
         float fTemp = 300.0 * pow(abs(fAxisValue) / 800.0, 1.3);
         if (fTemp > 14000.0)
            fTemp = 14000.0;

         fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp;
      }

      unsigned int idx = m_axesValues[i];
      gameAxes[idx] = fAxisValue;
   }

   float m_flPreviousJoystickForward = ScaleAxisValue( gameAxes[GAME_AXIS_FORWARD], MAX_BUTTONSAMPLE * joy_forwardthreshold->GetFloat() );
   float m_flPreviousJoystickSide = ScaleAxisValue( gameAxes[GAME_AXIS_SIDE], MAX_BUTTONSAMPLE * joy_sidethreshold->GetFloat()  );
   float m_flPreviousJoystickPitch   = ScaleAxisValue( gameAxes[GAME_AXIS_PITCH], MAX_BUTTONSAMPLE * joy_pitchthreshold->GetFloat()  );
   float m_flPreviousJoystickYaw = ScaleAxisValue( gameAxes[GAME_AXIS_YAW], MAX_BUTTONSAMPLE * joy_yawthreshold->GetFloat()  );

   DevMsg("Joysticks: %f / %f / %f / %f\n", m_flPreviousJoystickForward, m_flPreviousJoystickSide, m_flPreviousJoystickPitch, m_flPreviousJoystickYaw);