Interpolating through a circle

I’m trying to interpolate from one value to another, through a circle. I have two values, 0 to 180 (for the right side) and 0 to -180 (for the left side). This works pretty good, I’m using this as my code which smoothly interpolates it all.

MovementDelta = FMath::FInterpTo(MovementDelta, moveYaw, DeltaTime, 10.0f);

However there’s one massive problem.

If my current yaw is -165 and I need to get to 150 it takes the long way around (going clockwise) since it doesn’t know it needs to flip at -180. Does anyone have a solution for this? Thanks.

There’s perhaps a better way than mine. if you can’t figure out. here’s one way.
In my example I’m trying to rotate A to B

  1. I wrote a function to convert the -180 to 180 to 0-360 first. Then apply it on A and B

    If (x <0)
    return x + 360
    else
    return x

This would result in converting it to 0-360 where 90 is right, 270 is left.

  1. So now we have A and B value as a 0-360. Problem is not solve yet. because now A-B will always interpolate anti-clockwise. So I did another if statement

    If (A-B > 180)
    A interpolate to (B+360 )
    else
    A interpolate to B

this will guarantee you it will always interpolate to the shortest way.
example interpolating 350 to 10 would be 350 to 370

In your example…A = -165 , B = 150
would make it interpolate from 195 to 150

As again, there would probably be an easier way.

Since you are trying to interpolate angle, why dont you try RInterTo instead of FinterTo. You can build Rotattor with only the Yaw and the other two at 0.

So you would want to do:

FMath::RInterTo(FRotator(0.f, MovementDelata, 0.f), FRotator(0.f, moveYaw,0.f),....,....)

Then get the Yaw out of the returned Rotator.

1 Like