Search code examples
javaxbox360jinput

How to find when stick is released


I have problem to check when stick in my gamepad (XBox) is released. With buttons when I press the first I get pollData = 1.0 and after releasing it I get pollData = 0.0.

With analog sticks I don't have such event like pollData = 0.0

Thanks in advance for help!


Solution

  • Given you already have the axis as a

    Component component;
    

    (to be sure that it is an axis by testing

    if(component.isAnalog())
    

    then you can get the position by calling

    component.getPollData()
    

    The returned value will be between -1 and 1. -1 being left/bottom, +1 being right/top, depending on whether component.getIdentifier() equals Component.Identifier.Axis.X or Component.Identifier.Axis.Y.

    So you could do something like

    bool xReleased = false, yReleased = false;
    Component[] components = controller.getComponents();
    for(Component component : components) {
        if(component.isAnalog()) { //test that controller is analog
            Identifier id = component.getIdentifier();
            float axisPosition = component.getPollData(); //range: -1 to 1
            if(id == Component.Identifier.Axis.X && axisPosition == 0)
                xReleased = true;
            if(id == Component.Identifier.Axis.Y && axisPosition == 0)
                yReleased = true;
        }
    }
    
    if(xReleased && yReleased) {
        //do something...
    }