## Requesting Help: Smooth keyframe interpolation algorithm

Nibbie
Posts: 2
Joined: 2009.01
Post: #1
Animations in my game are stored as a series of keyframes and I use linear interpolation to tween between them when running them on a character - but linear interpolation either makes the animator use extra of keyframes (harder for them (me in this case)) or looks jerky.

I'd like to smoothly tween between the values, but I can't figure out an algorithm for this.

My terms:
time[] is the array of keyframe times
value[] is the array of keyframe values
t# is the time I want a value for
lowIdx is the index of the closest keyframe lower than t
highIdx is the index of the closest keyframe higher than t
rLowHigh# is the ratio of the distance of t from time[lowIdx] relative to the distance between time[highIdx] - time[lowIdx]
vLowHigh# is the value from linear interpolation between value[lowIdx] and value[highIdx]

My linear interpolation algorithm:
Code:
```rLowHigh# = (t - time[lowIdx]) / (time[highIdx] - time[lowIdx]) vLowHigh# = value[lowIdx] + (value[highIdx] - value[lowIdx]) * rLowHigh```

I've been trying to think of ways to smooth this, taking into account the keyframes before and after low and high, time[lowIdx - 1] and time[highIdx + 1] but I haven't come up with anything that works.

What algorithms do you use that are smoother looking than linear interpolation? I'd really love to fix this
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
whogben Wrote:What algorithms do you use that are smoother looking than linear interpolation? I'd really love to fix this

Bones.ï»¿ï»¿ï»¿ï»¿
Sage
Posts: 1,232
Joined: 2002.10
Post: #3
Catmull-rom splines.
Member
Posts: 446
Joined: 2002.09
Post: #4
Here's a good interpolation overview: http://local.wasp.uwa.edu.au/~pbourke/mi...rpolation/ (Catmull-Rom isn't on that page but it looks the same as Hermite with 0 tension. Google will turn up some more efficient code if you just want Catmull-Rom).

With that in mind, I've never had any problems using linear interpolation for keyframes - if it looks bad, your keyframes are most likely the problem. Bones won't necessarily fix this either - at some point you have to interpolate bones too...
Nibbie
Posts: 2
Joined: 2009.01
Post: #5
ThemsAllTook Wrote:Bones.

I'm using bones for my animation, these keyframe values are bone movements, not vertex movements. I'm looking for a smoother way to interpolate the values that factors in an extra keyframe forwards and backwards.

arekkusu Wrote:Catmull-rom splines.

Perfect, exactly what I was looking for. I'm adding cardinal splines with a tension of .5 for the interpolation. Thanks Arekkusu!