Inheritance #

Vector4




Table of contents

Vector4 #

float, builtin_classes

A 4D vector using floating-point coordinates.

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.

Note: In a boolean context, a Vector4 will evaluate to false if it's equal to Vector4(0, 0, 0, 0). Otherwise, a Vector4 will always evaluate to true.

Members #

var w: float = 0.0#

The vector's W component. Also accessible by using the index position 3.

var x: float = 0.0#

The vector's X component. Also accessible by using the index position 0.

var y: float = 0.0#

The vector's Y component. Also accessible by using the index position 1.

var z: float = 0.0#

The vector's Z component. Also accessible by using the index position 2.

Methods #

const func abs() -> Vector4#

Returns a new vector with all components in absolute values (i.e. positive).

const func ceil() -> Vector4#

Returns a new vector with all components rounded up (towards positive infinity).

const func clamp(max: Vector4) -> Vector4#

Returns a new vector with all components clamped between the components of min and max, by running @GlobalScope.clamp on each component.

const func clampf(max: float) -> Vector4#

Returns a new vector with all components clamped between min and max, by running @GlobalScope.clamp on each component.

const func cubic_interpolate(weight: float) -> Vector4#

Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

const func cubic_interpolate_in_time(post_b_t: float) -> Vector4#

Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

It can perform smoother interpolation than cubic_interpolate by the time values.

const func direction_to(to: Vector4) -> Vector4#

Returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).normalized().

const func distance_squared_to(to: Vector4) -> float#

Returns the squared distance between this vector and to.

This method runs faster than distance_to, so prefer it if you need to compare vectors or need the squared distance for some formula.

const func distance_to(to: Vector4) -> float#

Returns the distance between this vector and to.

const func dot(with: Vector4) -> float#

Returns the dot product of this vector and with.

const func floor() -> Vector4#

Returns a new vector with all components rounded down (towards negative infinity).

const 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).

const func is_equal_approx(to: Vector4) -> bool#

Returns true if this vector and to are approximately equal, by running @GlobalScope.is_equal_approx on each component.

const func is_finite() -> bool#

Returns true if this vector is finite, by calling @GlobalScope.is_finite on each component.

const func is_normalized() -> bool#

Returns true if the vector is normalized, i.e. its length is approximately equal to 1.

const func is_zero_approx() -> bool#

Returns true if this vector's values are approximately zero, by running @GlobalScope.is_zero_approx on each component.

This method is faster than using is_equal_approx with one value as a zero vector.

const func length() -> float#

Returns the length (magnitude) of this vector.

const func length_squared() -> float#

Returns the squared length (squared magnitude) of this vector.

This method runs faster than length, so prefer it if you need to compare vectors or need the squared distance for some formula.

const func lerp(weight: float) -> Vector4#

Returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

const func max(with: Vector4) -> Vector4#

Returns the component-wise maximum of this and with, equivalent to Vector4(maxf(x, with.x), maxf(y, with.y), maxf(z, with.z), maxf(w, with.w)).

const func max_axis_index() -> int#

Returns the axis of the vector's highest value. See AXIS_* constants. If all components are equal, this method returns AXIS_X.

const func maxf(with: float) -> Vector4#

Returns the component-wise maximum of this and with, equivalent to Vector4(maxf(x, with), maxf(y, with), maxf(z, with), maxf(w, with)).

const func min(with: Vector4) -> Vector4#

Returns the component-wise minimum of this and with, equivalent to Vector4(minf(x, with.x), minf(y, with.y), minf(z, with.z), minf(w, with.w)).

const func min_axis_index() -> int#

Returns the axis of the vector's lowest value. See AXIS_* constants. If all components are equal, this method returns AXIS_W.

const func minf(with: float) -> Vector4#

Returns the component-wise minimum of this and with, equivalent to Vector4(minf(x, with), minf(y, with), minf(z, with), minf(w, with)).

const func normalized() -> Vector4#

Returns the result of scaling the vector to unit length. Equivalent to v / v.length(). Returns (0, 0, 0, 0) if v.length() == 0. See also is_normalized.

Note: This function may return incorrect values if the input vector length is near zero.

const func posmod(mod: float) -> Vector4#

Returns a vector composed of the @GlobalScope.fposmod of this vector's components and mod.

const func posmodv(modv: Vector4) -> Vector4#

Returns a vector composed of the @GlobalScope.fposmod of this vector's components and modv's components.

const func round() -> Vector4#

Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

