Page 1 of 1

Catmull-Rom character animation

Posted: Thu Aug 11, 2011 12:50 pm
by Taehl
I've been thinking about how I'm going to animate my character in Underlife. So far, I think I'm going to be making a keyframe-based animation system so that it can be smooth as possible and have good transitions between animations (like in 3D animation, or Flash (I think)). Researching it a bit, it seems like using a Catmull-Rom spline to interpolate between keyframes would be ideal (it's relatively fast and will always travel exactly through each point).

The only problem is, a CR spline requires at least four points - the two points you're interpolating between, and one before and after to control the curve. This poses the problem: How do you know what animation points are in the future? It's easy enough if you're playing a loop or something, but what if your character is running, then jumps? The two future-ward points would change, and wouldn't that cause the curve to suddenly snap? How could you smooth that out?

Also, as I'm not extremely good with mathematics, I'm unsure if this would work for one-dimensional lines (I want it to control joint angles) as opposed to the two-dimensional (x, y) demonstrations I'm seeing everywhere. Is it possible?

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 12:53 pm
by TechnoCat
Taehl wrote:The only problem is, a CR spline requires at least four points - the two points you're interpolating between, and one before and after to control the curve. This poses the problem: How do you know what animation points are in the future? It's easy enough if you're playing a loop or something, but what if your character is running, then jumps? The two future-ward points would change, and wouldn't that cause the curve to suddenly snap? How could you smooth that out?
Why not just a natural spline? Derivative on the ends are always 0, so it will always be smooth on the ends.

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 1:44 pm
by Taehl
Just on the ends wouldn't be good enough, I would think. For instance, in a running animation, you don't want your character's feet to go too far down, or they'd go through the ground. Hence, it's desirable to hit each keyframe exactly.

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 2:08 pm
by TechnoCat
Taehl wrote:Just on the ends wouldn't be good enough, I would think. For instance, in a running animation, you don't want your character's feet to go too far down, or they'd go through the ground. Hence, it's desirable to hit each keyframe exactly.
Then how about clamped spline, you specify the end-point derivatives. I have a book with MATLAB code for cubic clamped splines.

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 2:25 pm
by kikito
Taehl wrote: The only problem is, a CR spline requires at least four points - the two points you're interpolating between, and one before and after to control the curve. This poses the problem: How do you know what animation points are in the future? It's easy enough if you're playing a loop or something, but what if your character is running, then jumps? The two future-ward points would change, and wouldn't that cause the curve to suddenly snap? How could you smooth that out?
If I've understood correctly, you are really asking "what to do while transitioning between animations". If you are inside the "run" cycle, you are fine. If you are jumping, you are fine. The problem happens when you are "transitioning", right?

That's though. I see some options, but none seems to be drawback-free.
  • Ignoring transitions. This is actually a good solution in lots of situations. If the character jumps, he gets "magically" to the "jumping start" frame, independently of where he was previously. It will not look very realistic, but the controls will feel very responsive.
  • Delaying the action. Create "transitioning" animations: a "jumping from running" one, for example. If the player presses "jump" but he's not in the frame in which "jumping while running" starts, you just note the desire to jump and continue playing the run animation until he reaches the right keyframe. This is what Prince of Persia did, for example.
  • Creating intermediate animations on the fly. You interpolate each point (probably linearly) from its current position while running to the "start jumping" frame. The time it takes to get that frame will depend on how different the current position is from the "start jumping" one. But it can't take too long, or it will look awkward. In the particular case of running, you will probably want to still move the character forward while playing the "interpolated animation"

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 3:20 pm
by Taehl
My fallback plan was to use linear interpolation between each keyframe. That'd be very straightforward to transition from one animation to another, but its main drawback is that without a lot of keyframes in each animation, it'd look jerky and awful.

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 5:26 pm
by tentus
You may want to look at kikito's tween.lua, and also Emmanuel Oga's easing library. Interesting stuff.

Re: Catmull-Rom character animation

Posted: Thu Aug 11, 2011 7:15 pm
by Taehl
The only problem with those is that they only tween between two points. If you did it with more points, you end up with this kind of "start, stop, start, stop" thing. Not so great for character animation, in my opinion.