我正在使用 xbox Controller 上的摇杆在屏幕周围移动一个小矩形。我非常适合鼠标输入,但我似乎遇到了 Controller 问题。该正方形出现在屏幕上的 (0,0) Controller 死区。当我移动拇指杆时,方 block 会移动一个特定的量,而不会再移动,当我将手指从操纵杆上移开时,它会回到 (0,0)。代码在下面,它非常简单的代码但无法让它工作。感谢您的帮助。
这是第一部分。
void Graphics::rectangle(int x, int y)
{
{
r1.x1 = x;
r1.y1 = y;
r1.x2 = r1.x1 + 50;
r1.y2 = r1.y1 + 50;
}
}
这是第二部分。
LONG x = input->getGamepadThumbRX(0);
LONG y = input->getGamepadThumbRY(0);
float x1 = x/32767;
float y1 = y/32767;
if(x1 > 0 && x1 < 0.2f || x1 < 0 && x1 > -0.2f){
x1 = 0;
}
if(y1 > 0 && y1 < 0.2f || y1 < 0 && y1 > -0.2f){
y1 = 0;
}
float factor = 10;
int dx = int(factor * x1);
int dy = int(factor * y1);
graphics->rectangle(dx,dy);
最佳答案
我终于找到了解决这个问题的方法。我添加了变量 xNew 和 yNew,它们采用 x 和 y 的值并将它们添加到现有值上。这让我可以在屏幕上移动方 block 。 xNew 和 yNew 在类的顶部初始化,下面是 Controller 输入的代码和矩形的结果。
`
void Graphics::rectangle(int x, int y)
{
xNew += x;
yNew += -y;
{
r1.x1 = xNew;
r1.y1 = yNew;
r1.x2 = r1.x1 + 50;
r1.y2 = r1.y1 + 50;
}
}
void Game::update()
{
LONG x = input->getGamepadThumbRX(0);
LONG y = input->getGamepadThumbRY(0);
float x1 = x/32767;
float y1 = y/32767;
float factor = 10;
int dx = int(factor * x1);
int dy = int(factor * y1);
graphics->rectangle(dx,dy);
}`
关于c++ - Xbox Controller 拇指输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288610/