Quaternion

csharp
public struct Quaternion : IEquatable<Quaternion>, IFormattable

Namespace NodeWorld.Flux.

A rotation (unit quaternion, Hamilton product: lhs * rhs rotates by rhs, then by lhs). Euler angles in degrees, applied around z, then x, then y (the convention of Unity).

Constants

kEpsilon

csharp
public const float kEpsilon = 0.000001f

The tolerance of ==: two rotations are equal when the dot product of their quaternions is above 1 - kEpsilon.

Fields

x

csharp
public float x

x of the vector part (not an angle).

y

csharp
public float y

y of the vector part (not an angle).

z

csharp
public float z

z of the vector part (not an angle).

w

csharp
public float w

The scalar part (not an angle).

Constructors

Quaternion

csharp
public Quaternion(float x, float y, float z, float w)

A quaternion of these four components.

Properties

identity

csharp
public static Quaternion identity { get; }

No rotation.

this[]

csharp
public float this[int index] { get; set; }

x, y, z, w by index 0 to 3.

eulerAngles

csharp
public Vector3 eulerAngles { get; set; }

The Euler angles in degrees, each in [0, 360).

normalized

csharp
public Quaternion normalized { get; }

This quaternion at length 1.

Methods

Set

csharp
public void Set(float newX, float newY, float newZ, float newW)

Sets the four components.

Dot

csharp
public static float Dot(Quaternion a, Quaternion b)

Dot product of two quaternions.

Angle

csharp
public static float Angle(Quaternion a, Quaternion b)

Angle in degrees between two rotations.

Euler

csharp
public static Quaternion Euler(float x, float y, float z)

The rotation of z degrees around the z axis, then x degrees around the x axis, then y degrees around the y axis, as Unity's.

csharp
public static Quaternion Euler(Vector3 euler)

Euler(euler.x, euler.y, euler.z).

AngleAxis

csharp
public static Quaternion AngleAxis(float angle, Vector3 axis)

The rotation of angle degrees around axis; identity for a zero axis.

ToAngleAxis

csharp
public void ToAngleAxis(out float angle, out Vector3 axis)

The angle in degrees and the axis of this rotation (the x axis when there is no rotation).

Inverse

csharp
public static Quaternion Inverse(Quaternion rotation)

The opposite rotation.

Normalize

csharp
public static Quaternion Normalize(Quaternion q)

q at length 1 (identity for a zero quaternion).

csharp
public void Normalize()

Brings this quaternion to length 1.

Lerp

csharp
public static Quaternion Lerp(Quaternion a, Quaternion b, float t)

From a (t = 0) to b (t = 1), the short way, normalized: quicker than Slerp, less even. t is kept between 0 and 1.

LerpUnclamped

csharp
public static Quaternion LerpUnclamped(Quaternion a, Quaternion b, float t)

From a (t = 0) to b (t = 1), the short way, normalized; t is not limited.

Slerp

csharp
public static Quaternion Slerp(Quaternion a, Quaternion b, float t)

From a (t = 0) to b (t = 1) at an even angular speed, the short way. t is kept between 0 and 1.

SlerpUnclamped

csharp
public static Quaternion SlerpUnclamped(Quaternion a, Quaternion b, float t)

From a (t = 0) to b (t = 1) at an even angular speed, the short way; t is not limited.

RotateTowards

csharp
public static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)

from turned towards to by maxDegreesDelta degrees at most.

LookRotation

csharp
public static Quaternion LookRotation(Vector3 forward, Vector3 upwards)

The rotation whose forward (z) is "forward" and whose up is as close as possible to "upwards".

csharp
public static Quaternion LookRotation(Vector3 forward)

The rotation whose forward (z) is forward and whose up is as close as possible to the world's up.

FromToRotation

csharp
public static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection)

The rotation that turns fromDirection onto toDirection.

Equals

csharp
public bool Equals(Quaternion other)

The four components are exactly equal.

csharp
public override bool Equals(object other)

other is a quaternion with exactly the same components.

GetHashCode

csharp
public override int GetHashCode()

A hash of the components.

ToString

csharp
public override string ToString()

"(x, y, z, w)" with 5 decimals.

csharp
public string ToString(string format)

The same text with the numbers in format.

csharp
public string ToString(string format, IFormatProvider formatProvider)

The same text with the numbers in format (F5 when empty) and formatProvider (the invariant culture when null).

Operators

operator *

csharp
public static Quaternion operator *(Quaternion lhs, Quaternion rhs)

The two rotations in sequence, as Unity's: lhs, then rhs in the frame lhs made.

csharp
public static Vector3 operator *(Quaternion rotation, Vector3 point)

Rotates the point: v + 2w (u x v) + 2 u x (u x v), with u the vector part.

operator ==

csharp
public static bool operator ==(Quaternion lhs, Quaternion rhs)

Same rotation (q and -q included) up to 1e-6.

operator !=

csharp
public static bool operator !=(Quaternion lhs, Quaternion rhs)

The rotations differ (see ==).