Domanda

I am working on moving a small rectangle around a screen using the thumbstick on the xbox controller. I have it perfect for the mouse Input but I seem to have run into a problem with the controller. The square appears on the screen at (0,0) the deadzone for the controller. When I move the thumbstick the square move a specific amount and no further, when I lift my finger off the stick it goes back to (0,0). The code is below, its pretty simple code but cant get it to work. Thanks for any help.

Here is the first part.

void Graphics::rectangle(int x, int y)
{

    {
    r1.x1 = x;
    r1.y1 = y;
    r1.x2 = r1.x1 + 50;
    r1.y2 = r1.y1 + 50;
    }
}

Here is the second part.

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);
È stato utile?

Soluzione

I finally got a solution to this problem. I added the variables xNew and yNew that take the values of x and y and add them on to the existing values. This allowed me to move the square around the screen. xNew and yNew are initialize at the top of the class below is the code for the input from controller and the result in rectangle.

`

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);
}`

Altri suggerimenti

The problem is the mouse is a "Relative Movement" Input and the thumb-stick is a "Absolute Movement" input. What you need to do is when you are working with the thumb-sticks your final line needs to be something like

graphics->rectangle(xOld + dx, yOld + dy);

where xOld and yOld is the location of the square before you applied the function.

Might be a little late, but I do have a very easy-to-use C++ XInput wrapper. Have a look here

It literally takes a few lines of code and you have full support for XInput controllers in your app :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top