const func sign() -> Vector4#

Returns a new vector with each component set to 1.0 if it's positive, -1.0 if it's negative, and 0.0 if it's zero. The result is identical to calling @GlobalScope.sign on each component.

const func snapped(step: Vector4) -> Vector4#

Returns a new vector with each component snapped to the nearest multiple of the corresponding component in step. This can also be used to round the components to an arbitrary number of decimals.

const func snappedf(step: float) -> 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.

Annotations #

Constants #

const AXIS_X = 0 enum Axis#

Enumerated value for the X axis. Returned by max_axis_index and min_axis_index.

const AXIS_Y = 1 enum Axis#

Enumerated value for the Y axis. Returned by max_axis_index and min_axis_index.

const AXIS_Z = 2 enum Axis#

Enumerated value for the Z axis. Returned by max_axis_index and min_axis_index.

const AXIS_W = 3 enum Axis#

Enumerated value for the W axis. Returned by max_axis_index and min_axis_index.

const ZERO = Vector4(0, 0, 0, 0)#

Zero vector, a vector with all components set to 0.

const ONE = Vector4(1, 1, 1, 1)#

One vector, a vector with all components set to 1.

const INF = Vector4(inf, inf, inf, inf)#

Infinity vector, a vector with all components set to @GDScript.INF.

Constructors #

Vector4() -> Vector4 #

Constructs a default-initialized Vector4 with all components set to 0.

Vector4(from: Vector4) -> Vector4 #

Constructs a Vector4 as a copy of the given Vector4.

Vector4(from: Vector4i) -> Vector4 #

Constructs a new Vector4 from the given Vector4i.

Vector4(w: float) -> Vector4 #

Returns a Vector4 with the given components.

Enums #

Axis#

enum Axis { AXIS_X = 0, AXIS_Y = 1, AXIS_Z = 2, AXIS_W = 3, }

Notifications#

enum { ZERO = Vector4(0, 0, 0, 0), ONE = Vector4(1, 1, 1, 1), INF = Vector4(inf, inf, inf, inf), }

Operators #

Vector4 != Vector4 -> bool#

Returns true if the vectors are not equal.

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

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4 * Projection -> Vector4#

Transforms (multiplies) the Vector4 by the transpose of the given Projection matrix.

For transforming by inverse of a projection projection.inverse() * vector can be used instead. See Projection.inverse.

Vector4 * Vector4 -> Vector4#

Multiplies each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) * Vector4(3, 4, 5, 6)) # Prints (30.0, 80.0, 150.0, 240.0)

Vector4 * float -> Vector4#

Multiplies each component of the Vector4 by the given float.

print(Vector4(10, 20, 30, 40) * 2) # Prints (20.0, 40.0, 60.0, 80.0)

Vector4 * int -> Vector4#

Multiplies each component of the Vector4 by the given int.

Vector4 + Vector4 -> Vector4#

Adds each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) + Vector4(3, 4, 5, 6)) # Prints (13.0, 24.0, 35.0, 46.0)

Vector4 - Vector4 -> Vector4#

Subtracts each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) - Vector4(3, 4, 5, 6)) # Prints (7.0, 16.0, 25.0, 34.0)

Vector4 / Vector4 -> Vector4#

Divides each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) / Vector4(2, 5, 3, 4)) # Prints (5.0, 4.0, 10.0, 10.0)

Vector4 / float -> Vector4#

Divides each component of the Vector4 by the given float.

print(Vector4(10, 20, 30, 40) / 2) # Prints (5.0, 10.0, 15.0, 20.0)

Vector4 / int -> Vector4#

Divides each component of the Vector4 by the given int.

Vector4 < Vector4 -> bool#

Compares two Vector4 vectors by first checking if the X value of the left vector is less than the X value of the right 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.

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4 <= 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 the right 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.

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4 == Vector4 -> bool#

Returns true if the vectors are exactly equal.

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

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4 > Vector4 -> bool#

Compares two Vector4 vectors by first checking if the X value of the left vector is greater than the X value of the right 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.

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4 >= 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 the right 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.

Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Vector4[int] -> float#

Access vector components using their index. v0 is equivalent to v.x, v1 is equivalent to v.y, v2 is equivalent to v.z, and v3 is equivalent to v.w.

+Vector4 -> Vector4#

Returns the same value as if the + was not there. Unary + does nothing, but sometimes it can make your code more readable.

-Vector4 -> 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.

Signals #

Theme Items #

Tutorials #