From 979aea52126c04b3ed45f0e6b5ba7be59b46ccea Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Fri, 8 Feb 2013 12:12:15 +0100 Subject: [PATCH] Add SGMisc::seek helper --- simgear/math/SGMisc.hxx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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()) -- 2.39.5