---
title: "Quaternion Slerp in C#: take the shortest path"
url: https://recursiveai.org/onemath/cookbook/interpolate-quaternion-with-slerp/
description: "Use quaternion Slerp in C# to take the shortest rotation, check the halfway direction, and see why opposite-sign quaternions give the same midpoint."
version: "1.0.0"
---

# Quaternion Slerp in C#: take the shortest path

**OneMath** is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/interpolate-quaternion-with-slerp/interpolate-quaternion-with-slerp.zip)

Slerp blends two unit quaternions along the shortest rotation.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;

var start = QuaternionD.Identity;
var end = QuaternionD.CreateFromAxisAngle(
    Vector3D.UnitZ, 3 * Math.PI / 2);
var mid = QuaternionD.Slerp(start, end, .5);
var v = mid.Rotate(Vector3D.UnitX);
var text = FormattableString.Invariant(
    $"({v.X:F3}, {v.Y:F3}, {v.Z:F3})");
Console.WriteLine(text);
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/interpolate-quaternion-with-slerp/interpolate-quaternion-with-slerp.zip), then `dotnet run`.

Prints:

```text
(0.707, -0.707, 0.000)
```

## The full program

```csharp
using RecursiveAI.OneMath;

var start = QuaternionD.Identity;
var end = QuaternionD.CreateFromAxisAngle(
    Vector3D.UnitZ, 3 * Math.PI / 2);
var mid = QuaternionD.Slerp(start, end, .5);
var v = mid.Rotate(Vector3D.UnitX);
var text = FormattableString.Invariant(
    $"({v.X:F3}, {v.Y:F3}, {v.Z:F3})");
Console.WriteLine(text);

var opposite = new QuaternionD(
    -end.X, -end.Y, -end.Z, -end.W);
var sameMid = QuaternionD.Slerp(start, opposite, .5);
Console.WriteLine($"Same midpoint: {mid == sameMid}");
```

```text
(0.707, -0.707, 0.000)
Same midpoint: True
```

The midpoint turns the positive X direction 45° clockwise around Z: X stays positive and Y goes negative. The full program also negates every component of the destination and gets the same midpoint. The input angle is +270°, but the shortest way to the same orientation is −90°.

`Slerp` assumes unit-length inputs; it does not reject non-unit quaternions or clamp `t`.

## Variations

For a point a quarter of the way along the same rotation, append this to the snippet at the top:

```csharp
var quarter = QuaternionD.Slerp(start, end, .25);
var direction = quarter.Rotate(Vector3D.UnitX);
Console.WriteLine(FormattableString.Invariant(
    $"({direction.X:F3}, {direction.Y:F3}, {direction.Z:F3})"));
```

The added line is `(0.924, -0.383, 0.000)`, the X direction turned −22.5°.

## Related recipes

- [Rotate vectors with quaternions](https://recursiveai.org/onemath/cookbook/vector-and-quaternion-math/)
- [Compose rigid transforms](https://recursiveai.org/onemath/cookbook/compose-rigid-transforms/)

## The short answer

To interpolate between two rotations in C#, pass unit quaternions to `QuaternionD.Slerp(start, end, t)`. Use `t = 0.5` for the midpoint. OneMath chooses the shorter arc without changing the destination orientation. To verify the result, rotate a direction that isn’t on the rotation axis. In the example, the X direction ends up at roughly `(0.707, -0.707, 0)`, which confirms a clockwise turn of 45°, not a counterclockwise turn of 135°.

## How do you find the shortest rotation between two quaternions?

A unit quaternion and its all-component negation describe the same rotation. But those two sets of four components sit on opposite sides of the unit sphere in quaternion space. If you use the wrong representative for the destination, interpolating toward it can take the long route in physical rotation.

OneMath calculates `QuaternionD.Dot(start, end)`. If the dot product is negative, it changes the sign of all four destination components before blending. This is not the same as taking the inverse: an inverse of a unit quaternion negates only its vector part. The sign flip in Slerp leaves the destination orientation alone. At exactly zero dot product, the two arcs are tied (the orientations differ by 180°), and OneMath keeps the destination sign you supplied.

In the example the 270° axis angle gives a negative dot with the identity. Negating that destination ahead of time gives a positive dot, and both inputs produce the same midpoint. For a general rotation check, compare the directions the quaternions rotate, not just their components. `==` compares components, so opposite signs may compare unequal even though the rotations match.

## What is the difference between Lerp and Slerp?

Slerp stands for spherical linear interpolation. Lerp blends the four components along a straight line; even if both endpoints have unit length, intermediate values need not. Normalizing afterward brings them back to unit length, but equal changes in `t` generally don’t mean equal changes in angle. Slerp blends along the sphere: on its spherical branch, evenly spaced `t` values divide the angle into equal steps, up to rounding. OneMath uses normalized Lerp instead when the sign-adjusted dot product is above `0.9995`, where the endpoints are nearly parallel. See the [pinned `Slerp` implementation](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/QuaternionD.cs#L205-L231).

For a rotation between two poses, use a fraction in `[0, 1]`. If you compute the fraction from elapsed time and want to stop at the target, clamp it before passing it to `Slerp`. Keep the original start pose fixed while `t` changes; feeding each result back as the new start is a different motion.
