Advancing Timing and Interpolation
In a couple of previous posts I showed you how to create a nice little Timer class and Interpolator class that can really help speed up game development by making things like timing actions, performing animations, and adding delays much simpler and more declarative. However, both of those classes suffered from a few flaws. The Timer was a bit over designed to support too many Action delegate types which made it more heavy weight than it needed to be. The Interpolator was pretty decent, but was limited to linear interpolation. So now let’s take another stab and make both of these classes a nicer.
Before we get going, you’ll need this new Pool<T> class I’ve whipped up. It’s more or less the same as my older Pool<T> class except the new one supports dynamically resizing itself so you don’t have to worry about guessing how many instances you’ll need. Just tell it how many you want off the bat (or don’t) and it’ll take care of the rest. Definitely a nice improvement there.
Now, the Timer. I won’t put much about this since it’s basically the same as last time. The only difference is that I’ve reduced the number of Create methods to just one. This simplification I find isn’t really a hindrance and makes the Timer easier to maintain as well as likely having a little perf gain from not having to do the conditionals and managing the different action types.
Next up is the Interpolator. Some big changes here. First let’s note the delegate we have:
public delegate float InterpolatorScaleDelegate(float progress);
An InterpolatorScaleDelegate is how we are able to change the behavior of the interpolator without subclasses. Basically our new interpolator has two values that represent its interpolation progress: we have a “progress” variable which always goes from 0 to 1 and then we have a “value” variable which is the actual value we’ve computed that lies between the specified start and end values.
Our delegate is how we compute that “value” variable. We pass in the progress and the delegate will return a value. That value indicates where we are between the start and finish. If the delegate returns 0, we’re at the start. If it returns 1, we’re at the end. If it returns .5 we’re half way between. If it returns 10, we’re a long ways past the end value.
Of course, having this delegate would be useless without some defaults you can use. I’ve included a static InterpolatorScales class that includes a few basic interpolation delegates that you can easily use for your games if you don’t want to create your own. The default is the Linear delegate, but you can easily use one of the overloads of Interpolator.Create to pass in one of the other delegates if you so choose.
Hopefully these new versions of the Timer and Interpolator (or Pool if you’re using that in your game) will be as helpful to you as they have been me. The Interpolator changes, especially, have really enabled me to create some nice animations and transitions with ease now that I can use non-linear interpolation. So take this code, and go add some polish to your game.
Possibly Related Posts
(Automatically Generated)Interpolation: Polish's Best Friend
Using interpolators and timers
More Timer/Interpolator Tweaks
Borrowing From The iPhone SDK
Learning lambda expressions for XNA GS developers
