From: Thomas Geymayer Date: Fri, 8 Feb 2013 11:12:15 +0000 (+0100) Subject: Add SGMisc::seek helper X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=979aea52126c04b3ed45f0e6b5ba7be59b46ccea;p=simgear.git Add SGMisc::seek helper --- diff --git a/simgear/math/SGMisc.hxx b/simgear/math/SGMisc.hxx index 8eaa124e..90f9c663 100644 --- a/simgear/math/SGMisc.hxx +++ b/simgear/math/SGMisc.hxx @@ -41,6 +41,23 @@ public: static T clip(const T& a, const T& _min, const T& _max) { return max(_min, min(_max, a)); } + /** + * Seek a variable towards a target value with given rate and timestep + * + * @param var Variable or eg. SGPropObj + * @param target Target value + * @param rate Max. change rate/sec + * @param dt Time step (sec) + */ + template + static T seek(Var& var, T target, T rate, T dt) + { + if( var < target ) + return var = min(var + rate * dt, target); + else + return var = max(var - rate * dt, target); + } + static int sign(const T& a) { if (a < -SGLimits::min())