使用Xinput,Xbox控制器两个拇指都不会改变值

时间:2016-07-19 15:26:54

标签: c++ xinput

我正在使用Xinput API,但我遇到以下代码问题。我的假设是R / LX和R / LY的定义应该一次又一次地动态改变,但拇指杆位置的值任意设置为-13108,因此X和Y的归一化幅度是-.707,归一化幅度是〜.428。我一直试图移动控制棒,但价值不会改变。有任何想法吗?我误解了Xinput API吗?结构控制器状态是否有意义?下面的代码仅适用于左手棒,但右手棒非常相似。

#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;
            }
    }

1 个答案:

答案 0 :(得分:0)

你已经规范了一个州,而且它是空的。我假设您至少在您的bool函数bConnected中调用XInputGetState(),但是这可能会被调用一次,因此值将保持不变。因此,无论是在main中,还是在上面显示的相关函数中,都应该调用getstate函数一次,while循环中的第一行,因此在运行时,状态会不断更新。