gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:47 28 4
gpt4 key购买 nike

我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com