//-----------------------------------------------------------------------------
// Purpose: Apply TrackIR to CUserCmd creation
// Input  : frametime - 
//			*cmd - 
//-----------------------------------------------------------------------------
void CInput::TrackIRMove( float frametime, CUserCmd *cmd )
{
#if !defined( _X360 )
	if ( !IsHeadTrackingEnabled() )
		return;

	// complete initialization if first time in ( needed as cvars are not available at initialization time )
	// verify TrackIR is available and that the user wants to use it
	if (!m_fTrackIRAvailable )
	{
		return; 
	}

	if (tir_start.GetFloat() == 1.0)
	{
		Init_TrackIR();
		tir_start.SetValue(0);
	}

	if (tir_stop.GetFloat() == 1.0)
	{
		Shutdown_TrackIR();
		tir_stop.SetValue(0);
	}

	// grab the data from the TrackIR
	TRACKIRDATA tid;
	NPRESULT    result;

	// Go get the latest data
	result = NPS_GetData(&tid);
	if( NP_OK == result )
	{
		QAngle viewangles;
		QAngle engineview;

		// get the current player
		C_BasePlayer * pPlayer = C_BasePlayer::GetLocalPlayer();

		// calculate the amount of rotation from TrackIR
		viewangles[YAW] = g_angleCenter[YAW] + (tid.fNPYaw / (float) TIR_MAX_VALUE) * tir_maxyaw.GetFloat();
		viewangles[PITCH] = g_angleCenter[PITCH] + (tid.fNPPitch / (float) TIR_MAX_VALUE) * tir_maxpitch.GetFloat();
		viewangles[ROLL] = g_angleCenter[ROLL] + (tid.fNPRoll / (float) TIR_MAX_VALUE) * tir_maxroll.GetFloat() * -1.0;

		// get the direction the player is facing
		QAngle eyeAngle;
		eyeAngle = pPlayer->EyeAngles();

		// add in the head rotation
		eyeAngle += viewangles;

		// get the rotation matrix for the head
		matrix3x4_t mat;
		AngleMatrix( pPlayer->EyeAngles(), mat );

		// create a normalized vector based on the TIR input
		Vector tirForward, tirEye;

		tirForward.x = (tid.fNPZ / (float) TIR_MAX_VALUE) * -1;
		tirForward.y = (tid.fNPX / (float) TIR_MAX_VALUE);
		tirForward.z = (tid.fNPY / (float) TIR_MAX_VALUE);

		// now rotate the vector based on the eye angle
		VectorRotate(tirForward, mat, tirEye);

		// scale the translation vector
		tirEye.x *= tir_maxz.GetFloat();
		tirEye.y *= tir_maxx.GetFloat();
		tirEye.z *= tir_maxy.GetFloat();

		// save the values for later
		pPlayer->SetEyeOffset(tirEye);
		pPlayer->SetEyeAngleOffset(viewangles);

		cmd->headangles = viewangles;
		cmd->headoffset = tirEye;
	}
#endif
}