Apply the inverse of a unit quaternion to bring a rotated vector back to its original direction.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; // 120° about (1, 1, 1): +X goes to +Y. var rotation = new QuaternionD( 0.5, 0.5, 0.5, 0.5); var original = new Vector3D(1, 0, 0); if (!rotation.TryInvert(out var inverse)) throw new ArgumentException("Bad norm"); var turned = rotation.Rotate(original); var restored = inverse.Rotate(turned); Console.WriteLine(turned); Console.WriteLine(restored); Console.WriteLine( (restored - original).Length() <= 1e-12);
Unzip the example, then dotnet run.
Prints the rotated vector, the recovered vector, and whether recovery is within 1e-12.
(0, 1, 0) (1, 0, 0) True
The full program
using RecursiveAI.OneMath; // 120° about (1, 1, 1): +X goes to +Y. var rotation = new QuaternionD( 0.5, 0.5, 0.5, 0.5); var original = new Vector3D(1, 0, 0); if (!rotation.TryInvert(out var inverse)) throw new ArgumentException("Bad norm"); var turned = rotation.Rotate(original); var restored = inverse.Rotate(turned); Console.WriteLine(turned); Console.WriteLine(restored); Console.WriteLine( (restored - original).Length() <= 1e-12); foreach (var invalid in new[] { default(QuaternionD), new QuaternionD(double.PositiveInfinity, 0, 0, 0) }) { if (!invalid.TryInvert(out _)) Console.WriteLine($"Rejected norm²: {invalid.LengthSquared()}"); }
(0, 1, 0) (1, 0, 0) True Rejected norm²: 0 Rejected norm²: ∞
The Boolean from TryInvert is required: on failure the out value is identity, which would quietly leave the rotated vector unchanged.
Variations
Append this to the snippet at the top when the starting rotation is known to be a unit quaternion.
// The original quaternion is already a unit rotation. var conjugate = rotation.Conjugated(); Console.WriteLine(conjugate.Rotate(turned));
(1, 0, 0)Related recipes
The short answer
For a unit QuaternionD that maps a vector from one frame to another, call TryInvert(out var inverse), check the Boolean result, then call inverse.Rotate(rotatedVector). Use a distance tolerance to compare the recovered vector with the original. The example uses an absolute tolerance of 1e-12 for a vector of length one.
How do you reverse a quaternion rotation?
Use the same rotation that moved the vector forward, not the difference between two sets of angles. The example’s quaternion has four components of 0.5, so its squared norm is exactly one. It sends the positive X unit vector to positive Y. Feeding that result to the inverse sends it back to positive X. If you’re undoing two steps, reverse their order too: undo the last rotation first and the first rotation last.
This recipe reverses a rotation about the origin. If the forward transform also added a translation after the rotation, subtract that translation from the transformed point before applying the inverse rotation. Applying the inverse directly to the translated point would rotate that offset too.
What is the difference between a quaternion conjugate and its inverse?
OneMath’s conjugate negates X, Y, and Z and leaves W unchanged. The inverse divides those conjugated components by the original squared norm. They are the same for a unit quaternion, which is why the conjugate variation also recovers the vector. Negating all four components isn’t conjugation: q and -q represent the same rotation.
TryInvert can compute the algebraic inverse of a non-unit quaternion, but a successful call doesn’t prove the input is a unit rotation. Rotate expects a unit quaternion. If you receive quaternion data from another system, check that the squared norm is positive and finite before normalizing the input for rotation. In particular, zero and nonfinite squared norms cannot be inverted by this API. The identity written to the out parameter on failure is not an inverse you can use to recover the vector.