【问题标题】:Xbox Controller both thumb sticks not changing value, using Xinput使用 Xinput,Xbox 控制器两个拇指杆都没有改变值
【发布时间】:2016-08-17 11:46:54
【问题描述】:

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

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

【问题讨论】:

  • 控制器可能是问题吗?
  • 您发布的代码未显示读取控制器。但是最有可能出现错误的地方是 XInputGetState() 附近的代码。 (你不是在调用 XInputGetState 吗?)

标签: c++ xinput


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    相关资源
    最近更新 更多