Monday, May 25, 2015

Combining Ragdoll and Keyframe Animation to Achieve Dynamic Poses

One of the greatest challenges in game character animation is the adaption of characters with the dynamic environments and dynamic actions of users. Each character owns many animations which help the animation controllers to respond well in different situations. Although owning plenty of animations can help to achieve better visual responses but it can't always be enough, since the environment which the character is moving in, can be dynamic and can be changed through time. Also player actions are dynamic and this can affect animations as well. Each action of the player needs feedback and animation plays a huge role here.

So even if the character owns many animations, it can't cover all situations, and the motions' visual and responsiveness can become unacceptable at some points. To overcome these issues, many animation techniques have been invented. Some of them are semi procedural, like animation blending and some of them are fully procedural like IK and physically based animations. In this post, I want to talk a little about combination of physical animation and keyframe animation and briefly show how they can be blended together to create dynamic poses. This subject is a  huge one, so I'm just going to briefly talk about it. At the end a case study is provided too.

Combining Keyframe Animation and Ragdoll


Using ragdoll animations have become a standard in the video gaming industry. You can see it in many games but nowadays using simple ragdoll is not acceptable for many game developers which are caring more about animations in their games. So they are using more advanced physical animations. There exists different physical animation techniques. Here I want to point out one which can be very useful in video games and some game engines or physics/animation APIs are providing it.

Using ragdoll alone, usually creates unaccepted motions and at many points it is not providing human like motions and it can be used just instead of die animations. It can't be used for a living character. To have a better ragdoll simulation, the ragdoll animations can be driven by the active keyframe animations. Assume that you have an animation which is generating poses at each frame and you have a ragdoll skeleton which consists of different physical joints and hinges. Here we can get the pose generated by keyframe animation as a target and apply forces to the physical joints of the ragdoll skeleton to reach this target. With this, the ragdoll skeleton is trying to follow animation at each frame while it can react to environment and applied forces within game. Also the pose generated by the physical skeleton can be blended by the actual pose of animation so we can make transitions between physical animation and keyframe animations. Having this can help a lot to achieve dynamic poses. Let's consider a simple example. You have a shooter game. You are shooting at an enemy while he is running. Based on the magnitude of the force caused by bullet, you can change the blend factor of the pose generated between ragdoll and currently playing animations. With this you will have a transition between animation and ragdoll while your ragdoll skeleton is reacting to the bullet physical force. Also the ragdoll skeleton tries to reach itself to the current animation pose.

If you want to find out more about this technique in details and find out physical joint equations behind it, I recommend to check this post from my blog. Also Havok physics API documentation is describing this technique very well but it's not talking about the equations.

This technique is beautifully implemented in Havok animation tool. You can define a ragdoll skeleton for a character and let it follow the currently playing animations. It also provides pose blending between ragdoll and currently playing animations. So it helps a lot to achieve dynamic acceptable poses.

Next I want to explain my mentioned example more in action. I implemented a simple body hit reaction by blending 4 run animations with the animation driven ragdoll. I used Havok Animation Tool and Vision engine which both are provided within Project Anarchy tool sets. In the Havok animation tool, "Rigid Body Ragdoll Controls Modifier" is responsible for animation driven ragdoll.

A Simple Case Study


Now let's consider the example more in action. I have a character which is running and I want the character to continue running while being hit. Each time the hit point differs. You can assume that the character is hit by randomly fired bullets. For this I made an animation blend tree with 4 run animations. One normal run and three unstable runs.

First, based on the direction of the applied force to different joints, I'm blending between three unstable runs. I have a unstable run left, forward and right.

Second, based on the magnitude of applied force, I'm blending between normal run and the unstable runs. So here I've generated a pose just by blending between 4 animations based on the direction and magnitude of the applied force. Although animation blending can create smooth poses, but it is not enough for creating dynamic poses in this example. So I'm applying animation driven ragdoll as a post process on the currently generated poses from the animation blend tree. The animation driven ragdoll is trying to apply forces to its motors and physical joints at each frame to make ragdoll skeleton pose close to the pose generated by active animations. So at each frame we have two poses. One which is generated by the animation blend tree and one which is generated by the animation driven ragdoll. The animation driven ragdoll is also affected by the bullets force bullets.

To achieve a dynamic and acceptable pose I blend between two poses generated by two animation systems based on the bullet force magnitude. After applying force, I'm blending back to actual animations through time so the pose generated by animation driven ragdoll, fades out through time. And again if the character is hit by a bullet, I apply forces to ragdoll skeleton and blend it with animation (based on force magnitude).

Conclusion


If you want to create such a system, it's better to create a platform which can blend animations very well like an advanced blend tree. The blend tree can respond to the input which is a force vector here. Then as a post process, you can use animation driven ragdoll to apply dynamic forces to body and blend it with the currently playing animations. The blend tree used in this example was very simple. You can use more complex blending systems with more animations to have a more realistic character.

The video here shows the example I described. I'm applying randomly generated forces to randomly selected joints to simulate bullet hits on the character body while he is running. As I mentioned, the character can have much more animations while being combined with ragdoll to shape more realistic motion.

After each hit, the character is blending back to its active animations smoothly.