In a videogame context, IK can be used to create a smooth animation of a character's limb at a desired position based on an end point in space. This end position in space is also known as the end-effector and it can be procedurally calculated using collision queries. One common example of IK in videogames is for making sure the character's feet conform to the ground topology or stairs, etc.
I think the picture below gives a better intuition on how the end-effector is used in IK to drive the desired animation.
In my example above, I'm using an IK setup to override the punching animation so that the hand bones don't go through the walls. Below I will describe some of the essentials of setting up IK in UE4 for my project as well as some of the lessons learned. Keep in mind I'm just scratching the tip of the iceberg here.
- On an event tick event, set up a Sphere Trace by Channel (Visibility) and make the trace start at the lower arm socket and end at the hand socket. Create appropriate sockets (if you need to) using Persona. Do this for both the left arm and the right arm. The radius for the trace sphere may need to be tweaked appropriately.
- Break the hit result in order to obtain the collision location in world space and store this in a variable. This result will effectively be our end effector. Do this for both sphere traces on the left and right arms.
- The animation blueprint will make use of an alpha blending value where 1.0 uses the IK result and 0.0 means don't use it at all. On a collision hit, set this alpha value to 1.0 and on a non collision make it drop back to zero smoothly via a 'Finterp to' node.
In the animation blueprint's event graph
- Cast the Pawn Owner to the appropriate character blueprint where you are setting up your sphere trace and obtain all the relevant data that will be used in the animation blueprint from it. Namely, (and do this for each arm) get the end effector location, and the alpha value.
In the animation blueprint's animation graph
Append the following nodes right before the final pose and right after the state machine pose and the montages pose has been calculated.
- Convert Local to Component node so that the animation data gets converted from bone local space to component space. This is done because the Two Bone IK node to be used to calculate IK takes animation data in component space as an input.
- Create two 'Two Bone IK' nodes. One will be the IK solver for the left arm and the other for the right arm.
- A 'Two Bone IK' node takes 5 inputs. The component pose, the alpha value that you set up in the character blueprint to decide how much control to assign to this bone control node, the final bone in the IK chain (i.e., the hand bone), the parent bone that acts as the 'joint target' for the hand (i.e., the lower hand bone), and the end effector location.
- Make sure the end effector location space is set to world space as this is the result of the sphere trace collision that we did in the character's blueprint.
- The joint target input is actually a bit more involved. Not only do we specify the joint bone to use but also we specify a 'pole' (constraint) for the bone. An illustration on how to set those up in Maya can be found here. This position for the pole is the hardest thing to get right about this whole IK setup thingy. Mainly because Persona does not offer a tool for visually adjusting this pole position. For now, this is mainly a trial and error process.
- Before passing the final result to the final 'Final animation pose' node, make sure you convert back to Local space using a 'Component to Local' node.
- At first I was seeing a very jumpy looking IK animation. Then I realized it was due to the alpha blending value for the IK node. Keep in mind that 1.0 means the node has total control of the bone position and 0.0 means none. Therefore, in order to generate a smoother animation, don't let the alpha value just change back to 0 when the collision disappears but rather interpolate that value smoothly from 1.0 to 0.0 over some time. The fInterpolate node is perfect for this.
- Placement of the pole joints is critical for creating natural looking Inverse Kinematics. Currently the editor does not offer any visual tool to do this so only way is to do a lot of trial and error. Assuming you chose bone space for the Joint pole position when you click on the IK node, the viewport will display the current axis space for the joint bone selected so you can use that to get a better idea on how to tweak the joint pole position
- Capsule radius is critical from preventing the arms to get too deep into the wall that the IK solution creates awkward looking movements of the joins. Granted, it has more to do with the placement of the joint poles but given the fact that there are no visual tools for the pole placements its better just to avoid the character punching animation to get too deep into the other side of the wall.