Catmull-Rom character animation

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Catmull-Rom character animation

Post 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?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Catmull-Rom character animation

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Catmull-Rom character animation

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Catmull-Rom character animation

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Catmull-Rom character animation

Post 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"
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Catmull-Rom character animation

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Catmull-Rom character animation

Post by tentus »

You may want to look at kikito's tween.lua, and also Emmanuel Oga's easing library. Interesting stuff.
Kurosuke needs beta testers
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Catmull-Rom character animation

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests