Problems with Weighted Moving Average Rotator

I’m trying to make a motion stabilization blueprint and I’m following the blueprint in the attached screenshot. But the problem seems to be, as the cross over between 179 and -179 comes, the weighted moving average rotator node just combines them and get’s a crazy value and starts spinning or something. What am I doing wrong?

Reviving this old thread because I ran into the same issue on UE 4.27. AFAICT it’s because the function is trying to linearly smooth between two values, and doesn’t take into account the fact that rotator axes loop between 179 and -179. My solution was to convert the rotator into vectors, smooth between them, and then convert them back into a rotator.

1 Like

I had this problem with a project that needed to stabilize movement from an external controller, and fixed it by writing new versions of the weighted average functions that properly understand the circular nature of angles. The source is available here. I highly recommend them for anyone trying to smooth rotators.

// The shortest distance around the circle from one angle to another. The sign of the result
// indicates the direction, and the result can be added to FromAngle to get to ToAngle,
// be sure to normalize after.
float UWorkbenchMath::AngleDistance(float FromAngle, float ToAngle)
{
    FromAngle = NormalizeAngle(FromAngle);
    ToAngle = NormalizeAngle(ToAngle);

    float Dist = ToAngle - FromAngle;
    if (Dist >= 180.f)
    {
        return Dist - 360.f;
    }
    else if (Dist < -180.f)
    {
        return Dist + 360.f;
    }
    else
    {
        return Dist;
    }
}