Up to date

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

Basis

A 3×3 matrix for representing 3D rotation and scale.

Description

A 3×3 matrix used for representing 3D rotation and scale. Usually used as an orthogonal basis for a Transform3D.

Contains 3 vector fields X, Y and Z as its columns, which are typically interpreted as the local basis vectors of a transformation. For such use, it is composed of a scaling and a rotation matrix, in that order (M = R.S).

Basis can also be accessed as an array of 3D vectors. These vectors are usually orthogonal to each other, but are not necessarily normalized (due to scaling).

For more information, read the "Matrices and transforms" documentation article.

備註

There are notable differences when using this API with C#. See C# API 與 GDScript 的不同 for more information.

Tutorials

Properties

Vector3

x

Vector3(1, 0, 0)

Vector3

y

Vector3(0, 1, 0)

Vector3

z

Vector3(0, 0, 1)

Constructors

Basis

Basis ( )

Basis

Basis ( Basis from )

Basis

Basis ( Vector3 axis, float angle )

Basis

Basis ( Quaternion from )

Basis

Basis ( Vector3 x_axis, Vector3 y_axis, Vector3 z_axis )

Methods

float

determinant ( ) const

Basis

from_euler ( Vector3 euler, int order=2 ) static

Basis

from_scale ( Vector3 scale ) static

Vector3

get_euler ( int order=2 ) const

Quaternion

get_rotation_quaternion ( ) const

Vector3

get_scale ( ) const

Basis

inverse ( ) const

bool

is_conformal ( ) const

bool

is_equal_approx ( Basis b ) const

bool

is_finite ( ) const

Basis

looking_at ( Vector3 target, Vector3 up=Vector3(0, 1, 0), bool use_model_front=false ) static

Basis

orthonormalized ( ) const

Basis

rotated ( Vector3 axis, float angle ) const

Basis

scaled ( Vector3 scale ) const

Basis

slerp ( Basis to, float weight ) const

float

tdotx ( Vector3 with ) const

float

tdoty ( Vector3 with ) const

float

tdotz ( Vector3 with ) const

Basis

transposed ( ) const

Operators

bool

operator != ( Basis right )

Basis

operator * ( Basis right )

Vector3

operator * ( Vector3 right )

Basis

operator * ( float right )

Basis

operator * ( int right )

bool

operator == ( Basis right )

Vector3

operator [] ( int index )


Constants

IDENTITY = Basis(1, 0, 0, 0, 1, 0, 0, 0, 1)

The identity basis, with no rotation or scaling applied.

This is identical to creating Basis without any parameters. This constant can be used to make your code clearer, and for consistency with C#.

FLIP_X = Basis(-1, 0, 0, 0, 1, 0, 0, 0, 1)

The basis that will flip something along the X axis when used in a transformation.

FLIP_Y = Basis(1, 0, 0, 0, -1, 0, 0, 0, 1)

The basis that will flip something along the Y axis when used in a transformation.

FLIP_Z = Basis(1, 0, 0, 0, 1, 0, 0, 0, -1)

The basis that will flip something along the Z axis when used in a transformation.


Property Descriptions

Vector3 x = Vector3(1, 0, 0)

The basis matrix's X vector (column 0). Equivalent to array index 0.


Vector3 y = Vector3(0, 1, 0)

The basis matrix's Y vector (column 1). Equivalent to array index 1.


Vector3 z = Vector3(0, 0, 1)

The basis matrix's Z vector (column 2). Equivalent to array index 2.


Constructor Descriptions

Basis Basis ( )

Constructs a default-initialized Basis set to IDENTITY.


Basis Basis ( Basis from )

Constructs a Basis as a copy of the given Basis.


Basis Basis ( Vector3 axis, float angle )

Constructs a pure rotation basis matrix, rotated around the given axis by angle (in radians). The axis must be a normalized vector.


Basis Basis ( Quaternion from )

Constructs a pure rotation basis matrix from the given quaternion.


Basis Basis ( Vector3 x_axis, Vector3 y_axis, Vector3 z_axis )

Constructs a basis matrix from 3 axis vectors (matrix columns).


Method Descriptions

