Quaternion

package
v0.0.0-...-01268d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package Quaternion provides a unit quaternion used for representing 3D rotations.

Index

Constants

This section is empty.

Variables

View Source
var Identity = IJKX{X: 1}

Identity quaternion, representing no rotation. This has the same rotation as [Basis.Identity].

If a Vector3 is rotated (multiplied) by this quaternion, it does not change.

Functions

func AngleBetween

func AngleBetween(q, other IJKX) Angle.Radians

AngleTo Returns the angle between this quaternion and to. This is the magnitude of the angle you would need to rotate by to get from one to the other.

Note: The magnitude of the floating-point error for this method is abnormally high, so methods such as [IsApproximatelyZero] will not work reliably.

func AngleInRadians

func AngleInRadians(q IJKX) Angle.Radians

AngleInRadians returns the angle of the rotation represented by this quaternion.

Note: The quaternion must be normalized.

func AsArray

func AsArray(vec IJKX) [4]Float.X

func AsBasis

func AsBasis(q IJKX) Basis.XYZ

func Axis

func Axis(q IJKX) Vector3.XYZ

func Dot

func Dot(q, other IJKX) Float.X

Dot returns the dot product of this quaternion and other.

func EulerAngles

func EulerAngles(order Angle.Order, q IJKX) Angle.Euler3D

EulerAngles returns the quaternion's rotation in the form of Euler angles. The Euler order depends on the order parameter, for example using the YXZ convention: since this method decomposes, first Z, then X, and Y last. See the EulerOrder enum for possible values. The returned vector contains the rotation angles in the format (X angle, Y angle, Z angle).

func IsApproximatelyEqual

func IsApproximatelyEqual(q, other IJKX) bool

IsApproximatelyEqual returns true if this quaternion and to are approximately equal, by running IsApproximatelyEqual on each component.

func IsFinite

func IsFinite(q IJKX) bool

IsFinite returns true if this quaternion is finite, by calling IsFinite on each component.

func IsNormalized

func IsNormalized(q IJKX) bool

IsNormalized returns whether the quaternion is normalized or not.

func Length

func Length(q IJKX) Float.X

Length returns the length of the quaternion.

func LengthSquared

func LengthSquared(q IJKX) Float.X

LengthSquared returns the length of the quaternion, squared.

Types

type IJKX

type IJKX = struct {
	I Float.X // I component of the quaternion. This is the value along the "imaginary" i axis.
	J Float.X // J component of the quaternion. This is the value along the "imaginary" j axis.
	K Float.X // K component of the quaternion. This is the value along the "imaginary" k axis.
	X Float.X // X component of the quaternion. This is the value along the "real" axis.
}

The Quaternion built-in Variant type is a 4D data structure that represents rotation in the form of a Hamilton convention quaternion. Compared to the Basis type which can store both rotation and scale, quaternions can only store rotation.

A Quaternion is composed by 4 floating-point components: x, i, j, and k. These components are very compact in memory, and because of this some operations are more efficient and less likely to cause floating-point errors. Methods such as [Angle], Axis, and Slerp are faster than their Basis counterparts.

For a great introduction to quaternions, see this video by 3Blue1Brown. You do not need to know the math behind quaternions, as this package provides several helper methods that handle it for you. These include Slerp and SphericalCubicInterpolate, as well as Mul.

Note: Quaternions must be normalized before being used for rotation (see normalized).

Note: Similarly to Vector2 and Vector3, the components of a quaternion use 32-bit precision by default, unlike float which is always 64-bit. If double precision is needed, compile with the Go build tag 'precision_double'.

func Add

func Add(a, b IJKX) IJKX

func AddX

func AddX[X Int.Any | Float.Any](a IJKX, b X) IJKX

func Div

func Div(a, b IJKX) IJKX

func DivX

func DivX[X Int.Any | Float.Any](a IJKX, b X) IJKX

func Euler

func Euler(e Angle.Euler3D) IJKX

Euler constructs a Quaternion from Euler angles in YXZ rotation order.

func Exponential

func Exponential(q IJKX) IJKX

Exponential returns the exponential of this quaternion. The rotation axis of the result is the normalized rotation axis of this quaternion, the angle of the result is the length of the vector part of this quaternion.

func Inverse

func Inverse(q IJKX) IJKX

Inverse returns the inverse of the quaternion.

func Log

func Log(q IJKX) IJKX

Log returns the logarithm of this quaternion. Multiplies this quaternion's rotation axis by its rotation angle, and stores the result in the returned quaternion's vector part (x, y, and z). The returned quaternion's real part (w) is always 0.0.

func Mul

func Mul(a, b IJKX) IJKX

func MulX

func MulX[X Int.Any | Float.Any](a IJKX, b X) IJKX

func Neg

func Neg(v IJKX) IJKX

func New

func New() IJKX

New constructs a Quaternion identical to the Identity.

func Normalized

func Normalized(q IJKX) IJKX

Normalized returns a copy of the quaternion, normalized to unit length.

func Slerp

func Slerp[X Float.Any](a, b IJKX, weight X) IJKX

Slerp returns the result of the spherical linear interpolation between this quaternion and to by amount weight.

Note: Both quaternions must be normalized.

func Slerpni

func Slerpni[X Float.Any](a, b IJKX, weight X) IJKX

Slerpni returns the result of the spherical linear interpolation between this quaternion and to by amount weight, but without checking if the rotation path is not bigger than 90 degrees.

func SphericalCubicInterpolate

func SphericalCubicInterpolate[X Float.Any](a, b, pre_a, post_b IJKX, weight X) IJKX

SphericalCubicInterpolate performs a spherical cubic interpolation between quaternions pre_a, this vector, b, and post_b, by the given amount weight.

func SphericalCubicInterpolateInTime

func SphericalCubicInterpolateInTime[X Float.Any](a, b, pre_a, post_b IJKX, weight, b_t, pre_a_t, post_b_t X) IJKX

SphericalCubicInterpolateInTime performs a spherical cubic interpolation between quaternions pre_a, this vector, b, and post_b, by the given amount weight.

It can perform smoother interpolation than spherical_cubic_interpolate by the time values.

func Sub

func Sub(a, b IJKX) IJKX

func SubX

func SubX[X Int.Any | Float.Any](a IJKX, b X) IJKX

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL