From f3997025fd4785606fe4f8b05037e9463c5be7da Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Fri, 3 Aug 2018 00:25:37 +0200 Subject: Smoothed yaw rotation for objects (#6825) --- src/util/numeric.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/util/numeric.h') diff --git a/src/util/numeric.h b/src/util/numeric.h index f7df19ca9..61370a3f4 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -376,3 +376,22 @@ inline u32 npot2(u32 orig) { orig |= orig >> 16; return orig + 1; } + +// Gradual steps towards the target value in a wrapped (circular) system +// using the shorter of both ways +template +inline void wrappedApproachShortest(T ¤t, const T target, const T stepsize, + const T maximum) +{ + T delta = target - current; + if (delta < 0) + delta += maximum; + + if (delta > stepsize && maximum - delta > stepsize) { + current += (delta < maximum / 2) ? stepsize : -stepsize; + if (current >= maximum) + current -= maximum; + } else { + current = target; + } +} -- cgit v1.2.3