Search code examples
swiftsprite-kitapple-tvgamecontroller

Get angle of direction from GCExtendedGamepad.leftThumbstick


How do I get the angle of direction from the left/right thumb-stick of a game controller in swift? Any help would be appreciated.


Solution

  • leftThumbstick is a GCControllerDirectionPad, so it has an xAxis and a yAxis. Each of those is a GCControllerAxisInput, which has a value property of type float. The value property ranges from -1 to 1, where 0 means the stick is at the center along that axis (or in the deadzone around the center).

    So you can compute the angle of the stick in radians like this:

    let controller = GCController.controllers()[0]
    let gamepad = controller.extendedGamepad!
    let stick = gamepad.leftThumbstick
    let radians = atan2(stick.yAxis.value, stick.xAxis.value)