Vector4
A 4D vector using floating-point coordinates.
Vector4.swift:21struct Vector4
A 4-element structure that can be used to represent 4D coordinates or any other quadruplet of numeric values.
It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike float which is always 64-bit. If double precision is needed, compile the engine with the option precision=double
.
See Vector4i
for its integer counterpart.
Citizens in SwiftGodot
Conformances
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol DoubleScalable
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol IntScalable
protocol LinearInterpolation
protocol SelfVariantRepresentable
Structs and scalar types use their own layout for storage.
protocol Snappable
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.
Types
Type members
init(
) Constructs a default-initialized
Vector4
with all components set to0
.init(from: Vector4
) init(from: Vector4i
) init(x: Float, y: Float, z: Float, w: Float
) Returns a
Vector4
with the given components.static let axisW: Int
Enumerated value for the W axis. Returned by
maxAxisIndex
andminAxisIndex
.static let axisX: Int
Enumerated value for the X axis. Returned by
maxAxisIndex
andminAxisIndex
.static let axisY: Int
Enumerated value for the Y axis. Returned by
maxAxisIndex
andminAxisIndex
.static let axisZ: Int
Enumerated value for the Z axis. Returned by
maxAxisIndex
andminAxisIndex
.static var godotType: Variant.GType
static let inf: Vector4
Infinity vector, a vector with all components set to
@GDScript.INF
.static let one: Vector4
One vector, a vector with all components set to
1
.static let zero: Vector4
Zero vector, a vector with all components set to
0
.static func != (lhs: Vector4, rhs: Vector4
) -> Bool Returns
true
if the vectors are not equal.static func * (lhs: Vector4, rhs: Projection
) -> Vector4 Transforms (multiplies) the
Vector4
by the transpose of the givenProjection
matrix.static func * (lhs: Vector4, rhs: Vector4
) -> Vector4 Multiplies each component of the
Vector4
by the components of the givenVector4
.static func * (lhs: Vector4, rhs: Double
) -> Vector4 Multiplies each component of the
Vector4
by the given float.static func * (lhs: Vector4, rhs: Int64
) -> Vector4 Multiplies each component of the
Vector4
by the given integer.static func *= (left: inout `Self`, right: `Self`
) static func + (lhs: Vector4, rhs: Vector4
) -> Vector4 Adds each component of the
Vector4
by the components of the givenVector4
.static func += (left: inout `Self`, right: `Self`
) static func - (v: `Self`
) -> Vector4 Returns the negative value of the Vector4. This is the same as writing Vector4(-v.x, -v.y, -v.z, -v.w). This operation flips the direction of the vector while keeping the same magnitude. With floats, the number zero can be either positive or negative.
static func - (lhs: Vector4, rhs: Vector4
) -> Vector4 Subtracts each component of the
Vector4
by the components of the givenVector4
.static func -= (left: inout `Self`, right: `Self`
) static func / (lhs: Vector4, rhs: Vector4
) -> Vector4 Divides each component of the
Vector4
by the components of the givenVector4
.static func / (lhs: Vector4, rhs: Double
) -> Vector4 Divides each component of the
Vector4
by the given float.static func / (lhs: Vector4, rhs: Int64
) -> Vector4 Divides each component of the
Vector4
by the given integer.static func /= (left: inout `Self`, right: `Self`
) static func < (lhs: Vector4, rhs: Vector4
) -> Bool Compares two
Vector4
vectors by first checking if the X value of the left vector is less than the X value of theright
vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.static func <= (lhs: Vector4, rhs: Vector4
) -> Bool Compares two
Vector4
vectors by first checking if the X value of the left vector is less than or equal to the X value of theright
vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.static func == (lhs: Vector4, rhs: Vector4
) -> Bool Returns
true
if the vectors are exactly equal.static func > (lhs: Vector4, rhs: Vector4
) -> Bool Compares two
Vector4
vectors by first checking if the X value of the left vector is greater than the X value of theright
vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.static func >= (lhs: Vector4, rhs: Vector4
) -> Bool Compares two
Vector4
vectors by first checking if the X value of the left vector is greater than or equal to the X value of theright
vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.
Instance members
var w: Float
The vector’s W component. Also accessible by using the index position
[3]
.var x: Float
The vector’s X component. Also accessible by using the index position
[0]
.var y: Float
The vector’s Y component. Also accessible by using the index position
[1]
.var z: Float
The vector’s Z component. Also accessible by using the index position
[2]
.subscript(Int64
) -> Double func abs(
) -> Vector4 Returns a new vector with all components in absolute values (i.e. positive).
func ceil(
) -> Vector4 Returns a new vector with all components rounded up (towards positive infinity).
func clamp(min: Vector4, max: Vector4
) -> Vector4 Returns a new vector with all components clamped between the components of
min
andmax
, by running@GlobalScope.clamp
on each component.func clampf(min: Double, max: Double
) -> Vector4 Returns a new vector with all components clamped between
min
andmax
, by running@GlobalScope.clamp
on each component.func cubicInterpolate(b: Vector4, preA: Vector4, postB: Vector4, weight: Double
) -> Vector4 Performs a cubic interpolation between this vector and
b
usingpreA
andpostB
as handles, and returns the result at positionweight
.weight
is on the range of 0.0 to 1.0, representing the amount of interpolation.func cubicInterpolateInTime(b: Vector4, preA: Vector4, postB: Vector4, weight: Double, bT: Double, preAT: Double, postBT: Double
) -> Vector4 Performs a cubic interpolation between this vector and
b
usingpreA
andpostB
as handles, and returns the result at positionweight
.weight
is on the range of 0.0 to 1.0, representing the amount of interpolation.func directionTo(Vector4
) -> Vector4 Returns the normalized vector pointing from this vector to
to
. This is equivalent to using(b - a).normalized()
.func distanceSquaredTo(Vector4
) -> Double Returns the squared distance between this vector and
to
.func distanceTo(Vector4
) -> Double Returns the distance between this vector and
to
.func dot(with: Vector4
) -> Double Returns the dot product of this vector and
with
.func floor(
) -> Vector4 Returns a new vector with all components rounded down (towards negative infinity).
func inverse(
) -> Vector4 Returns the inverse of the vector. This is the same as
Vector4(1.0 / v.x, 1.0 / v.y, 1.0 / v.z, 1.0 / v.w)
.func isEqualApprox(to: Vector4
) -> Bool Returns
true
if this vector andto
are approximately equal, by running@GlobalScope.is_equal_approx
on each component.func isFinite(
) -> Bool Returns
true
if this vector is finite, by calling@GlobalScope.is_finite
on each component.func isNormalized(
) -> Bool Returns
true
if the vector is normalized, i.e. its length is approximately equal to 1.func isZeroApprox(
) -> Bool Returns
true
if this vector’s values are approximately zero, by running@GlobalScope.is_zero_approx
on each component.func length(
) -> Double Returns the length (magnitude) of this vector.
func lengthSquared(
) -> Double Returns the squared length (squared magnitude) of this vector.
func lerp(to: `Self`, weight: any BinaryFloatingPoint
) -> Vector4 func max(with: Vector4
) -> Vector4 Returns the component-wise maximum of this and
with
, equivalent toVector4(maxf(x, with.x), maxf(y, with.y), maxf(z, with.z), maxf(w, with.w))
.func maxAxisIndex(
) -> Int64 Returns the axis of the vector’s highest value. See
AXIS_*
constants. If all components are equal, this method returns .x.func maxf(with: Double
) -> Vector4 Returns the component-wise maximum of this and
with
, equivalent toVector4(maxf(x, with), maxf(y, with), maxf(z, with), maxf(w, with))
.func min(with: Vector4
) -> Vector4 Returns the component-wise minimum of this and
with
, equivalent toVector4(minf(x, with.x), minf(y, with.y), minf(z, with.z), minf(w, with.w))
.func minAxisIndex(
) -> Int64 Returns the axis of the vector’s lowest value. See
AXIS_*
constants. If all components are equal, this method returns .w.func minf(with: Double
) -> Vector4 Returns the component-wise minimum of this and
with
, equivalent toVector4(minf(x, with), minf(y, with), minf(z, with), minf(w, with))
.func normalized(
) -> Vector4 Returns the result of scaling the vector to unit length. Equivalent to
v / v.length()
. Returns(0, 0, 0, 0)
ifv.length() == 0
. See alsoisNormalized
.func posmod(mod: Double
) -> Vector4 Returns a vector composed of the
@GlobalScope.fposmod
of this vector’s components andmod
.func posmodv(modv: Vector4
) -> Vector4 Returns a vector composed of the
@GlobalScope.fposmod
of this vector’s components andmodv
’s components.func round(
) -> Vector4 Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
func sign(
) -> Vector4 Returns a new vector with each component set to
1.0
if it’s positive,-1.0
if it’s negative, and0.0
if it’s zero. The result is identical to calling@GlobalScope.sign
on each component.func snapped(step: `Self`
) -> Vector4 func snappedf(step: Double
) -> Vector4 Returns a new vector with each component snapped to the nearest multiple of
step
. This can also be used to round the components to an arbitrary number of decimals.
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
)