When we think about artificial intelligence, weÂ
usually think about natural language processing,  or image recognition. But AI hasÂ
many other less known applications.  One that Iâm very passionate aboutÂ
is replicating natural movement.  I just love how some systems are able to moveÂ
in a way that makes it look like they are alive. So, for the past months, Iâve been workingÂ
on this character animation system for Unity.  You see, standard animations are playedÂ
completely out of the physics simulation.  For example, if a character is walking andÂ
something collides with its arm, the other object  may react to the collision, but the arm animationÂ
wonât be modified at all. For animations to be  responsive to the physical environment, we needÂ
to simulate them using whatâs called physical  animations, also known as active ragdolls.
This type of animation demands more processing  power than standard animations and is much harderÂ
to set up, that's why it isnât very commonly used.  But in the last decade, itâs been rising inÂ
popularity due to the increasing potential of  tools like Unity and Unreal. Games like Human FallÂ
Flat and Gang Beasts are some famous examples.  Itâs also been used in big blockbusters such asÂ
GTA, which uses a software called Euphoria that  generates realistic animations in real-time, butÂ
they use much more than just physical animations  so itâs not that relevant for us. Letâs get started. The first thing we need is a model, I made this one quickly. Yes, Iâm obviously not a 3D modeler. After that,  I made a very simple skeleton along with someÂ
quick animations. Yess, Iâm not an animator  either. I pulled everything into Unity and got theÂ
standard animations working. If you look at how  Unity handles characters with skeletons, you canÂ
see that it creates a GameObject for each bone,  then the Animator component movesÂ
these game objects to play animations. Some bones are child to others, which meansÂ
that when you move or rotate the parent,  the child will follow it accordingly. If youÂ
want to move the arm, you just need to move  the shoulder and the position of all itsÂ
children will be automatically calculated.  The process of finding how child objectsÂ
move with respect to their parents  is called Forward Kinematics. It may lookÂ
simple, but itâs the basis of classic robotics,  and knowing it will allow us to understand aÂ
more advanced technique later in the video. Getting back to physical animations, the firstÂ
thing I tried was to put the joints and physical  rigid bodies directly on those animated gameÂ
objects. It was a stupid idea indeed. Since the  animator is forcing their positions and rotationsÂ
in each frame, I canât control them with joints. Trying to physically move theÂ
animated body wonât get me anywhere.  Instead, Iâll do what everyone suggests onÂ
forums and videos all over the internet:Â Â use one body for animation, and make a duplicateÂ
that tries to match it by applying forces. Our character now contains two different bodies.Â
The first is just a normal animated character,  which plays the animation weâre trying to achieve,Â
but without any physics. The second one is a  copy of the first, but instead of being animated,Â
itâs made up of rigid bodies connected by joints,  itâs whatâs usually called a ragdoll.
Joints are just physical entities that  link two rigid bodies together, makingÂ
them rotate and move around each other  in a certain way we can configure.
But as it stands now, itâs just that,  a normal ragdoll, it does nothing but fallingÂ
awkwardly. We need to bring some life into it  with some movement. To do so weâll be usingÂ
a feature of joints called target rotation,  which applies a torque to reach aÂ
specific rotation, as its name implies.... If we could just set this target rotationÂ
to that of the animated body for each joint,  we would obtain a ragdoll that wouldÂ
try to match our predefined animation,  exactly what weâre looking for.
Itâs actually not as easy as it appears.  Rotations are messy, and quaternions areÂ
evil. Making joints match the rotation of its  animated peers is not writing âtargetRotation =Â
animatedRotationâ. But thanks to the advancements  made in the 20th century, weâve got this niceÂ
thing called the Internet, and someone has  already done it. So thank you, Michael Stevenson,Â
your efforts will be put to good use, hopefully. By the way, Iâve hidden the animated model byÂ
disabling it, but doing so stops animations from  being played unless you change the culling mode toÂ
âAlways Animateâ. Just in case someone is trying  to follow this as if it was a tutorial, which itÂ
isnât. If you want an actual tutorial to know how  to implement this yourself, you can find it in theÂ
description of this video. I will also put a link  to the Github repository of this same project, inÂ
case you want to test it yourself or use it as a  basis, feel free to do whatever you want with it.
This is nice and everything, but itâs completely  useless. Yes, we have physical animations, butÂ
right now Iâm fixing the hips to see it properly,  this is what happens when I release it. And soÂ
we get into one of the most complicated subjects  Iâve ever dived into: balance. We need to getÂ
this thing to balance itself without falling.  Not only that, we need to make it stable enoughÂ
so it can walk properly using the contact between  the feet and the floor. This is a concept thatâsÂ
been highly explored in robotics, itâs absolutely  fascinating and Iâll be working on it for the nextÂ
few months as part of my bachelorâs final project. Naturally, I will not be dealing with balanceÂ
in this video, there are other methods that can  be used... Instead of balancing with its ownÂ
movement, we can just force it to stay upright  with artificial forces. This may not work for realÂ
world applications, but games are not real world  applications.Human Fall Flat does this veryÂ
clearly, just look at how the hips tend to  stay vertical even in unbalanced situations.
The more straightforward way to achieve this is  by locking the rotation of the hips on the X and ZÂ
axes, that way the ragdoll literally cannot fall.  Unless you know for sure this isÂ
a good fit for your application,  I believe this to be an awful idea. It looksÂ
rigid and can be a source of huge glitches. The second method is using another joint. This oneÂ
will connect the hips to a new rigid body, which  will be upright, and the hips will try to get toÂ
that rotation through the jointâs target rotation  feature. This new body will be kinematic, whichÂ
means it cannot be affected by external forces,  so it will always be upright since theÂ
jointâs torque will only affect the hips.  Itâs given good results to me so far, but itÂ
produces a springy feeling Iâm not a fan of. By the way, if youâre wonderingÂ
why itâs always the hips,  itâs because itâs where theÂ
bodyâs center of mass is. The last method is applying torque to rotateÂ
the torso towards an upright direction.  This is the same as what the joint of the previousÂ
method does internally, so there are no apparent  differences. But actually, there are. Doing it byÂ
ourselves allows us to decide how much torque is  applied at any given point. The previous jointÂ
had a torque output that worked like a spring.  The more you get out of balance, the more torqueÂ
it delivers, just like this [show graph]. But now  we can choose that function, so we can changeÂ
the way it delivers torque, and thus get rid of  the springiness, at least to some extent.
Okay so now we can sort of play physical  animations with our character. Itâs not perfectÂ
and it would require a lot more tuning to have  something that could be used commercially, butÂ
as a prototype, it works pretty well nonetheless. But thereâs one more thing that we havenât doneÂ
yet. What if instead of playing a predetermined  animation we wanted to generate movementÂ
dynamically depending on the context. This is best exemplified by making theÂ
character reach for an object with its hand.  We cannot use an animation because the characterÂ
will not always be in the same spot in relation  to the object. And teleporting it, while it canÂ
look decent enough with usual animations, will  look terrible combined with physical animations.
We need a system that allows us to position the  hand where we need it and let the body findÂ
the ideal configuration for that to happen. With forward kinematics, we could calculate aÂ
childâs position given that of its parents. Now we  need just the opposite, given the childâs positionÂ
and rotation, find the parentâs configuration that  makes it possible. This can be done by a complexÂ
mathematical process called Inverse Kinematics The problem is, it can be done in many ways,Â
you can reach the same spot with your hand  in infinitely different postures. To solveÂ
that uncertainty, we need whatâs called a  hint , an object in space that tells a certainÂ
bone where to point to. This adds a constraint  to the system and reduces the infinitely possibleÂ
configurations to just one. In our case, we need  a hint for the elbow, once the elbow knows whereÂ
to point to, our solver can solve the equations. Unity has a simple InverseÂ
Kinematics system built-in.  Itâs not perfect, and Iâve run into some problemsÂ
while using it, but it works well enough for the  projectâs scope. This is actually the part of thisÂ
project Iâve spent more time on. I think I might  have overdone it for what I actually needed. ButÂ
anyway, I learned some new things, so itâs okay. And this is all I had to show you, itâs not aÂ
perfect solution and there are a million ways to  improve it, but this is not a commercial projectÂ
so it makes no sense for me to spend more time  polishing it. It looks cool and it has taughtÂ
me a lot, so it accomplished its objectives. I hope you enjoyed this little journey ofÂ
programming physical animations in Unity.  Iâm very interested in natural movementÂ
generation in real-time, as well as other  types of artificial intelligence. Iâll be talkingÂ
about similar topics in this channel regularly,  just in case you want to hit aÂ
certain button. See you next time.
Thanks for sharing đđ
Quaternions are evil. Preach, brother!
Wow, awesome video, Sergio! The simplicity in which you demonstrated the concepts was great. And the splices were perfect.
Enjoyed the vid ! lots of interesting stuff , iv subscribed to your channel
:)
thanks for sharing!
Awesome content. It was enlightening to watch a video on a topic I had always wondered about.
Subbed !
And please do make more videos :)