---
url: /dyad/stable/stdlib/MultibodyComponents/components/Distance.md
description: >-
  Dyad Distance docs.  Measures the Euclidean distance between the origins of
  two frame connectors.
---

# `Distance` {#Distance}

Measures the Euclidean distance between the origins of two frame connectors.

Frame-invariant scalar: no `resolve_in_frame` is needed. A small regularization parameter `s_small` provides a smooth (differentiable) extension near zero distance, avoiding the `sqrt` derivative singularity there.

This is a sensor: no forces or torques are exerted.

## Usage {#Usage}

`MultibodyComponents.Distance(s_small=1e-10)`

## Parameters: {#Parameters:}

|      Name |                                                                                                           Description | Units | Default value |
| ---------:| ---------------------------------------------------------------------------------------------------------------------:| -----:| -------------:|
| `s_small` | Distance threshold below which a smooth quadratic replaces sqrt, avoiding the derivative singularity at zero distance |     m |         1e-10 |

## Connectors {#Connectors}

* `frame_a` - Frame3D is the fundamental 3D connector used for 6DOF motion. Most components have one or several `Frame`

connectors that can be connected together ([`Frame3D`](/stdlib/Dyad/connectors/Frame3D#Frame3D))

* `frame_b` - Frame3D is the fundamental 3D connector used for 6DOF motion. Most components have one or several `Frame`

connectors that can be connected together ([`Frame3D`](/stdlib/Dyad/connectors/Frame3D#Frame3D))

* `distance` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

## Variables {#Variables}

| Name |                                              Description | Units |
| ----:| --------------------------------------------------------:| -----:|
| `L2` | Squared distance between the frame_a and frame_b origins |     – |

## Behavior {#Behavior}

$$ \begin{align}
\mathtt{frame_a.f}\_{1}\left( t \right) &= 0 \\
\mathtt{frame_a.f}\_{2}\left( t \right) &= 0 \\
\mathtt{frame_a.f}\_{3}\left( t \right) &= 0 \\
\mathtt{frame_a.tau}\_{1}\left( t \right) &= 0 \\
\mathtt{frame_a.tau}\_{2}\left( t \right) &= 0 \\
\mathtt{frame_a.tau}\_{3}\left( t \right) &= 0 \\
\mathtt{frame_b.f}\_{1}\left( t \right) &= 0 \\
\mathtt{frame_b.f}\_{2}\left( t \right) &= 0 \\
\mathtt{frame_b.f}\_{3}\left( t \right) &= 0 \\
\mathtt{frame_b.tau}\_{1}\left( t \right) &= 0 \\
\mathtt{frame_b.tau}\_{2}\left( t \right) &= 0 \\
\mathtt{frame_b.tau}\_{3}\left( t \right) &= 0 \\
\mathtt{L2}\left( t \right) &= dot\left( broadcast\left( -, \mathtt{frame_b.r_0}\left( t \right), \mathtt{frame_a.r_0}\left( t \right) \right), broadcast\left( -, \mathtt{frame_b.r_0}\left( t \right), \mathtt{frame_a.r_0}\left( t \right) \right) \right) \\
\mathtt{distance}\left( t \right) &= ifelse\left( \mathtt{s_small}^{2} < \mathtt{L2}\left( t \right), \sqrt{\mathtt{L2}\left( t \right)}, \frac{\mathtt{L2}\left( t \right) ~ \left( 3 + \frac{ - \mathtt{L2}\left( t \right)}{\mathtt{s_small}^{2}} \right)}{2 ~ \mathtt{s_small}} \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Measures the Euclidean distance between the origins of two frame connectors.

Frame-invariant scalar: no `resolve_in_frame` is needed. A small regularization
parameter `s_small` provides a smooth (differentiable) extension near zero
distance, avoiding the `sqrt` derivative singularity there.

This is a sensor: no forces or torques are exerted.
"""
component Distance
  frame_a = Frame3D() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  frame_b = Frame3D() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 950, "y1": 450, "x2": 1050, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  distance = RealOutput() {
    "Dyad": {
      "placement": {"diagram": {"x1": 450, "y1": 1000, "x2": 550, "y2": 1100, "rot": 90}}
    }
  }
  "Distance threshold below which a smooth quadratic replaces sqrt, avoiding the derivative singularity at zero distance"
  parameter s_small::Length = 1e-10
  "Squared distance between the frame_a and frame_b origins"
  variable L2::Real
relations
  frame_a.f = [0, 0, 0]
  frame_a.tau = [0, 0, 0]
  frame_b.f = [0, 0, 0]
  frame_b.tau = [0, 0, 0]
  L2 = dot(frame_b.r_0 - frame_a.r_0, frame_b.r_0 - frame_a.r_0)
  distance = ifelse(L2 > s_small ^ 2, sqrt(L2), L2 / (2 * s_small) * (3 - L2 / s_small ^ 2))
metadata {
  "Dyad": {
    "icons": {"default": "dyad://MultibodyComponents/TwoFrameSensor.svg"},
    "labels": [
      {
        "label": "$(instance)",
        "x": 500,
        "y": 150,
        "rot": 0,
        "attrs": {"font-size": "140"}
      },
      {"label": "distance", "x": 500, "y": 680, "rot": 0}
    ]
  }
}
end
```

```dyad
"""
Measures the Euclidean distance between the origins of two frame connectors.

Frame-invariant scalar: no `resolve_in_frame` is needed. A small regularization
parameter `s_small` provides a smooth (differentiable) extension near zero
distance, avoiding the `sqrt` derivative singularity there.

This is a sensor: no forces or torques are exerted.
"""
component Distance
  frame_a = Frame3D() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  frame_b = Frame3D() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 950, "y1": 450, "x2": 1050, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  distance = RealOutput() {
    "Dyad": {
      "placement": {"diagram": {"x1": 450, "y1": 1000, "x2": 550, "y2": 1100, "rot": 90}}
    }
  }
  "Distance threshold below which a smooth quadratic replaces sqrt, avoiding the derivative singularity at zero distance"
  parameter s_small::Length = 1e-10
  "Squared distance between the frame_a and frame_b origins"
  variable L2::Real
relations
  frame_a.f = [0, 0, 0]
  frame_a.tau = [0, 0, 0]
  frame_b.f = [0, 0, 0]
  frame_b.tau = [0, 0, 0]
  L2 = dot(frame_b.r_0 - frame_a.r_0, frame_b.r_0 - frame_a.r_0)
  distance = ifelse(L2 > s_small ^ 2, sqrt(L2), L2 / (2 * s_small) * (3 - L2 / s_small ^ 2))
metadata {
  "Dyad": {
    "icons": {"default": "dyad://MultibodyComponents/TwoFrameSensor.svg"},
    "labels": [
      {
        "label": "$(instance)",
        "x": 500,
        "y": 150,
        "rot": 0,
        "attrs": {"font-size": "140"}
      },
      {"label": "distance", "x": 500, "y": 680, "rot": 0}
    ]
  }
}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`SensorsTest`](Distance.md)
