Vector2
A 2D vector using floating point coordinates.
Vector2.swift:15struct Vector2
A 2-element structure that can be used to represent 2D coordinates or any other pair 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 Vector2i
for its integer counterpart.
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 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
Vector2
with all components set to0
.init(from: Vector2
) init(from: Vector2i
) init(x: Float, y: Float
) Constructs a new
Vector2
from the givenx
andy
.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 down: Vector2
Down unit vector. Y is down in 2D, so this vector points +Y.
static var godotType: Variant.GType
static let inf: Vector2
Infinity vector, a vector with all components set to
@GDScript.INF
.static let left: Vector2
Left unit vector. Represents the direction of left.
static let one: Vector2
One vector, a vector with all components set to
1
.static let right: Vector2
Right unit vector. Represents the direction of right.
static let up: Vector2
Up unit vector. Y is down in 2D, so this vector points -Y.
static let zero: Vector2
Zero vector, a vector with all components set to
0
.static func fromAngle(Double
) -> Vector2 Creates a unit
Vector2
rotated to the givenangle
in radians. This is equivalent to doingVector2(cos(angle), sin(angle))
orVector2.RIGHT.rotated(angle)
.static func != (lhs: Vector2, rhs: Vector2
) -> Bool Returns
true
if the vectors are not equal.static func * (lhs: Vector2, rhs: Transform2D
) -> Vector2 Multiplies each component of the
Vector2
by the given integer.static func * (lhs: Vector2, rhs: Vector2
) -> Vector2 Multiplies each component of the
Vector2
by the given integer.static func * (lhs: Vector2, rhs: Double
) -> Vector2 Multiplies each component of the
Vector2
by the given integer.static func * (lhs: Vector2, rhs: Int64
) -> Vector2 Multiplies each component of the
Vector2
by the given integer.static func *= (left: inout `Self`, right: `Self`
) static func + (lhs: Vector2, rhs: Vector2
) -> Vector2 Adds each component of the
Vector2
by the components of the givenVector2
.static func += (left: inout `Self`, right: `Self`
) static func - (v: `Self`
) -> Vector2 Returns the negative value of the Vector2. This is the same as writing Vector2(-v.x, -v.y). 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: Vector2, rhs: Vector2
) -> Vector2 Subtracts each component of the
Vector2
by the components of the givenVector2
.static func -= (left: inout `Self`, right: `Self`
) static func / (lhs: Vector2, rhs: Vector2
) -> Vector2 Divides each component of the
Vector2
by the given integer.static func / (lhs: Vector2, rhs: Double
) -> Vector2 Divides each component of the
Vector2
by the given integer.static func / (lhs: Vector2, rhs: Int64
) -> Vector2 Divides each component of the
Vector2
by the given integer.static func /= (left: inout `Self`, right: `Self`
) static func < (lhs: Vector2, rhs: Vector2
) -> Bool Compares two
Vector2
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. This operator is useful for sorting vectors.static func <= (lhs: Vector2, rhs: Vector2
) -> Bool Compares two
Vector2
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. This operator is useful for sorting vectors.static func == (lhs: Vector2, rhs: Vector2
) -> Bool Returns
true
if the vectors are exactly equal.static func > (lhs: Vector2, rhs: Vector2
) -> Bool Compares two
Vector2
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. This operator is useful for sorting vectors.static func >= (lhs: Vector2, rhs: Vector2
) -> Bool Compares two
Vector2
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. This operator is useful for sorting vectors.
Instance members
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]
.subscript(Int64
) -> Double func abs(
) -> Vector2 Returns a new vector with all components in absolute values (i.e. positive).
func angle(
) -> Double Returns this vector’s angle with respect to the positive X axis, or
(1, 0)
vector, in radians.func angleTo(Vector2
) -> Double Returns the angle to the given vector, in radians.
func angleToPoint(to: Vector2
) -> Double Returns the angle between the line connecting the two points and the X axis, in radians.
func aspect(
) -> Double Returns the aspect ratio of this vector, the ratio of
x
toy
.func bezierDerivative(control1: Vector2, control2: Vector2, end: Vector2, t: Double
) -> Vector2 Returns the derivative at the given
t
on the Bézier curve defined by this vector and the givencontrol1
,control2
, andend
points.func bezierInterpolate(control1: Vector2, control2: Vector2, end: Vector2, t: Double
) -> Vector2 Returns the point at the given
t
on the Bézier curve defined by this vector and the givencontrol1
,control2
, andend
points.func bounce(n: Vector2
) -> Vector2 Returns a new vector “bounced off” from a plane defined by the given normal.
func ceil(
) -> Vector2 Returns a new vector with all components rounded up (towards positive infinity).
func clamp(min: Vector2, max: Vector2
) -> Vector2 Returns a new vector with all components clamped between the components of
min
andmax
, by running@GlobalScope.clamp
on each component.func cross(with: Vector2
) -> Double Returns the 2D analog of the cross product for this vector and
with
.func cubicInterpolate(b: Vector2, preA: Vector2, postB: Vector2, weight: Double
) -> Vector2 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: Vector2, preA: Vector2, postB: Vector2, weight: Double, bT: Double, preAT: Double, postBT: Double
) -> Vector2 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(Vector2
) -> Vector2 Returns the normalized vector pointing from this vector to
to
. This is equivalent to using(b - a).normalized()
.func distanceSquaredTo(Vector2
) -> Double Returns the squared distance between this vector and
to
.func distanceTo(Vector2
) -> Double Returns the distance between this vector and
to
.func dot(with: Vector2
) -> Double Returns the dot product of this vector and
with
. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player.func floor(
) -> Vector2 Returns a new vector with all components rounded down (towards negative infinity).
func isEqualApprox(to: Vector2
) -> 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
) -> Vector2 func limitLength(Double
) -> Vector2 Returns the vector with a maximum length by limiting its length to
length
.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 minAxisIndex(
) -> Int64 Returns the axis of the vector’s lowest value. See
AXIS_*
constants. If all components are equal, this method returns .y.func moveToward(to: Vector2, delta: Double
) -> Vector2 Returns a new vector moved toward
to
by the fixeddelta
amount. Will not go past the final value.func normalized(
) -> Vector2 Returns the result of scaling the vector to unit length. Equivalent to
v / v.length()
. See alsoisNormalized
.func orthogonal(
) -> Vector2 Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
func posmod(mod: Double
) -> Vector2 Returns a vector composed of the
@GlobalScope.fposmod
of this vector’s components andmod
.func posmodv(modv: Vector2
) -> Vector2 Returns a vector composed of the
@GlobalScope.fposmod
of this vector’s components andmodv
’s components.func project(b: Vector2
) -> Vector2 Returns the result of projecting the vector onto the given vector
b
.func reflect(n: Vector2
) -> Vector2 Returns the result of reflecting the vector from a line defined by the given direction vector
n
.func rotated(angle: Double
) -> Vector2 Returns the result of rotating this vector by
angle
(in radians). See also@GlobalScope.deg_to_rad
.func round(
) -> Vector2 Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
func sign(
) -> Vector2 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 slerp(to: Vector2, weight: Double
) -> Vector2 Returns the result of spherical linear interpolation between this vector and
to
, by amountweight
.weight
is on the range of 0.0 to 1.0, representing the amount of interpolation.func slide(n: Vector2
) -> Vector2 Returns the result of sliding the vector along a plane defined by the given normal.
func snapped(step: `Self`
) -> Vector2
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
)