How can I make orientation get the shortest possible path to curve into?

So I have a thing that I want to bend smoothly towards another point via clamping them inbetween max points like this:

for i = 1, 10, 1 do
    local maxBend = 20
    local a = 10
    local b = 170
    a = math.clamp(b, a - maxBend, a + maxBend)
end

But whenever it goes through the 360° point, it curves in a whole circle, which makes it look awkward and buggy. :sob: :sob:
How can I solve this? It’s probably really easy though it’s just that my math ain’t mathing today. :smiling_face_with_tear:

I think this is how it’s done. I’m not touching your code because I’m not fully certain what you’re doing, but hopefully you can work this into your script.

local function closest_angle(current, target)
    local diff = target-current
    return diff - 360*math.round(diff/360)
end

print("Closest angle from 300 degrees to 15 degrees is ", closest_angle(300, 15))
--> Closest angle ... is 75
print("Closest angle from 15 degrees to 350 degrees is ", closest_angle(15, 350))
--> Closest angle ... is -25

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.