## A Study of Inverse Kinematics

30 April 2020
Inverse kinematics is the mathematical process of calculating the variable joint parameters needed to place the end of a kinematic chain (Source)

As humans, we can easily think, "I want my hand in this location" and you move your hand there. When doing this, your joints in your arm, shoulder, and wrist either contract or expand to reach said location without much thought. This is very easy for us to do since we have had limbs our entire life. However, computers have to make these calculations when it is being adapted to a six-axis robotic arm or a 3D character with proper rigging.

We can see the results of these formulas in the short video below of a six-axis robotic arm drawing Shrek. This was part of a small project I did in college to understand how to program these arms.

Six Axis Robot Drawing

For this project, we used an array of different points that we mapped on an X-axis and Y-axis. From there the built-in software would calculate the rotation of each joint thus creating inverse kinematics in the robotic arm. However, these robotic arms do have some limitations such as a work envelope. A work envelope refers to the full range the machine can reach such as when the arm is fully extended. This is limited by the robot's arms and its design of axes.

That being said, 3D models do not have these limitations since an arm's length can just be increased or other axes added. I focused this month on implementing inverse kinematics within Unreal Engine 4. I have been learning many things about creating realistic characters in 3D environments and wanted my characters to interact with the environment correctly. The character uses open-source animations that are available on Mixamo. This is a great resource if you do not have a mocap studio readily available as well as easy placeholder animations. These animations are great, however, in the 3D world I have created, the ground is not flat all the time. Without the ground is perfectly flat, the character's feet will sometimes clip through the terrain.

Before Inverse Kinematics

We can see in the image above how the character's feet are cutting through the terrain and the legs are not properly adjusting to the elevation. To fix this behavior, the shins, feet, and waist will need to be dynamically adjusted to react to a different elevation.

To do this, I first got the XY position of the character's feet. I also got the character's location and added the negative half-height of the character to the character's Z value. This will be the max height the character's foot can go. From these XYZ values, I was able to use a function to trace the collision of the character's foot to the ground. This calculated the correct elevation for the feet, however, it does not calculate the correct rotation.

Without correctly rotating the feet, the foot can be elevated correctly but not aligned with the terrain. This can cause some wonky effects. To fix this, I was able to grab the collision's XYZ location from the foot elevation calculation. From this, I calculated the inverse tangent of the Y-axis & Z-axis and the X-axis and Z-axis. I then set each foot's roll and pitch to these values and did not touch the yaw. I let the yaw be dictated by the animation so the foot will not turn some crazy directions.

Foot Pitch & Roll

At this point, the legs and feet align properly to the ground but the hips are stationary. This means that the feet or leg can be on a surface much higher than the hips. To combat this, I was able to take the lowest Z value from either foot and subtract the X value. Then I used a lerp function to smoothly change the character hip height to the new calculated height.

Before & After

I am pretty satisfied with the results of this project. There are still some issues with the way I calculated the inverse kinematics but, I am still working on troubleshooting these at a later time. In the image above you can see the feet now rotate to the uneven terrain. This is exactly what I was hoping to accomplish in this project.