How to stabilize variable?

Hi,
I want to stabilize my variable which is heading of compass. I want something like avarange of last couple of frames. The reason of this is that heading variable is so unstable.

Well you can use this class i’ve just written. I’ve created two implementations of the RunningAverage, once with double values and one with Vector4 values. That way it can handle most cases.

Just create an instance of the RunningDoubleAverage or RunningVector4Average class and pass the desired number of samples you want to calculate the average over. Now just use the Add method to add a new sample each frame. Instead of using the actual input value you would use the Current property which is the average over the last X samples.

Note that the vector4 variant is not really suited for direction vectors as if calculates just the average vector which could change the length. You could renormalize the result to get back a unit vector.

So basically instead of

target = yourInputValue;

You would do

// class variable
RunningDoubleAverage average = new RunningDoubleAverage(20);

// ...
average.Add(yourInputValue);
target = average.Current;