float determinant ( ) const

Returns the determinant of the basis matrix. If the basis is uniformly scaled, its determinant is the square of the scale.

A negative determinant means the basis has a negative scale. A zero determinant means the basis isn't invertible, and is usually considered invalid.


Basis from_euler ( Vector3 euler, int order=2 ) static

Constructs a pure rotation Basis matrix from Euler angles in the specified Euler rotation order. By default, use YXZ order (most common). See the EulerOrder enum for possible values.


Basis from_scale ( Vector3 scale ) static

Constructs a pure scale basis matrix with no rotation or shearing. The scale values are set as the diagonal of the matrix, and the other parts of the matrix are zero.


Vector3 get_euler ( int order=2 ) const

Returns the basis's rotation in the form of Euler angles. The Euler order depends on the order parameter, by default it uses the YXZ convention: when decomposing, first Z, then X, and Y last. The returned vector contains the rotation angles in the format (X angle, Y angle, Z angle).

Consider using the get_rotation_quaternion method instead, which returns a Quaternion quaternion instead of Euler angles.


Quaternion get_rotation_quaternion ( ) const

Returns the basis's rotation in the form of a quaternion. See get_euler if you need Euler angles, but keep in mind quaternions should generally be preferred to Euler angles.


Vector3 get_scale ( ) const

Assuming that the matrix is the combination of a rotation and scaling, return the absolute value of scaling factors along each axis.


Basis inverse ( ) const

Returns the inverse of the matrix.


bool is_conformal ( ) const

Returns true if the basis is conformal, meaning it preserves angles and distance ratios, and may only be composed of rotation and uniform scale. Returns false if the basis has non-uniform scale or shear/skew. This can be used to validate if the basis is non-distorted, which is important for physics and other use cases.


bool is_equal_approx ( Basis b ) const

Returns true if this basis and b are approximately equal, by calling @GlobalScope.is_equal_approx on all vector components.


bool is_finite ( ) const

Returns true if this basis is finite, by calling @GlobalScope.is_finite on all vector components.


Basis looking_at ( Vector3 target, Vector3 up=Vector3(0, 1, 0), bool use_model_front=false ) static

Creates a Basis with a rotation such that the forward axis (-Z) points towards the target position.

The up axis (+Y) points as close to the up vector as possible while staying perpendicular to the forward axis. The resulting Basis is orthonormalized. The target and up vectors cannot be zero, and cannot be parallel to each other.

If use_model_front is true, the +Z axis (asset front) is treated as forward (implies +X is left) and points toward the target position. By default, the -Z axis (camera forward) is treated as forward (implies +X is right).


Basis orthonormalized ( ) const

Returns the orthonormalized version of the matrix (useful to call from time to time to avoid rounding error for orthogonal matrices). This performs a Gram-Schmidt orthonormalization on the basis of the matrix.


Basis rotated ( Vector3 axis, float angle ) const

Introduce an additional rotation around the given axis by angle (in radians). The axis must be a normalized vector.


Basis scaled ( Vector3 scale ) const

Introduce an additional scaling specified by the given 3D scaling factor.


Basis slerp ( Basis to, float weight ) const

Assuming that the matrix is a proper rotation matrix, slerp performs a spherical-linear interpolation with another rotation matrix.


float tdotx ( Vector3 with ) const

Transposed dot product with the X axis of the matrix.


float tdoty ( Vector3 with ) const

Transposed dot product with the Y axis of the matrix.


float tdotz ( Vector3 with ) const

Transposed dot product with the Z axis of the matrix.


Basis transposed ( ) const

Returns the transposed version of the matrix.


Operator Descriptions

bool operator != ( Basis right )

Returns true if the Basis matrices are not equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.


Basis operator * ( Basis right )

Composes these two basis matrices by multiplying them together. This has the effect of transforming the second basis (the child) by the first basis (the parent).


Vector3 operator * ( Vector3 right )

Transforms (multiplies) the Vector3 by the given Basis matrix.


Basis operator * ( float right )

This operator multiplies all components of the Basis, which scales it uniformly.


Basis operator * ( int right )

This operator multiplies all components of the Basis, which scales it uniformly.