/ C++

UE4 Quickie: Input Axes to Angle

As you develop games, there will likely be times when you want to translate the direction from an analog stick input into an angle for use in displaying an onscreen control or widget that point in the same relative direction as the analog stick. An example of a common control where you would need to do this is the pie menu, such as the dialogue wheel that BioWare uses for both the Mass Effect and Dragon Age series, or the weapon wheel used in the Far Cry and Assassin's Creed games (not to mention a bazillion others).

In a pie menu, options are presented in a circle on screen. If you want to select the item that's directly to the left, you push the analog stick to the left. If you want the item at the top of the circle, you push the stuck straight up.


This type of control is really intuitive. You just rotate the analog stick to "point" at the option you want. Fortunately, it's also a relatively easy effect to implement in UE4. Of course, if you want to implement a full dynamic wheel system with a variable number of items, it'll get a little involved and require some math, but the general control mechanism is super simple. All you have to do is create a widget and then set the widget's render angle to match the input from the desired analog stick.

But, how do you translate the two axis values that make up the analog stick's input into a single rotation angle value?

Fortunately, there's a standard math library function called atan2 (FMath::Atan2() in UE4) that does exactly that. It calculates the arc tangent (or the inverse of the tangent) of two numbers. Feed it the Y and X axis values from your analog stick input and it will return the angle that the stick is pointing. The only adjustment you need to make to the value returned by atan2 is to add 90° to it to orient it so that 0° becomes the top of the screen. There is a minor gotcha when doing this from C++, which is that we don't want to call atan2 with both X and Y values of 0.0, since the result of atan2 with an input of 0.0 is undefined.

You can use this technique from either C++ or Blueprint. In C++, it would look like this:

    if (XAxisValue == 0.f && YAxisValue == 0.f) {
        return 0.f;
    float angle = FMath::Atan2(YAxisValue, XAxisValue);
    return FMath::RadiansToDegrees(angle) + 90.f;

In Blueprint, it's similar, but even easier because we don't even have to handle the conversion from radians to degrees. UE4 comes with built-in node that returns the result of atan2 in degrees.

Note: In the following Blueprint, the Get YInput Axis Value and Get XInput Axis Value nodes need to be replaced with the correct input axes inputs for your game. If you're using the standard third person template and wanted to use the right analog stick for input, for example, you would use use Get LookUpRate for the Y Input, and Get TurnRate for the X Axis.

If you want to see it in action, we've put together two super simple implementations. Right here you can find a version implemented completely in Blueprint. Here you can find one that uses a Blueprint Library written in C++ to accomplish the same thing.

UE4 Quickie: Input Axes to Angle
Share this

Subscribe to Recursive Blueprints