c++ - Xbox Controller 两个拇指棒都没有改变值,使用 Xinput

标签 c++ xinput

我正在使用 Xinput API,但我在处理以下代码时遇到了问题。我的假设是 R/LX 和 R/LY 的定义应该随着一次又一次的调用而动态变化,但是拇指杆位置的值被任意设置为 -13108,所以 X 和 Y 的标准化幅度为 -.707,归一化幅度为 ~.428。我一直试图移动控制杆,但值不会改变。有任何想法吗?我误解了 Xinput API 吗?结构 Controller 状态有意义吗?下面的代码仅适用于左摇杆,但右摇杆非常相似。

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

最佳答案

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

关于c++ - Xbox Controller 两个拇指棒都没有改变值,使用 Xinput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462724/

相关文章:

linux - linux/X11中的"Seamless"多用户 session

c++ - C++ 头文件中的大小写敏感性

c++ - 如何截断 HWND 标题

c++ - 断言释放 C++ 中的内存

c++ - 自修改代码 [C++]

Java:如何模拟 XInput 游戏 handle / Controller ?

c++ - 不考虑参数依赖查找

c++ - 是否可以在基于 Linux 的系统上使用 gcc 或 g++ 进行静态编译?

c++ - 在 XInput 事件期间监听哪个事件