Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Quaternion

A unit quaternion used for representing 3D rotations.

Description

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: w, x, y, and z. 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 get_angle, get_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 Godot provides several helper methods that handle it for you. These include slerp and spherical_cubic_interpolate, as well as the * operator.

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 the engine with the option precision=double.

Note

There are notable differences when using this API with C#. See C# API differences to GDScript for more information.

Tutorials

Properties

float

w

1.0

float

x

0.0

float

y

0.0

float

z

0.0

Constructors

Quaternion

Quaternion ( )

Quaternion

Quaternion ( Quaternion from )

Quaternion

Quaternion ( Vector3 arc_from, Vector3 arc_to )

Quaternion

Quaternion ( Vector3 axis, float angle )

Quaternion

Quaternion ( Basis from )

Quaternion

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

Methods

float

angle_to ( Quaternion to ) const

float

dot ( Quaternion with ) const

Quaternion

exp ( ) const

Quaternion

from_euler ( Vector3 euler ) static

float

get_angle ( ) const

Vector3

get_axis ( ) const

Vector3

get_euler ( int order=2 ) const

Quaternion

inverse ( ) const

bool

is_equal_approx ( Quaternion to ) const

bool

is_finite ( ) const

bool

is_normalized ( ) const

float

length ( ) const

float

length_squared ( ) const

Quaternion

log ( ) const

Quaternion

normalized ( ) const

Quaternion

slerp ( Quaternion to, float weight ) const

Quaternion

slerpni ( Quaternion to, float weight ) const

Quaternion

spherical_cubic_interpolate ( Quaternion b, Quaternion pre_a, Quaternion post_b, float weight ) const

Quaternion

spherical_cubic_interpolate_in_time ( Quaternion b, Quaternion pre_a, Quaternion post_b, float weight, float b_t, float pre_a_t, float post_b_t ) const

Operators

bool

operator != ( Quaternion right )

Quaternion

operator * ( Quaternion right )

Vector3

operator * ( Vector3 right )

Quaternion

operator * ( float right )

Quaternion

operator * ( int right )

Quaternion

operator + ( Quaternion right )

Quaternion

operator - ( Quaternion right )

Quaternion

operator / ( float right )

Quaternion

operator / ( int right )

bool

operator == ( Quaternion right )

float

operator [] ( int index )

Quaternion

operator unary+ ( )

Quaternion

operator unary- ( )


Constants

IDENTITY = Quaternion(0, 0, 0, 1)

The 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.


Property Descriptions

float w = 1.0

W component of the quaternion. This is the "real" part.

Note: Quaternion components should usually not be manipulated directly.


float x = 0.0

X component of the quaternion. This is the value along the "imaginary" i axis.

Note: Quaternion components should usually not be manipulated directly.


float y = 0.0

Y component of the quaternion. This is the value along the "imaginary" j axis.

Note: Quaternion components should usually not be manipulated directly.


float z = 0.0

Z component of the quaternion. This is the value along the "imaginary" k axis.

Note: Quaternion components should usually not be manipulated directly.


Constructor Descriptions

Quaternion Quaternion ( )

Constructs a Quaternion identical to the IDENTITY.


Quaternion Quaternion ( Quaternion from )

Constructs a Quaternion as a copy of the given Quaternion.


Quaternion Quaternion ( Vector3 arc_from, Vector3 arc_to )

Constructs a Quaternion representing the shortest arc between arc_from and arc_to. These can be imagined as two points intersecting a sphere's surface, with a radius of 1.0.


Quaternion Quaternion ( Vector3 axis, float angle )

Constructs a Quaternion representing rotation around the axis by the given angle, in radians. The axis must be a normalized vector.


Quaternion Quaternion ( Basis from )

Constructs a Quaternion from the given rotation Basis.

This constructor is faster than Basis.get_rotation_quaternion, but the given basis must be orthonormalized (see Basis.orthonormalized). Otherwise, the constructor fails and returns IDENTITY.