Basis
Three axes of a linear 3D transform without translation.
Basis stores rotation, scale and possible shear. Basis.Identity is the identity basis; a zero struct is not a substitute. Uniform scale s has determinant s³; s = 2 gives 8. A zero determinant means no inverse exists.
Pure orientation needs orthonormal axes or a unit Quaternion. Orthonormalized changes the original scale/skew. Transform a normal under nonuniform scale with Inverse().Transposed(), then normalize; ordinary Basis multiplication need not preserve surface perpendicularity.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors4
Basis4 overloads
public Basis(Quaternion quaternion);Constructs a Basis from 3 axis vectors. These are the columns of the basis matrix.
| Name | Type | Default |
|---|---|---|
quaternion | Quaternion | — |
quaternionThe quaternion to create the basis from.
public Basis(Vector3 column0, Vector3 column1, Vector3 column2);Constructs a Basis from 3 axis vectors. These are the columns of the basis matrix.
| Name | Type | Default |
|---|---|---|
column0 | Vector3 | — |
column1 | Vector3 | — |
column2 | Vector3 | — |
column0The X vector, or Column0.
column1The Y vector, or Column1.
column2The Z vector, or Column2.
public Basis(Vector3 axis, float angle);Constructs a Basis from 3 axis vectors. These are the columns of the basis matrix.
| Name | Type | Default |
|---|---|---|
axis | Vector3 | — |
angle | float | — |
axisThe axis to rotate around. Must be normalized.
angleThe angle to rotate, in radians.
public Basis(float xx, float yx, float zx, float xy, float yy, float zy, float xz, float yz, float zz);Constructs a Basis from 3 axis vectors. These are the columns of the basis matrix.
| Name | Type | Default |
|---|---|---|
xx | float | — |
yx | float | — |
zx | float | — |
xy | float | — |
yy | float | — |
zy | float | — |
xz | float | — |
yz | float | — |
zz | float | — |
xxThe X component of the X column vector, accessed via b.X.X or [0][0].
yxThe X component of the Y column vector, accessed via b.Y.X or [1][0].
zxThe X component of the Z column vector, accessed via b.Z.X or [2][0].
xyThe Y component of the X column vector, accessed via b.X.Y or [0][1].
yyThe Y component of the Y column vector, accessed via b.Y.Y or [1][1].
zyThe Y component of the Z column vector, accessed via b.Y.Y or [2][1].
xzThe Z component of the X column vector, accessed via b.X.Y or [0][2].
yzThe Z component of the Y column vector, accessed via b.Y.Y or [1][2].
zzThe Z component of the Z column vector, accessed via b.Y.Y or [2][2].
Properties13
Column0Property
public Vector3 Column0 { get; set; }Column 0 of the basis matrix (the X vector).
Column1Property
public Vector3 Column1 { get; set; }Column 1 of the basis matrix (the Y vector).
Column2Property
public Vector3 Column2 { get; set; }Column 2 of the basis matrix (the Z vector).
FlipXProperty
public static Basis FlipX { get; }When any basis is multiplied by Basis.FlipX, it negates all components of the Basis.X axis (the X column).
When Basis.FlipX is multiplied by any basis, it negates the Vector3.X component of all axes (the X row).
FlipYProperty
public static Basis FlipY { get; }When any basis is multiplied by Basis.FlipY, it negates all components of the Basis.Y axis (the Y column).
When Basis.FlipY is multiplied by any basis, it negates the Vector3.Y component of all axes (the Y row).
FlipZProperty
public static Basis FlipZ { get; }When any basis is multiplied by Basis.FlipZ, it negates all components of the Basis.Z axis (the Z column).
When Basis.FlipZ is multiplied by any basis, it negates the Vector3.Z component of all axes (the Z row).
IdentityProperty
public static Basis Identity { get; }The identity Basis. This is an orthonormal basis with no rotation, no shear, and a scale of Vector3.One. This also means that:
- The
Basis.Xpoints right (Vector3.Right); - The
Basis.Ypoints up (Vector3.Up); - The
Basis.Zpoints back (Vector3.Back).
If a Vector3 or another Basis is transformed (multiplied) by this constant, no transformation occurs.
Note: In C#, this constant is equivalent to creating a [constructor Basis] without any arguments. It can be used to make your code clearer, and for consistency with C#.
Item2 overloads
public Vector3 this[int column] { get; set; }Access whole columns in the form of Vector3.
public float this[int column, int row] { get; set; }Access matrix elements in column-major order.
ScaleProperty
public Vector3 Scale { get; }Assuming that the matrix is the combination of a rotation and scaling, return the absolute value of scaling factors along each axis.
XProperty
public Vector3 X { get; set; }The basis's X axis, and the column 0 of the matrix.
On the identity basis, this vector points right (Vector3.Right).
YProperty
public Vector3 Y { get; set; }The basis's Y axis, and the column 1 of the matrix.
On the identity basis, this vector points up (Vector3.Up).
ZProperty
public Vector3 Z { get; set; }The basis's Z axis, and the column 2 of the matrix.
On the identity basis, this vector points back (Vector3.Back).
Methods29
Determinantfloat
public float Determinant();Determinant of the 3×3 matrix. Uniform scale s gives s³: scale 2 yields 8. Zero means noninvertible; a negative sign reverses basis orientation.
floatThe determinant of the basis matrix.
Equals2 overloads
public bool Equals(Basis other);Returns true if the basis matrices are exactly
equal. Note: Due to floating-point precision errors, consider using
Basis.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
other | Basis | — |
otherThe other basis.
boolWhether or not the basis matrices are exactly equal.
public override bool Equals(object? obj);Returns true if the Basis is
exactly equal to the given object (obj).
Note: Due to floating-point precision errors, consider using
Basis.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
obj | object | — |
objThe object to compare with.
boolWhether or not the basis matrix and the object are exactly equal.
FromEulerBasis
public static Basis FromEuler(Vector3 euler, EulerOrder order = EulerOrder.Yxz);Constructs a new Basis that only represents rotation from the given Vector3 of Euler angles, in radians.
- The
Vector3.Xshould contain the angle around theBasis.Xaxis (pitch); - The
Vector3.Yshould contain the angle around theBasis.Yaxis (yaw); - The
Vector3.Zshould contain the angle around theBasis.Zaxis (roll).
// Creates a Basis whose z axis points down.
var myBasis = Basis.FromEuler(new Vector3(Mathf.Tau / 4.0f, 0.0f, 0.0f));
ReckfellApi.Print(myBasis.Z); // Prints (0, -1, 0)The order of each consecutive rotation can be changed with order (see EulerOrder constants). In Reckfell, Euler angles always use intrinsic order. By default, the intrinsic YXZ convention is used (EULER_ORDER_YXZ): the basis rotates first around the local Y axis (yaw), then local X (pitch), and lastly local Z (roll). When using the opposite method Basis.GetEuler to decompose a rotation, this order is reversed.
| Name | Type | Default |
|---|---|---|
euler | Vector3 | — |
order | EulerOrder | EulerOrder.Yxz |
eulerThe Euler angles to use.
orderThe order to compose the Euler angles.
BasisFromScaleBasis
public static Basis FromScale(Vector3 scale);Constructs a new Basis that only represents scale, with no rotation or shear, from the given scale vector.
var myBasis = Basis.FromScale(new Vector3(2.0f, 4.0f, 8.0f));
ReckfellApi.Print(myBasis.X); // Prints (2, 0, 0)
ReckfellApi.Print(myBasis.Y); // Prints (0, 4, 0)
ReckfellApi.Print(myBasis.Z); // Prints (0, 0, 8)Note: In linear algebra, the matrix of this basis is also known as a diagonal matrix.
| Name | Type | Default |
|---|---|---|
scale | Vector3 | — |
scaleThe scale Vector3.
BasisA pure scale Basis matrix.
GetEulerVector3
public Vector3 GetEuler(EulerOrder order = EulerOrder.Yxz);Returns this basis's rotation as a Vector3 of Euler angles, in radians. For the returned value:
- The
Vector3.Xcontains the angle around theBasis.Xaxis (pitch); - The
Vector3.Ycontains the angle around theBasis.Yaxis (yaw); - The
Vector3.Zcontains the angle around theBasis.Zaxis (roll). The order of each consecutive rotation can be changed withorder(seeEulerOrderconstants). In Reckfell, Euler angles always use intrinsic order. By default, the intrinsic YXZ convention is used (EULER_ORDER_YXZ): since we are decomposing, local Z (roll) is calculated first, then local X (pitch), and lastly local Y (yaw). When using the opposite methodBasis.FromEulerto compose a rotation, this order is reversed. Note: For this method to return correctly, the basis needs to be orthonormal (seeBasis.Orthonormalized). Note: Euler angles are much more intuitive but are not suitable for 3D math. Because of this, consider using theBasis.GetRotationQuaternionmethod instead, which returns aQuaternion. Note: In the Inspector dock, a basis's rotation is often displayed in Euler angles (in degrees), as is the case with theEntity3D.Rotationproperty.
| Name | Type | Default |
|---|---|---|
order | EulerOrder | EulerOrder.Yxz |
orderThe Euler order to use. By default, use YXZ order (most common).
GetHashCodeint
public override int GetHashCode();Serves as the hash function for Basis.
intA hash code for this basis.
GetRotationQuaternionQuaternion
public Quaternion GetRotationQuaternion();Returns this basis's rotation as a Quaternion.
Note: Quaternions are much more suitable for 3D math but are less intuitive. For user interfaces, consider using the Basis.GetEuler method, which returns Euler angles.
QuaternionThe basis rotation.
InverseBasis
public Basis Inverse();Returns the inverse Basis. An exactly zero determinant throws InvalidOperationException; near-degenerate inputs can greatly amplify numerical error.
BasisThe inverse value. An exactly zero determinant throws InvalidOperationException instead of returning a result.
IsEqualApproxbool
public bool IsEqualApprox(Basis other);Returns true if this basis and b are approximately equal, by calling @GlobalScope.is_equal_approx on all vector components.
| Name | Type | Default |
|---|---|---|
other | Basis | — |
otherThe other basis to compare.
boolWhether or not the bases are approximately equal.
IsFinitebool
public bool IsFinite();Returns true if this basis is finite, by calling @GlobalScope.is_finite on all vector components.
boolWhether this vector is finite or not.
LookingAt2 overloads
public static Basis LookingAt(Vector3 target, Vector3 up);Creates a new Basis with a rotation such that the forward axis (-Z) points towards the target position.
By default, the -Z axis (camera forward) is treated as forward (implies +X is right). 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.
The up axis (+Y) points as close to the up vector as possible while staying perpendicular to the forward axis. The returned basis is orthonormalized (see Basis.Orthonormalized).
The target and the up cannot be Vector3.Zero, and shouldn't be colinear to avoid unintended rotation around local Z axis.
| Name | Type | Default |
|---|---|---|
target | Vector3 | — |
up | Vector3 | — |
Basispublic static Basis LookingAt(Vector3 target, Vector3? up = default, bool useModelFront = false);Creates a new Basis with a rotation such that the forward axis (-Z) points towards the target position.
By default, the -Z axis (camera forward) is treated as forward (implies +X is right). 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.
The up axis (+Y) points as close to the up vector as possible while staying perpendicular to the forward axis. The returned basis is orthonormalized (see Basis.Orthonormalized).
The target and the up cannot be Vector3.Zero, and shouldn't be colinear to avoid unintended rotation around local Z axis.
| Name | Type | Default |
|---|---|---|
target | Vector3 | — |
up | Vector3? | default |
useModelFront | bool | false |
targetThe position to look at.
upThe relative up direction.
useModelFrontIf true, then the model is oriented in reverse, towards the model front axis (+Z, Vector3.ModelFront), which is more useful for orienting 3D models.
BasisThe resulting basis matrix.
OrthonormalizedBasis
public Basis Orthonormalized();Returns the orthonormalized version of this basis. An orthonormal basis is both orthogonal (the axes are perpendicular to each other) and normalized (the axes have a length of 1.0), which also means it can only represent a rotation.
It is often useful to call this method to avoid rounding errors on a rotating basis:
// Rotate this Entity3D every frame.
public override void _Process(double delta)
{
Basis = Basis.Rotated(Vector3.Up, Mathf.Tau * (float)delta)
.Rotated(Vector3.Right, Mathf.Tau * (float)delta)
.Orthonormalized();
}BasisAn orthonormalized basis matrix.
RotatedBasis
public Basis Rotated(Vector3 axis, float angle);Returns a copy of this basis rotated around the given axis by the given angle (in radians).
The axis must be a normalized vector (see Vector3.Normalized). If angle is positive, the basis is rotated counter-clockwise around the axis.
var myBasis = Basis.Identity;
var angle = Mathf.Tau / 2.0f;
myBasis = myBasis.Rotated(Vector3.Up, angle); // Rotate around the up axis (yaw).
myBasis = myBasis.Rotated(Vector3.Right, angle); // Rotate around the right axis (pitch).
myBasis = myBasis.Rotated(Vector3.Back, angle); // Rotate around the back axis (roll).| Name | Type | Default |
|---|---|---|
axis | Vector3 | — |
angle | float | — |
axisThe axis to rotate around. Must be normalized.
angleThe angle to rotate, in radians.
BasisThe rotated basis matrix.
ScaledBasis
public Basis Scaled(Vector3 scale);Returns this basis with each axis's components scaled by the given scale's components.
The basis matrix's rows are multiplied by scale's components. This operation is a global scale (relative to the parent).
var myBasis = new Basis(
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(2.0f, 2.0f, 2.0f),
new Vector3(3.0f, 3.0f, 3.0f)
);
myBasis = myBasis.Scaled(new Vector3(0.0f, 2.0f, -2.0f));
ReckfellApi.Print(myBasis.X); // Prints (0, 2, -2)
ReckfellApi.Print(myBasis.Y); // Prints (0, 4, -4)
ReckfellApi.Print(myBasis.Z); // Prints (0, 6, -6)| Name | Type | Default |
|---|---|---|
scale | Vector3 | — |
scaleThe scale to introduce.
BasisThe scaled basis matrix.
ScaledLocalBasis
public Basis ScaledLocal(Vector3 scale);Returns this basis with each axis scaled by the corresponding component in the given scale.
The basis matrix's columns are multiplied by scale's components. This operation is a local scale (relative to self).
var myBasis = new Basis(
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(2.0f, 2.0f, 2.0f),
new Vector3(3.0f, 3.0f, 3.0f)
);
myBasis = myBasis.ScaledLocal(new Vector3(0.0f, 2.0f, -2.0f));
ReckfellApi.Print(myBasis.X); // Prints (0, 0, 0)
ReckfellApi.Print(myBasis.Y); // Prints (4, 4, 4)
ReckfellApi.Print(myBasis.Z); // Prints (-6, -6, -6)| Name | Type | Default |
|---|---|---|
scale | Vector3 | — |
scaleThe scale to introduce.
BasisThe scaled basis matrix.
SlerpBasis
public Basis Slerp(Basis target, float weight);Performs a spherical-linear interpolation with the to basis, given a weight. Both this basis and to should represent a rotation.
Example: Smoothly rotate a Entity3D to the target basis over time, with a Tween:
| Name | Type | Default |
|---|---|---|
target | Basis | — |
weight | float | — |
targetThe destination basis for interpolation.
weightA value on the range of 0.0 to 1.0, representing the amount of interpolation.
BasisThe resulting basis matrix of the interpolation.
Tdotxfloat
public float Tdotx(Vector3 with);Returns the transposed dot product between with and the Basis.X axis (see Basis.Transposed).
This is equivalent to basis.x.dot(vector).
| Name | Type | Default |
|---|---|---|
with | Vector3 | — |
withA vector to calculate the dot product with.
floatThe resulting dot product.
Tdotyfloat
public float Tdoty(Vector3 with);Returns the transposed dot product between with and the Basis.Y axis (see Basis.Transposed).
This is equivalent to basis.y.dot(vector).
| Name | Type | Default |
|---|---|---|
with | Vector3 | — |
withA vector to calculate the dot product with.
floatThe resulting dot product.
Tdotzfloat
public float Tdotz(Vector3 with);Returns the transposed dot product between with and the Basis.Z axis (see Basis.Transposed).
This is equivalent to basis.z.dot(vector).
| Name | Type | Default |
|---|---|---|
with | Vector3 | — |
withA vector to calculate the dot product with.
floatThe resulting dot product.
ToString2 overloads
public override string ToString();Converts this Basis to a string.
stringA string representation of this basis.
public string ToString(string? format);Converts this Basis to a string with the given format.
| Name | Type | Default |
|---|---|---|
format | string | — |
stringA string representation of this basis.
TransposedBasis
public Basis Transposed();Returns the transposed version of this basis. This turns the basis matrix's columns into rows, and its rows into columns.
var myBasis = new Basis(
new Vector3(1.0f, 2.0f, 3.0f),
new Vector3(4.0f, 5.0f, 6.0f),
new Vector3(7.0f, 8.0f, 9.0f)
);
myBasis = myBasis.Transposed();
ReckfellApi.Print(myBasis.X); // Prints (1, 4, 7)
ReckfellApi.Print(myBasis.Y); // Prints (2, 5, 8)
ReckfellApi.Print(myBasis.Z); // Prints (3, 6, 9)BasisThe transposed basis matrix.
op_Equalitybool
public static bool operator ==(Basis left, Basis right);Returns true if the basis matrices are exactly
equal. Note: Due to floating-point precision errors, consider using
Basis.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
left | Basis | — |
right | Basis | — |
leftThe left basis.
rightThe right basis.
boolWhether or not the basis matrices are exactly equal.
op_Inequalitybool
public static bool operator !=(Basis left, Basis right);Returns true if the basis matrices are not equal.
Note: Due to floating-point precision errors, consider using
Basis.IsEqualApprox instead, which is more reliable.
| Name | Type | Default |
|---|---|---|
left | Basis | — |
right | Basis | — |
leftThe left basis.
rightThe right basis.
boolWhether or not the basis matrices are not equal.
op_Multiply3 overloads
public static Basis operator *(Basis left, 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).
| Name | Type | Default |
|---|---|---|
left | Basis | — |
right | Basis | — |
leftThe parent basis.
rightThe child basis.
BasisThe composed basis.
public static Vector3 operator *(Basis basis, Vector3 vector);Returns a Vector3 transformed (multiplied) by the basis matrix.
| Name | Type | Default |
|---|---|---|
basis | Basis | — |
vector | Vector3 | — |
basisThe basis matrix transformation to apply.
vectorA Vector3 to transform.
Vector3The transformed Vector3.
public static Vector3 operator *(Vector3 vector, Basis basis);Returns a Vector3 transformed (multiplied) by the inverse basis matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
vector * basis is equivalent to basis.Transposed() * vector. See Basis.Transposed.
For transforming by inverse of a non-orthonormal basis (e.g. with scaling) basis.Inverse() * vector can be used instead. See Basis.Inverse.
| Name | Type | Default |
|---|---|---|
vector | Vector3 | — |
basis | Basis | — |
vectorA Vector3 to inversely transform.
basisThe basis matrix transformation to apply.
Vector3The inversely transformed vector.
Fields and constants3
Row0Field
public Vector3 Row0;Row 0 of the basis matrix. Shows which vectors contribute to the X direction. Rows are not very useful for user code, but are more efficient for some internal calculations.
Row1Field
public Vector3 Row1;Row 1 of the basis matrix. Shows which vectors contribute to the Y direction. Rows are not very useful for user code, but are more efficient for some internal calculations.
Row2Field
public Vector3 Row2;Row 2 of the basis matrix. Shows which vectors contribute to the Z direction. Rows are not very useful for user code, but are more efficient for some internal calculations.