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.
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.
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:
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.
My computer at work went bust (thanks to an IT push) and I needed to have an admin reinstall the Logitech Quickcam Pro drivers. Unfortunately, the little driver for the camera comes with a 85 Mb download including some intrusive software that is always running. Why do these little peripherals have this crap?
Anyway, I was determined not to install this crap again so I scoured for other drivers without much luck. So I tried opening the 85 Mb EXE (downloaded from Logitech) with WinZip and what do you know, its an archive. So I extracted it, plugged in my camera and navigated the hardware wizard to the extracted drivers directory and now my Quickcam Pro 4000 works without their “software”.