ozz-animation

Samples - Foot ik

View foot_ik sample source code.

Description

Foot IK, or foot planting, is a technique used to correct character legs and ankles procedurally at runtime, as well as character/pelvis height, so that the feet can touch ground.

Initializing WebGL

Concept

The core concept is to apply two bone IK to the hip/knee/ankle joint chains of the character so each foot is at the correct height from the ground, and aim IK to the ankle so that it’s aligned to the ground. Raycast (from ankle position, directed down to the ground) is used to find the point and normal where each foot should be on ground. Foot height (heel, sole) and slope angle must be considered when computing ankle position, to be sure the orientated foot remains precisely on ground. The second important aspect is to adjust character height, so that the lowest foot touches the ground. The over leg(s) will be bent by IK.

This sample focuses on IK application and core maths. It’s not a complete foot planting implementation, as it would require more:

Sample usage

The sample allows to move and rotate the character on the collision mesh, with y value being automatically computed if “auto character height” option is enabled. Pelvis correction, two bone IK and aim IK can be activated independently. Debug settings are exposed to help understanding: showing joints, raycasts, IK targets…

Implementation

  1. At initialization time:
    1. Loads skeleton, animation and mesh. See “playback” sample for more details.
    2. Locates hip, knee and ankle joints for each leg, searching them by name.
  2. At run time:
    1. Updates base animation and skeleton joints model-space matrices. See “playback” sample for more details.
    2. Estimates character height on the floor, evaluted at its root position.
    3. For each leg, raycasts a vector going down from the ankle position. This allows to find the intersection point (I) with the floor.
    4. Comptutes ankle target position (C), so that the foot is in contact with the floor. Because of floor slope (defined by raycast intersection normal), ankle position cannot be simply be offseted by foot offset. See diagram below.

ankle correction

  1. Offsets the character down, so that the lowest ankle (lowest from its original position) reaches its targetted position. The other leg(s) will be ik-ed.
  2. Applies two bone IK to each leg, so the ankles reache their targetted position. This in computed in character model-space. Pole vector is given by original knee forward vector, such that the result remains close to original animation.
  3. Applies aim IK to each ankle, so the foot is correctly aligned to the floor. Uses floor normal as a reference to align ankle up vector. The forward direction of the foot (pole vector) is then driven by the ankle forward vector, which polls the foot toward it’s original (animated) direction.