]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/numeric.h
Smoothed yaw rotation for objects (#6825)
[minetest.git] / src / util / numeric.h
index f7df19ca94f75a05ba7d2460d4e0fa5e56837390..61370a3f4ba2f2d6ae2d288d50ff29a920420916 100644 (file)
@@ -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<typename T>
+inline void wrappedApproachShortest(T &current, 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;
+       }
+}