def default_terminal_condition_for_agility(env,
                                           max_roll: float = 1.8,
                                           max_pitch: float = 1.8,
                                           min_height: float = 0.0,
                                           enforce_foot_contacts: bool = False):
  """A default terminal condition for more agile tasks (i.e. jumping).

  The robot is considered as fallen if the base position is too low, the base
  tilts/rolls too much or parts of the body other than the feet touch the
  ground.

  Args:
    env: An instance of the gym env.
    max_roll:  Max roll before the episode terminates.
    max_pitch:  Max pitch before the episode terminates.
    min_height:  Min height before the episode terminates.
    enforce_foot_contacts: Ensure that contacts are established with the feet.

  Returns:
    A boolean indicating if the episode should be terminated.
  """

  # Make sure that contacts are only made with the robot's feet.
  unwanted_collision = False
  if enforce_foot_contacts:
    knee_link_ids = [2, 5, 8, 11]
    contacts = env.pybullet_client.getContactPoints(bodyA=env.robot.robot_id)
    for contact in contacts:
      if contact[1] != contact[2]:
        foot_contact = contact[3] in knee_link_ids
        unwanted_collision = unwanted_collision or not foot_contact

  roll, pitch, _ = env.robot.base_roll_pitch_yaw
  pos = env.robot.base_position
  return (abs(roll) > max_roll or abs(pitch) > max_pitch or
          pos[2] < min_height or unwanted_collision)