1

I am using the Xinput API, but I am having trouble with the following bit of code. My assumption is that the definition of R/LX and R/LY, should dynamically change as its called again and again, but the value for the position of the thumb stick is arbitrarily set to -13108, so the normalized magnitude of X and Y is -.707, and the normalized magnitude is ~.428. I keep trying to move the control stick but the values won't change. Any ideas? Am I misunderstanding the Xinput API? Does the struct controller state make sense? The code is below is just for the left stick, but the right stick is very similar.

#define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE  7849
#define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
#define XINPUT_GAMEPAD_TRIGGER_THRESHOLD    30
struct CONTROLLER_STATE
    {
        XINPUT_STATE state;
        bool bConnected;
    };
    CONTROLLER_STATE g_Controllers[4];

    while(1)
  {
            //...
            XINPUT_STATE state = g_Controllers[1].state;


            float LX = state.Gamepad.sThumbLX;
            float LY = state.Gamepad.sThumbLY;

            //determine how far the controller is pushed
            float magnitude = sqrt(LX*LX + LY*LY);

            //determine the direction the controller is pushed
            float normalizedLX = LX / magnitude;
            float normalizedLY = LY / magnitude;
            cout << " Y " << LY << endl;
            float normalizedMagnitude = 0;

            //check if the controller is outside a circular dead zone
            if (magnitude >  XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
            {
                //clip the magnitude at its expected maximum value
                if (magnitude > 32767) magnitude = 32767;

                //adjust magnitude relative to the end of the dead zone
                magnitude -= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;

                //optionally normalize the magnitude with respect to its expected range
                //giving a magnitude value of 0.0 to 1.0
                normalizedMagnitude = magnitude / (32767 - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
                cout << "normalizedMagnitude " << normalizedMagnitude;

            }
            else //if the controller is in the deadzone zero out the magnitude
            {
                magnitude = 0.0;
                normalizedMagnitude = 0.0;
            }
    }
Zotto
  • 73
  • 1
  • 2
  • 9
  • Could the controller be the problem? – Zotto Jul 19 '16 at 15:55
  • The code you posted doesn't show reading the controllers. But the most likely place for your error is in the code near XInputGetState(). (You are calling XInputGetState aren't you?) – Chip Burwell Jul 19 '16 at 21:47

1 Answers1

0

You have normalised a state, and it is rather empty. I would assume that you are atleast calling XInputGetState() in your bool function bConnected, however this would probably be called once and hence values would remain the same. Therefore, either in your main, or in your associated function displayed above, you should call the getstate function once, first line in the while loop, so as it runs, the state is updated continously.