Thursday, March 31, 2011

Rolling Average Smoothing

At the IGDA Twin Cities meeting in March, the lead developer for Just Jam, Matt Heinzen, mentioned using rolling averages to do animation. This caught my attention, since it was a perfect phrase for a trick I have used a number of times in my programming. I thought I'd take a shot at explaining it here since I did a horrible job on the IGDATC Podcast.

A rolling average is also known as a moving average along with a few other cute names. Wikipedia does a good job explaining it in that Wikipedia way. You know, the equation way with complicated, but accurate, descriptions. I say a rolling average is just a way to take the average of a select part of data set. For example, in the last 7 days, I've averaged 0.6 cans of Mt. Dew a day. If, tomorrow, I don't consume any Mt. Dew, my 7 day average will go down.

So how does this help in programming? For me, its usually a matter of convenience. You may have binary data and you want to smooth it out some for instance. To really smooth it out you may want to be aware of your time step, acceleration/deceleration, velocity, motion time, time into motion. Maybe even more. With a rolling average all you need is a target value.

Lets say you've got an arrow and you want to have it point somewhere. You don't want to have super control over the animation of the arrow like I mention above, you simply want to tell it to point up or point down. However, it would be nice if the arrow rotation had some motion.

Let's set up this example. There is an arrow pointing up, lets call that 0 degrees. Now at some moment you want to say point down, rotating 180 degrees. If all you do is say at one moment your 0 degrees, and the next moment your 180, there is no animation. Let's use rolling average to smooth it out. All we need to know is the target rotation angle, which in this example is either 0 or 180, and the current angle. We need to pick a period, which in the Mt. Dew example was 7 days. In a typical game, and in most cases I've designed, they end up depending on the frame rate, lets not concern ourselves with that at the moment and pick a period of 10.

rolling average rotation = ((current angle) * 9 + (target angle) * 1) / 10

With that example, the first frame after the target angle is changed from 0 to 180, the rolling average will calculate it to be as follows.

rolling average rotation = ((0 degrees) * 9 + (180 degrees) * 1) / 10

rolling average rotation = 18 degrees

That is to say instead of it immediately snapping to 180 degrees, the first frame after its told to go to 180 degrees it rotates to 18 degrees. The next frame would look like this:

rolling average rotation = ((18 degrees) * 9 + (180 degrees) * 1) / 10

rolling average rotation = 34.2

And continuing on.

To help illustrate this I've made a little XNA program.  You can either download it and give it a try (no guarantees), or watch this video.



No comments: