Quaternion
A unit quaternion used for representing 3D rotations.
Quaternion.swift:13struct Quaternion
Quaternions are similar to Basis
, which implements the matrix representation of rotations. Unlike Basis
, which stores rotation, scale, and shearing, quaternions only store rotation.
Quaternions can be parametrized using both an axis-angle pair or Euler angles. Due to their compactness and the way they are stored in memory, certain operations (obtaining axis-angle and performing SLERP, in particular) are more efficient and robust against floating-point errors.
Citizens in SwiftGodot
Conformances
protocol DoubleScalable
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol IntScalable
protocol SelfVariantRepresentable
Structs and scalar types use their own layout for storage.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
init(
) Constructs a default-initialized quaternion with all components set to
0
.init(arcFrom: Vector3, arcTo: Vector3
) Constructs a quaternion representing the shortest arc between two points on the surface of a sphere with a radius of
1.0
.init(axis: Vector3, angle: Float
) Constructs a quaternion that will rotate around the given axis by the specified angle. The axis must be a normalized vector.
init(from: Quaternion
) Constructs a
Quaternion
as a copy of the givenQuaternion
.init(from: Basis
) Constructs a quaternion from the given
Basis
.init(x: Float, y: Float, z: Float, w: Float
) Constructs a quaternion defined by the given values.
static var godotType: Variant.GType
static let identity: Quaternion
The identity quaternion, representing no rotation. Equivalent to an identity
Basis
matrix. If a vector is transformed by an identity quaternion, it will not change.static func fromEuler(Vector3
) -> Quaternion Constructs a Quaternion from Euler angles in YXZ rotation order.
static func != (lhs: Quaternion, rhs: Quaternion
) -> Bool Returns
true
if the quaternions are not equal.static func * (lhs: Quaternion, rhs: Quaternion
) -> Quaternion Multiplies each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func * (lhs: Quaternion, rhs: Double
) -> Quaternion Multiplies each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func * (lhs: Quaternion, rhs: Int64
) -> Quaternion Multiplies each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func * (lhs: Quaternion, rhs: Vector3
) -> Vector3 Multiplies each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func + (lhs: Quaternion, rhs: Quaternion
) -> Quaternion Adds each component of the left
Quaternion
to the rightQuaternion
. This operation is not meaningful on its own, but it can be used as a part of a larger expression, such as approximating an intermediate rotation between two nearby rotations.static func - (q: `Self`
) -> Quaternion Returns the negative value of the Quaternion. This is the same as writing Quaternion(-q.x, -q.y, -q.z, -q.w). This operation results in a quaternion that represents the same rotation.
static func - (lhs: Quaternion, rhs: Quaternion
) -> Quaternion Subtracts each component of the left
Quaternion
by the rightQuaternion
. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func / (lhs: Quaternion, rhs: Double
) -> Quaternion Divides each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func / (lhs: Quaternion, rhs: Int64
) -> Quaternion Divides each component of the
Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.static func == (lhs: Quaternion, rhs: Quaternion
) -> Bool Returns
true
if the quaternions are exactly equal.
Instance members
var w: Float
W component of the quaternion (real part).
var x: Float
X component of the quaternion (imaginary
i
axis part).var y: Float
Y component of the quaternion (imaginary
j
axis part).var z: Float
Z component of the quaternion (imaginary
k
axis part).subscript(Int64
) -> Double func angleTo(Quaternion
) -> Double 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.func dot(with: Quaternion
) -> Double Returns the dot product of two quaternions.
func exp(
) -> Quaternion func getAngle(
) -> Double func getAxis(
) -> Vector3 func getEuler(order: Int64
) -> Vector3 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 theEulerOrder
enum for possible values. The returned vector contains the rotation angles in the format (X angle, Y angle, Z angle).func inverse(
) -> Quaternion Returns the inverse of the quaternion.
func isEqualApprox(to: Quaternion
) -> Bool Returns
true
if this quaternion andto
are approximately equal, by running@GlobalScope.is_equal_approx
on each component.func isFinite(
) -> Bool Returns
true
if this quaternion is finite, by calling@GlobalScope.is_finite
on each component.func isNormalized(
) -> Bool Returns whether the quaternion is normalized or not.
func length(
) -> Double Returns the length of the quaternion.
func lengthSquared(
) -> Double Returns the length of the quaternion, squared.
func log(
) -> Quaternion func normalized(
) -> Quaternion Returns a copy of the quaternion, normalized to unit length.
func slerp(to: Quaternion, weight: Double
) -> Quaternion Returns the result of the spherical linear interpolation between this quaternion and
to
by amountweight
.func slerpni(to: Quaternion, weight: Double
) -> Quaternion Returns the result of the spherical linear interpolation between this quaternion and
to
by amountweight
, but without checking if the rotation path is not bigger than 90 degrees.func sphericalCubicInterpolate(b: Quaternion, preA: Quaternion, postB: Quaternion, weight: Double
) -> Quaternion Performs a spherical cubic interpolation between quaternions
preA
, this vector,b
, andpostB
, by the given amountweight
.func sphericalCubicInterpolateInTime(b: Quaternion, preA: Quaternion, postB: Quaternion, weight: Double, bT: Double, preAT: Double, postBT: Double
) -> Quaternion Performs a spherical cubic interpolation between quaternions
preA
, this vector,b
, andpostB
, by the given amountweight
.
Type features
init?(Variant
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
static func * (lhs: Self, rhs: Int
) -> Self static func *= (lhs: inout Self, rhs: Int
) static func *= (lhs: inout Self, rhs: Double
) static func / (lhs: Self, rhs: Int
) -> Self static func /= (lhs: inout Self, rhs: Int
) static func /= (lhs: inout Self, rhs: Double
)